Reverse diagonal in two-dimensional array

Hi guys,

I can't solve this problem.
Task : I'm given a 2d array and my goal is to make a program that will sum every element above reverse diagonal. Baseline data:
1 2 3
4 5 6
7 8 9

The program should sum 1 + 2 + 4 = 7

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Then program should output 1 + 2 + 3 + 5 + 6 + 9 = 26

This is what I have:

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
#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
	int n, m, x[10][10];

	double v[10];

	cout << "Input the size of 2d array\n";

	cin >> n;

	m = n;

	cout << "Input the elements of array\n";

	for(int i=0; i<n; i++)
	{
		for(int j=0; j<m; j++)
		{
			cin >> x[i][j];
		}
	}
	cout << "Your 2d array: \n";

	for(int i=0; i<n; i++)
	{
		for(int j=0; j<m; j++)

		cout << setw(5) << x[i][j];

		cout << endl;
	}

	return 0;
}


And the whole trick is that algorithm should calculate various sizes of array. 2x2, 3x3, 4x4, 5x5 and so on.
@Torex

All you had to do, was subtract 1 from the second set of for loops, and keep subtracting 1 more from inside the loop. You should also check that the user doesn't enter 10 or more as the size of the 2d array, else you'll crash the program.

I'll let you figure out how to output 1 + 2 + 3 + 5 + 6 + 9 = 26, instead of just the sum, as I have it.

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
41
42
43
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int n, m, x[10][10],sum=0;

	double v[10];

	cout << "Input the size of 2d array\n";

	cin >> n;

	m = n;

	cout << "Input the elements of array\n";

	for (int i = 0; i<n; i++)
	{
		for (int j = 0; j<m; j++)
		{
			cin >> x[i][j];
		}
	}
	m--;
	n--;
	cout << "Your 2d array: \n";

	for (int i = 0; i<n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << setw(5) << x[i][j];
			sum += x[i][j];
		}
		cout << endl;
		m--;
	}
	cout << endl << "Sum = " << sum << endl;
	return 0;
}
@whitenite1

Thank's a lot. I was thinking the right way, but the problem was that I was decreasing m and n not in the right place.

And for curiosity, what is the easiest way to sum elements by columns?

For example:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Answer should be something like this:

1st column = 1 + 5 + 9 = 15
2nd column = 2 + 6 = 8
3rd column = 3

Am I thinking right?
sum1 += x[i]
sum2 += j[i]
Last edited on
@Torex

No, sorry to say, you're not thinking right. For one, you haven't got a j array. Plus that would be very cumbersome, using separate variables for each column. You already specified an array named v, so we'll use that. All of the arrays need to be zeroed out first when created.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <iomanip>

using std::cin;
using std::cout;
using std::endl;
using std::setw;

// using namespace isn't a completely safe thing to do,
// so I set up just the ones being used.

int main()
{
	int n, m, x[10][10] = { { 0 }, { 0 } }, sum = 0, i, j;
	int v[10] = { 0 };
	// Zero out sum, the x array and v array.
	// Initialized i and j so we don't need show int in the for loops. 
	cout << "Input the size of 2d array\n";

	cin >> n;

	m = n;

	cout << "Input the elements of array\n";

	for (i = 0; i<n; i++)
	{
		for (j = 0; j<m; j++)
		{
			cin >> x[i][j];
		}
	}
	m--;
	n--;
	cout << "Your 2d array: \n";

	for (i = 0; i<n; i++)
	{
		for (j = 0; j < m; j++)
		{
			v[j] += x[i][j]; // Adding up the columns 
			cout << setw(5) << x[i][j];
			sum += x[i][j];
		}
		cout << endl;
		m--;
		
	}
	cout << endl << "Sum = " << sum << endl;
	for (i = 0; i < n; i++)
		cout << "Column " << i << " sum = " << v[i] << endl;
	return 0;
}
Thank's for a solution. However I noticed a small error. If I input negative numbers, the answer gives sum of 0. How can I solve this problem? It should cout the addition up the columns is not possible. And maybe you can explain how do m-- and n-- works in this program?
@Torex

During the addition of the columns, you check if its a 0 or more. If less than 0, end the for loops, and inform the user that the program has terminated.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <iomanip>

using std::cin;
using std::cout;
using std::endl;
using std::setw;

int main()
{
	int n, m, x[10][10] = { { 0 }, { 0 } }, sum = 0, i, j;
	int v[10] = { 0 };
	// Zero outsum, x array and v array.
	// Initialized i and j so we don't need show int in the for loops. 
	bool ok = true;
	cout << "Input the size of 2d array\n";

	cin >> n;

	m = n;

	cout << "Input the elements of array\n";

	for (i = 0; i<n; i++)
	{
		for (j = 0; j<m; j++)
		{
			cin >> x[i][j];
		}
	}
	m--;
	n--;
	cout << "Your 2d array: \n";

	for (i = 0; i<n; i++)
	{
		for (j = 0; j < m; j++)
		{
			if (x[i][j] >= 0)
			{
				v[j] += x[i][j];
				cout << setw(5) << x[i][j];
				sum += x[i][j];
			}
			else
			{
				cout << endl << endl << "Sorry, a negative number has been inputted. Program cannot continue." << endl;
				i = n; // Ends the for loops
				ok = false; // Negative number found. Set to false
			}
		}
		cout << endl;
		m--;
	}
	if (ok) // Print only IF ok is still true
	{
		cout << endl << "Sum = " << sum << endl;
		for (i = 0; i < n; i++)
			cout << "Column " << i << " sum = " << v[i] << endl;
	}
	return 0;
}


You asked..
And maybe you can explain how do m-- and n-- works in this program?


Well, you're trying to figure a triangle of numbers in an array. If the user says the array is sized as 4, the inputted numbers are array[0][0], array[0][1], array[0][2] and array[0][3] for the top, which is kept track of, by variable m. N is for the array rows. 1 is subtracted from the rows count by the n--. Anyway, by subtracting 1, from m and n, the for loop counts only array[0][0] to array[0][2]. Then the second time thru the loop, because m is subtracted by 1 again, it only counts array[1][0] and array[1][1], and the last loop only counts array[2][0], because , again, 1 is subtracted from m. When finished, you have the numbers added from the triangle of numbers in the array.

Hope this clarifies everything. If not, ask the next question.
Last edited on
Topic archived. No new replies allowed.