//Using Static Array
using namespace std;
/*
Write functions that do the following:
Sum the elements of a ma (two-dimensional array) of any match.
Sum the elements on the even/odd rows of a matrix
Find the maximum/minimum value of a matrix.
Find the maximum value on the cth column of the matrix
Write a main program that uses these functions in two cases:
The input matrix is ??static with the given elements
The input matrix is ??dynamic with the number of rows, columns and element values ??entered from the keyboard.
*/
int main()
{
int arr[5][4] = {
{1, 3, 5, 7},
{3, 5, 0, 7},
{0, 2, 9, 6},
{1, 3, 5, 0},
{2, 5, 4, 8}
};
int r, c,Sum,rows,cols,Min,Max,cth;
// Sum the elements of a ma (two-dimensional array) of any match.
rows = sizeof(arr) / sizeof(arr[0]); // returns rows
cols = sizeof(arr[0]) / sizeof(int); // returns col for(r=0;r<)
cout<<"\nRows = "<<rows;
cout<<"\nCols = "<<cols<<endl;
for(r=0;r<rows;r++)
{
for(c=0;c<cols;c++) cout<<"\t"<<arr[r][c];
cout<<endl;
}
// Sum the elements on the even/odd rows of a matrix
for(r=0;r<rows;r++)
{
Sum=0;
if(r%2==0)
{
for(c=0;c<cols;c++) Sum = Sum + arr[r][c];
cout<<"\nSum of Even Row-"<<r<<" = "<<Sum;
}
Sum=0;
if(r%2==1)
{
for(c=0;c<cols;c++) Sum = Sum + arr[r][c];
cout<<"\nSum of Odd Row-"<<r<<" = "<<Sum;
}
}
// Find the maximum/minimum value of a matrix.
Min = arr[0][0]; Max = arr[0][0];
for(r=0;r<rows;r++)
{
for(c=0;c<cols;c++)
{
if(Min>=arr[r][c]) Min = arr[r][c];
if(Max<=arr[r][c]) Max = arr[r][c];
}
}
cout<<"\n\nMax. Value = "<<Max;
cout<<"\n\nMin. Value = "<<Min;
// Find the maximum value on the cth column of the matrix
cth = 3;
Min = arr[0][cth]; Max = arr[0][cth];
for(r=0;r<rows;r++)
{
if(Min>=arr[r][cth]) Min = arr[r][cth];
if(Max<=arr[r][cth]) Max = arr[r][cth];
}
cout<<"\n\nMin Value in "<<cth<<"th column = "<<Min;
cout<<"\n\nMax Value in "<<cth<<"th column = "<<Max;
}
int main()
{
int arr[5][4] = {
{1, 3, 5, 7},
{3, 5, 0, 7},
{0, 2, 9, 6},
{1, 3, 5, 0},
{2, 5, 4, 8}
};
int r, c,Sum,rows,cols,Min,Max,cth;
// Sum the elements of a ma (two-dimensional array) of any match.
rows = sizeof(arr) / sizeof(arr[0]); // returns rows
cols = sizeof(arr[0]) / sizeof(int); // returns col for(r=0;r<)
cout<<"\nRows = "<<rows;
cout<<"\nCols = "<<cols<<endl;
for(r=0;r<rows;r++)
{
for(c=0;c<cols;c++) cout<<"\t"<<arr[r][c];
cout<<endl;
}
// Sum the elements on the even/odd rows of a matrix
for(r=0;r<rows;r++)
{
Sum=0;
if(r%2==0)
{
for(c=0;c<cols;c++) Sum = Sum + arr[r][c];
cout<<"\nSum of Even Row-"<<r<<" = "<<Sum;
}
Sum=0;
if(r%2==1)
{
for(c=0;c<cols;c++) Sum = Sum + arr[r][c];
cout<<"\nSum of Odd Row-"<<r<<" = "<<Sum;
}
}
// Find the maximum/minimum value of a matrix.
Min = arr[0][0]; Max = arr[0][0];
for(r=0;r<rows;r++)
{
for(c=0;c<cols;c++)
{
if(Min>=arr[r][c]) Min = arr[r][c];
if(Max<=arr[r][c]) Max = arr[r][c];
}
}
cout<<"\n\nMax. Value = "<<Max;
cout<<"\n\nMin. Value = "<<Min;
// Find the maximum value on the cth column of the matrix
cth = 3;
Min = arr[0][cth]; Max = arr[0][cth];
for(r=0;r<rows;r++)
{
if(Min>=arr[r][cth]) Min = arr[r][cth];
if(Max<=arr[r][cth]) Max = arr[r][cth];
}
cout<<"\n\nMin Value in "<<cth<<"th column = "<<Min;
cout<<"\n\nMax Value in "<<cth<<"th column = "<<Max;
}
//Using Dynamic Array
int main()
{
int r, c,Sum,rows,cols,Min,Max,cth;
// Sum the elements of a ma (two-dimensional array) of any match.
cout<<"\nEnter no. of rows: "; cin>>rows;
cout<<"\nEnter no. of cols: "; cin>>cols;
int arr[rows][cols];
for(r=0;r<rows;r++)
{
for(c=0;c<cols;c++)
{
cout<<"Enter element Array["<<r<<"]["<<c<<"] = "; cin>>arr[r][c];
}
}
for(r=0;r<rows;r++)
{
for(c=0;c<cols;c++) cout<<"\t"<<arr[r][c];
cout<<endl;
}
// Sum the elements on the even/odd rows of a matrix
for(r=0;r<rows;r++)
{
Sum=0;
if(r%2==0)
{
for(c=0;c<cols;c++) Sum = Sum + arr[r][c];
cout<<"\nSum of Even Row-"<<r<<" = "<<Sum;
}
Sum=0;
if(r%2==1)
{
for(c=0;c<cols;c++) Sum = Sum + arr[r][c];
cout<<"\nSum of Odd Row-"<<r<<" = "<<Sum;
}
}
// Find the maximum/minimum value of a matrix.
Min = arr[0][0]; Max = arr[0][0];
for(r=0;r<rows;r++)
{
for(c=0;c<cols;c++)
{
if(Min>=arr[r][c]) Min = arr[r][c];
if(Max<=arr[r][c]) Max = arr[r][c];
}
}
cout<<"\n\nMax. Value = "<<Max;
cout<<"\n\nMin. Value = "<<Min;
// Find the maximum value on the cth column of the matrix
cth=cols+1;
while(cth<0 || cth>cols-1)
{
cout<<"\n\nEnter cth column: (0 to "<<cols-1<<")"; cin>>cth;
}
Min = arr[0][cth]; Max = arr[0][cth];
for(r=0;r<rows;r++)
{
if(Min>=arr[r][cth]) Min = arr[r][cth];
if(Max<=arr[r][cth]) Max = arr[r][cth];
}
cout<<"\n\nMin Value in "<<cth<<"th column = "<<Min;
cout<<"\n\nMax Value in "<<cth<<"th column = "<<Max;
}
Comments
Leave a comment