The biggest sum in a row (arrays)

Why do i get an incorrect result?

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
int z[5][4];


	srand(static_cast<unsigned int>(time(0)));

	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			z[i][j] = rand() % 10 + 1;
			cout << setw(3) << z[i][j];
		}
		cout << endl;
	}

	int sum = 0; int max = -1;
	int row;
	for (int i = 0; i < 5; i++)
	{
		sum = 0;

		for (int j = 0; j < 4; j++)
		{
			sum = sum + z[i][j];
		}
		if (sum>max)
		{
			max = sum;
			row = i;
		}


	}

	cout << "The biggest sum is " << sum << " the row is " << row << endl;

Last edited on
Line 35:
1
2
// cout << "The biggest sum is " << sum << " the row is " << row << endl;
std::cout << "The biggest sum is " << max << " the row is " << row << '\n' ;
hahha I really need to go to sleep i can't believe it :D
Topic archived. No new replies allowed.