You are on page 1of 3

Q1) Write a C program that reads a two dimensional array of size

4*4.Construct a one dimensional array that contains all the array


elements whose values between 25 and 70.

int main()
{
int a[4][4],b[16],i,j,k,c;
cout<<"enter a two dimensional array of size 4*4\n";
for(i=0;i<4;i++)
for(j=0;j<4;j++)
cin>>a[i][j];
k=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j]>25&&a[i][j]<70)
{
b[k]=a[i][j];
k++;
}
for(i=0;i<k;i++)
cout<<’\t’,b[i];
}
Q2)Write a C program that reads a two dimensional float array 3*4.
compute the average value of the array elements. Count how
many elements are greater than the average.

#include <iostream>
using namespace std;
int main()
{ const int row=3, col=4;
int arr[row][col],cont=0,i,j;
float sum=0,avr;
cout<<"enter"<<row<<'*'<<col<<"array";
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>arr[i][j];

for(i=0;i<row;i++)
for(j=0;j<col;j++)
sum+=arr[i][j];

avr=sum/(row*col);
cout << "average=" << avr<<endl;

for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(arr[i][j]>avr)
cont++;
cout<<"no. of elements greater than the average are"<<cont;
return 0;
}
Q3)write a program that reads a one dimensional array and
exchange the maximum with minimum.
#include <iostream>
using namespace std;

int main()
{int const n=5;
int x[n],i,maxloc=0,minloc=0;
cout<<"Enter array elements :\n";
for(i=0;i<n;i++)
cin>>x[i];

int max=x[0],min=x[0];
for(i=0;i<n;i++)
{
if(x[i]>max)
{
max=x[i];
maxloc=i;
}
if(x[i]<min)
{
min=x[i];
minloc=i;
}
}
cout<<"\nmax="<<max;
cout<<"\nmin="<<min;
cout<<"\nThe array elemnent before exchange\n";
for(i=0;i<n;i++)
cout<<'\t'<<x[i];
x[minloc]=max;
x[maxloc]=min;
cout<<"\nThe array elemnent after exchange\n";
for(i=0;i<n;i++)
cout<<'\t'<<x[i];
}

You might also like