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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cstdlib>
#include <ctime>
#include <iostream>

}

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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<rowsize; ++row)
{
    rowsum = 0;
    for (int col=0; col<colsize; ++col)
    {
        value = rand() % 100 + 1;
        Array[row][col] = value;
        rowsum += value;
    }
}



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

Hello cidex,

You could try something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>


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.