### Highest sum of array row

Hi,
I have created this code that makes an 10x10 array grid and fills it with random numbers from 1 to 99. I also add up every row numbers together and see what row has the highest sum.

 1234567891011121314151617181920212223242526272829 #include #include #include } int main() { std::srand((unsigned)time(NULL)); int Array[10][10]; int value = 0; for (int i = 0; i < 10; ++i) { for (int j = 0; j < 10; ++j) { Array[i][j] = rand() % 100 + 1; std::cout << Array[i][j] << ' '; value += Array[i][j]; } std::cout << std::endl; value = 0; } system("pause"); }

So my problem is how I output the rows nr which has the highest sum?

For exaple like this:
1 2 1 //=> sum 4
1 3 3 //=> sum 7
2 2 1 //=> sum 5
The largest sum has row nr 2
You are using value to track the highest sum of each row.
You need a max_value variable, to track the highest value you find.

Good luck!
To not get lost about rows and columns, it might help you to rename "i" to "row" , "j" to "col". Similarly, avoid using 10 everywhere and instead change it to variables to show row and column sizes. This way, you can change your matrix dimensions in one place.

Here is some code to track sum of rows, but it's still missing logic for the highest one. I'll leave that to you.

 123456789101112131415161718 int rowsize = 10; int colsize = 10; int Array[rowsize][colsize]; int value; int rowsum; int highest_rowsum; // Figure out where to insert logic to track the *highest* sum for (int row=0; row

@Duthomhas -- he wants highest row sum, not highest individual cell value.

Hello cidex,

You could try something like this:

 12345678910111213141516171819202122232425262728293031323334353637383940 #include #include #include #include int main() { std::srand((unsigned)time(NULL)); int Array[10][10]; int value = 0; int maxRow{}; int maxValue{}; for (int i = 0; i < 10; ++i) { std::cout << std::setw(3) << i + 1 << ". "; for (int j = 0; j < 10; ++j) { Array[i][j] = rand() % 100 + 1; std::cout << std::setw(3) << Array[i][j] << ' '; value += Array[i][j]; if (value > maxValue) { maxValue = value; maxRow = i; } } std::cout << std::setw(7) << value << std::endl; value = 0; } std::cout << "\n The largest row is: " << maxRow + 1 << '\n' << std::endl; system("pause"); }

Hope that helps,

Andy
@icy1
I know what he wants.

Notice how he uses value to compute the sum of each row.
Convenient, no?
 So my problem is how I output the rows nr which has the highest sum?

Is this your first "find max" problem, or have you previously looked at a "you get 10 numbers, which one was largest"?
Topic archived. No new replies allowed.