Programs on array manipulation (1D &2D)
Write a program to input 10 numbers from the user and print the average of smallest and largest number.
#include<conio.h>
void main()
{
int i,num[10],avg,max,min;
cout<<”Enter any 10
number”;
for(i=0;i<10;i++)
{
cin>>num[i];
}
max=num[0],min=num[0];
for(i=0;i<10;i++)
{
if(max<num[i])
max=num[i];
if(min>num[i])
min=num[i];
}
avg=(min+max)/2;
cout<<”The average of
largest and smallest number is”<<avg;
getch()
}
output
Enter any 10 numbers1
2
3
4
5
6
7
8
9
1
The average of largest and
smallest number is5
Write a program to input 10 number from the user and print the sum of odd numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,num[10];s=0;
cout<<”Enter any 10
number”;
for(i=0;i<10;i++)
{
cin>>num[i];
}
for(i=0;i<10;i++)
{
if((num[i]%2)==1)
{
s=s+num[i];
}
}
cout<<”/n the sum of
odd numbers”<<s;
getch();
}
output
Enter any 10 numbers1
2
3
4
5
6
7
8
9
10
the sum of odd numbers25
Write a program to search entered number by the user in the list of 10 numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10]={10,20,30,40,50,60,70,80,90,100};
int i,flag=1,key;
cout<<”Enter the
number”;
cin>>key;
for(i=0;i<10;i++)
if(key=arr[i])
{
flag==0;
}
break;
if(flag==0)
cout<<”The element is
found”;
else
cout<<”The element is
not found”;
getch()
}
Output
Enter the number30
The element is found
Write c++ program to print sum of diagonals element of 3*3 array entered by user.
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[3][3];i,j,sl=0;sr=0;
cout<<”Enter element in
3*3 array”;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
}
for(i-0;i<3;i++)
{
for(j=0;j<3;j++)
{
{
if(i==j)
sr=sr+arr[i][j];
}
{
if((i+j)=2)
sl=sl+arr[i][j];
}
}
}
cout<<”The sum of right
diagonal”<<sr;
cout<<”The sum of left
diagonal”<<sl;
getch();
}
Output
Enter the element
in3*3matrix1
2
3
4
5
6
7
8
9
The sum of right diagonal15
The sum of left diagonal15
Write a program to input elements in 3*3 array and print sum of rows
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[3][3],i,j,s;
cout<<”Enter element in
3*3 array”;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
for(i-0;i<3;i++)
s=0;
{
for(j=0;j<3;j++)
{
s=s+n[i][j];
}
cout<<”The sum of
row<<i+1<<’is’<<s;
}
getch();
}
Output
Enter element in3*3array1
2
3
4
5
6
7
8
The sum of row 1 is12
The sum of row 2 is15
The sum of row 3 is18
0 Comments