multidimensional array help

I have searched all over the internet and tried many different things but i cant seem to get this right. I'm trying to get this program to find the smallest number in this multidimensional array. I did that. Now I am trying to get it to find the sum of the row and column that contain the smallest number. I am having trouble with this. I can only get it to print out the sum of each row and column but I don't want that. I just want the row and column of the smallest. This is the code that finds the sum of each row and column as opposed to only the one that I want. Do you have any advice or hints? thanks in advance

1
2
3
4
5
6
7
8
9
	int totalrow = 0;
	for (int i = 0; i<r; i++)
	{
		for (int j = 0; j<c; j++)
			totalrow = totalrow + cool[i][j];
			cout << "total of row " << i << " is " << totalrow << endl;
			totalrow = 0;
	}
	
Last edited on
Line 4: You need to save off i and j in addition to the lowest value.
1
2
3
4
5
6
7
int low_row = 0;
int low_col = 0;
if (low > cool[i][j]) 
  {  low = cool[i][j];
      low_row = il
      low_col = j;
  }

Then in lines 10-16, you need only a single loop to add row low_row.
Likewise in lines 17-24, you need only a single loop to add column low_col.

line 17-24, You should be using totalcol, not total row.
thanks for quick reply i changed it to
1
2
3
4
5
6
7
for (int i = low_row; i<r; i++)
			{
				int totalrow = 0;
				totalrow = low + low_row;
				cout << "total of row " << i << " is " << totalrow << endl;
				totalrow = 0;
			}

is that what you meant? it doesnt seem to be working
No, that is not what I meant.

1
2
3
4
5
int totalrow = 0;  // Needs to be initialized before the loop.  Not in the loop
for (int col = 0; col<C; col++)  // Changed c to upper case to indicate # columns constant
{  totalrow += cool[low_row][col];   
}
cout << "total of row " << low_row << " is " << totalrow << endl;


Last edited on
Topic archived. No new replies allowed.