Sample Qustion 10
Write a C++ program to display
a. Smallest value in each column
b. Smallest value in each row
c. Largest value in each column
d. Largest value in each row
e. Overall smallest value and largest value
f. Column name of the overall largest
value
Use the give 2D array.
#include<iostream>
using namespace std;
int main()
{
int arr[3][4] = {{34,56,9,89} , {80,22,56,15} , {34,67,12,67}};
cout << "--- Array Displaying ---" << endl;
for(int i = 0; i <3; i++)
{
for(int j=0 ; j< 4 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
int allsmallest = arr[0][0];
int alllargest = arr[0][0];
for(int a=0 ; a < 3 ; a++)
{
for(int b = 0; b < 4 ; b++)
{
if(arr[a][b]> allsmallest)
{
allsmallest = arr[a][b];
}
else if(arr[a][b]< alllargest)
{
alllargest = arr[a][b];
}
}
}
cout <<endl;
cout <<"Overall largest value is :"<<alllargest <<endl;
cout <<"Overall smallest value is :"<<allsmallest<<endl;
cout <<endl ;
int x = alllargest;
int y = allsmallest;
for(int i=0 ;i < 3; i++)
{
for(int j=0; j < 4; j++)
{
if(x==arr[i][j])
{
cout <<"Row number is "<<i<<" Column number is "<< j << endl;
}
}
}
cout << endl;
for(int i=0 ;i < 3; i++)
{
for(int j=0; j < 4; j++)
{
if(y==arr[i][j])
{
cout <<"Row number is "<<i<<" Column number is "<< j << endl;
}
}
}
cout << endl ;
cout << "--- C1 Displaying ---" << endl;
for(int i = 0; i <3; i++)
{
for(int j=0 ; j< 1 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int c1smaler = arr[0][0];
int c1largest = arr[0][0];
for (int a = 0; a <3 ; a++)
{
for(int b =0; b<1 ;b++)
{
if (arr[a][b] < c1smaler)
{
c1smaler = arr[a][b];
}
else if (arr[a][b] > c1largest)
{
c1largest = arr[a][b];
}
}
}
cout <<"c1 smallest value :"<<c1smaler<<endl;
cout <<"c1 largest value :"<<c1largest<<endl;
cout << endl ;
cout << "--- C2 Displaying ---" << endl;
for(int i = 0; i <3; i++)
{
for(int j=1 ; j< 2 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int c2smaler = arr[0][1];
int c2largest = arr[0][1];
for (int a = 0; a <3 ; a++)
{
for(int b =1; b<2 ;b++)
{
if (arr[a][b] < c2smaler)
{
c2smaler = arr[a][b];
}else if (arr[a][b] > c2largest)
{
c2largest = arr[a][b];
}
}
}
cout <<"c2 smallest value :"<<c2smaler<<endl;
cout <<"c2 largest value :"<<c2largest<<endl;
cout << endl ;
cout << "--- C3 Displaying ---" << endl;
for(int i = 0; i <3; i++)
{
for(int j=2 ; j< 3 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int c3smaler = arr[0][1];
int c3largest = arr[0][1];
for (int a = 0; a <3 ; a++)
{
for(int b =2; b<3 ;b++)
{
if (arr[a][b] < c3smaler)
{
c3smaler = arr[a][b];
}
else if(arr[a][b] >c3largest)
{
c3largest = arr[a][b];
}
}
}
cout <<"c3 smallest value :"<<c3smaler<<endl;
cout <<"c3 largest value :"<<c3largest<<endl;
cout << endl ;
cout << "--- C4 Displaying ---" << endl;
for(int i = 0; i <3; i++)
{
for(int j=3 ; j< 4 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int c4smaler = arr[0][1];
int c4largest = arr[0][1];
for (int a = 0; a <3 ; a++)
{
for(int b =3; b<4 ;b++)
{
if (arr[a][b] < c4smaler)
{
c4smaler = arr[a][b];
}
else if(arr[a][b] > c4largest)
{
c4largest = arr[a][b];
}
}
}
cout <<"c4 smallest value :"<<c4smaler<<endl;
cout <<"c4 largest value :"<<c4largest<<endl;
cout << endl ;
cout << "--- R1 Displaying ---" << endl;
for(int i = 0; i <1; i++)
{
for(int j=0 ; j< 4 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int r1smaler = arr[0][0];
int r1largest = arr[0][0];
for (int a = 0; a <1 ; a++)
{
for(int b =0; b<4 ;b++)
{
if (arr[a][b] < r1smaler)
{
r1smaler = arr[a][b];
}
else if(arr[a][b] > r1largest)
{
r1largest = arr[a][b];
}
}
}
cout <<"r1 smallest value :"<<r1smaler<<endl;
cout <<"r1 largest value :"<<r1largest<<endl;
cout << endl ;
cout << "--- R2 Displaying ---" << endl;
for(int i = 1; i <2; i++)
{
for(int j=0 ; j< 4 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int r2smaler = arr[1][0];
int r2largest = arr[1][0];
for (int a = 1; a <2 ; a++)
{
for(int b =0; b<4 ;b++)
{
if (arr[a][b] < r2smaler)
{
r2smaler = arr[a][b];
}
else if(arr[a][b] > r2largest)
{
r2largest = arr[a][b];
}
}
}
cout <<"r2 smallest value :"<<r2smaler<<endl;
cout <<"r2 largest value :"<<r2largest<<endl;
cout << endl ;
cout << "--- R3 Displaying ---" << endl;
for(int i = 2; i <3; i++)
{
for(int j=0 ; j< 4 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl ;
int r3smaler = arr[2][0];
int r3largest = arr[2][0];
for (int a = 2; a <3 ; a++)
{
for(int b =0; b<4 ;b++)
{
if (arr[a][b] < r3smaler)
{
r3smaler = arr[a][b];
}
else if(arr[a][b] > r3largest)
{
r3largest = arr[a][b];
}
}
}
cout <<"r3 smallest value :"<<r3smaler<<endl;
cout <<"r3 largest value :"<<r3largest<<endl;
return 0;
}
download file cpp file
do you have any qustion please comment
click the advertisement to help for me ....
a. Smallest value in each column
b. Smallest value in each row
c. Largest value in each column
d. Largest value in each row
e. Overall smallest value and largest value
f. Column name of the overall largest
value
Use the give 2D array.
#include<iostream>
using namespace std;
int main()
{
int arr[3][4] = {{34,56,9,89} , {80,22,56,15} , {34,67,12,67}};
cout << "--- Array Displaying ---" << endl;
for(int i = 0; i <3; i++)
{
for(int j=0 ; j< 4 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
int allsmallest = arr[0][0];
int alllargest = arr[0][0];
for(int a=0 ; a < 3 ; a++)
{
for(int b = 0; b < 4 ; b++)
{
if(arr[a][b]> allsmallest)
{
allsmallest = arr[a][b];
}
else if(arr[a][b]< alllargest)
{
alllargest = arr[a][b];
}
}
}
cout <<endl;
cout <<"Overall largest value is :"<<alllargest <<endl;
cout <<"Overall smallest value is :"<<allsmallest<<endl;
cout <<endl ;
int x = alllargest;
int y = allsmallest;
for(int i=0 ;i < 3; i++)
{
for(int j=0; j < 4; j++)
{
if(x==arr[i][j])
{
cout <<"Row number is "<<i<<" Column number is "<< j << endl;
}
}
}
cout << endl;
for(int i=0 ;i < 3; i++)
{
for(int j=0; j < 4; j++)
{
if(y==arr[i][j])
{
cout <<"Row number is "<<i<<" Column number is "<< j << endl;
}
}
}
cout << endl ;
cout << "--- C1 Displaying ---" << endl;
for(int i = 0; i <3; i++)
{
for(int j=0 ; j< 1 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int c1smaler = arr[0][0];
int c1largest = arr[0][0];
for (int a = 0; a <3 ; a++)
{
for(int b =0; b<1 ;b++)
{
if (arr[a][b] < c1smaler)
{
c1smaler = arr[a][b];
}
else if (arr[a][b] > c1largest)
{
c1largest = arr[a][b];
}
}
}
cout <<"c1 smallest value :"<<c1smaler<<endl;
cout <<"c1 largest value :"<<c1largest<<endl;
cout << endl ;
cout << "--- C2 Displaying ---" << endl;
for(int i = 0; i <3; i++)
{
for(int j=1 ; j< 2 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int c2smaler = arr[0][1];
int c2largest = arr[0][1];
for (int a = 0; a <3 ; a++)
{
for(int b =1; b<2 ;b++)
{
if (arr[a][b] < c2smaler)
{
c2smaler = arr[a][b];
}else if (arr[a][b] > c2largest)
{
c2largest = arr[a][b];
}
}
}
cout <<"c2 smallest value :"<<c2smaler<<endl;
cout <<"c2 largest value :"<<c2largest<<endl;
cout << endl ;
cout << "--- C3 Displaying ---" << endl;
for(int i = 0; i <3; i++)
{
for(int j=2 ; j< 3 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int c3smaler = arr[0][1];
int c3largest = arr[0][1];
for (int a = 0; a <3 ; a++)
{
for(int b =2; b<3 ;b++)
{
if (arr[a][b] < c3smaler)
{
c3smaler = arr[a][b];
}
else if(arr[a][b] >c3largest)
{
c3largest = arr[a][b];
}
}
}
cout <<"c3 smallest value :"<<c3smaler<<endl;
cout <<"c3 largest value :"<<c3largest<<endl;
cout << endl ;
cout << "--- C4 Displaying ---" << endl;
for(int i = 0; i <3; i++)
{
for(int j=3 ; j< 4 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int c4smaler = arr[0][1];
int c4largest = arr[0][1];
for (int a = 0; a <3 ; a++)
{
for(int b =3; b<4 ;b++)
{
if (arr[a][b] < c4smaler)
{
c4smaler = arr[a][b];
}
else if(arr[a][b] > c4largest)
{
c4largest = arr[a][b];
}
}
}
cout <<"c4 smallest value :"<<c4smaler<<endl;
cout <<"c4 largest value :"<<c4largest<<endl;
cout << endl ;
cout << "--- R1 Displaying ---" << endl;
for(int i = 0; i <1; i++)
{
for(int j=0 ; j< 4 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int r1smaler = arr[0][0];
int r1largest = arr[0][0];
for (int a = 0; a <1 ; a++)
{
for(int b =0; b<4 ;b++)
{
if (arr[a][b] < r1smaler)
{
r1smaler = arr[a][b];
}
else if(arr[a][b] > r1largest)
{
r1largest = arr[a][b];
}
}
}
cout <<"r1 smallest value :"<<r1smaler<<endl;
cout <<"r1 largest value :"<<r1largest<<endl;
cout << endl ;
cout << "--- R2 Displaying ---" << endl;
for(int i = 1; i <2; i++)
{
for(int j=0 ; j< 4 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
int r2smaler = arr[1][0];
int r2largest = arr[1][0];
for (int a = 1; a <2 ; a++)
{
for(int b =0; b<4 ;b++)
{
if (arr[a][b] < r2smaler)
{
r2smaler = arr[a][b];
}
else if(arr[a][b] > r2largest)
{
r2largest = arr[a][b];
}
}
}
cout <<"r2 smallest value :"<<r2smaler<<endl;
cout <<"r2 largest value :"<<r2largest<<endl;
cout << endl ;
cout << "--- R3 Displaying ---" << endl;
for(int i = 2; i <3; i++)
{
for(int j=0 ; j< 4 ; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl ;
int r3smaler = arr[2][0];
int r3largest = arr[2][0];
for (int a = 2; a <3 ; a++)
{
for(int b =0; b<4 ;b++)
{
if (arr[a][b] < r3smaler)
{
r3smaler = arr[a][b];
}
else if(arr[a][b] > r3largest)
{
r3largest = arr[a][b];
}
}
}
cout <<"r3 smallest value :"<<r3smaler<<endl;
cout <<"r3 largest value :"<<r3largest<<endl;
return 0;
}
download file cpp file
do you have any qustion please comment
click the advertisement to help for me ....
Comments
Post a Comment