Write a function that will receive a two-dimensional array (matrix) A of integers and, then, determine those elements of the array that their values are maximum in their respective rows and columns. Assume that the stored values are distinct. Print the subscripts of those elements. Example,
3 4 8 5
2 9 10 1
7 12 6 11
A[1][2]= 10 and A[2][1]= 12 satisfy the requirements and their subscripts should be printed.
CODE
#include<iostream.h>
#include<conio.h>
int matrix[3][4],row,col,maxrows=3,maxcols=4;
int maxrow1,maxrow2,maxrow3,maxcol1,maxcol2,maxcol3,maxcol4;
main()
{
/* enternig the martix elements */
for(row=0;row<maxrows;row++)
{
for(col=0;col<maxcols;col++)
{
cout<<"Enter the Array Elements=["<<row<<","<<col<<"] ";
cin>>matrix[row][col];
}
}
/* matrix completed */
/* dislay the elements of the matrix with sub-scripts */
cout<< "The values Entered into Matrix are="<<endl;
for(row=0;row<maxrows;row++)
{
for(col=0;col<maxcols;col++)
{
cout<<"\t"<<matrix[row][col];
}
cout<<endl;
}
/* maximum element of row1 is displayed */
maxrow1=matrix[0][0];
for(int i=0;i<4;i++)
{
if( matrix[0][i]>matrix[0][0])
{
int temp=matrix[0][i];
maxrow1=temp;
}
}
cout<<"\nMaxrow1 element="<<maxrow1;
/* maximum element of row2 is displayed */
maxrow2=matrix[1][0];
for(int i=0;i<4;i++)
{
if( matrix[1][i]>matrix[1][0])
{
int temp=matrix[1][i];
maxrow2=temp;
}
}
cout<<"\nMaxrow2 element="<<maxrow2;
/* maximum element of row3 is displayed */
maxrow3=matrix[2][0];
for(int i=0;i<4;i++)
{
if( matrix[2][i]>matrix[2][0])
{
int temp=matrix[2][i];
maxrow3=temp;
}
}
cout<<"\nMaxrow3 element="<<maxrow3<<endl;;
/* maximum colums */
/* maximum element of col1 is displayed */
maxcol1=matrix[0][0];
for(int i=1;i<3;i++)
{
if( matrix[i][0]>matrix[0][0])
{
int temp=matrix[i][0];
maxcol1=temp;
}
}
cout<<"\nMaxcol1 element="<<maxcol1;
/* maximum element of col2 is displayed */
maxcol2=matrix[0][1];
for(int i=1;i<3;i++)
{
if( matrix[i][1]>matrix[0][1])
{
int temp=matrix[i][1];
maxcol2=temp;
}
}
cout<<"\nMaxcol2 element="<<maxcol2;
/* maximum element of col3 is displayed */
maxcol3=matrix[0][2];
for(int i=1;i<3;i++)
{
if( matrix[i][2]>matrix[0][2])
{
int temp=matrix[i][2];
maxcol3=temp;
}
}
cout<<"\nMaxcol3 element="<<maxcol3;
/* maximum element of col4 is displayed */
maxcol4=matrix[0][3];
for(int i=1;i<3;i++)
{
if( matrix[i][3]>matrix[0][2])
{
int temp=matrix[i][3];
maxcol4=temp;
}
}
cout<<"\nMaxcol4 element="<<maxcol4;
return 0;
}
the program is not working properly , any help would be much appreciated.
This post has been edited by jjhaag: 12 Nov, 2007 - 07:46 PM