c++ - Locating the largest element in the row of a multi dimensional array -
i'd subtract highest element in 5x2 matrix, it's subsequent element, in other column.
for ex: highest element right 150, location (4,1). i'd subtract 150 89, it's subsequent element. in same way if highest element belonged first column, should subtract element in next column.
thanks
int big=0,lead=0,m,n; int a[5][2]={140,82,89,150,110,110,112,106,88,90}; for(int j=0; j<5; j++) { for(int i=0 ; i<2; i++) { m=j; n=i; if(a[j][i] > big) { big = a[j][i]; if(big == a[j][i]) { lead = big-a[j][1]; } else { lead = big-a[1][i]; } } } } cout<<big<<"\n"<<lead<<"\n"<<m<<","<<n<<endl; }
i think declare array wrong, build 2d array
int a[2][5] = { {140,82,89,150,110}, {110,112,106,88,90} };
first, want find largest using big variable (i assume). so
int big = 0,n,m; for(int x = 0; x < 2; x++){ for(int y = 0; y < 5; y++){ if(a[x][y] > big){ big = a[x][y]; n = x; m = y; } } }
here save biggest , it's coordinate @ n , m variable.
after that, want subtract column - 1 (column + 1 if on first column)
if(m == 0){ big -= a[n][m + 1]; } else{ big -= a[n][m - 1]; }
Comments
Post a Comment