Your answer is one click away!

Kartik Sibal February 2016
### Locating the largest element in the row of a multi dimensional array

I'd like to subtract the highest element in this 5x2 matrix, with it's subsequent element, in the other column.

For ex: the highest element right now is 150, whose location is (4,1). I'd like to subtract **150** with **89**, which is it's subsequent element. In the same way if the highest element belonged to the first column, then it should subtract itself from the element in the 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 you declare the array wrong, i build 2D array as

```
int a[2][5] = {
{140,82,89,150,110},
{110,112,106,88,90}
};
```

First, you want to find the 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 you save the biggest and it's coordinate at n and m variable.

After that, you want to subtract it with column - 1 (column + 1 if it is on the first column)

```
if(m == 0){
big -= a[n][m + 1];
}
else{
big -= a[n][m - 1];
}
```

Asked in February 2016

Viewed 3,883 times

Voted 6

Answered 1 times

Viewed 3,883 times

Voted 6

Answered 1 times