Adding-up Elements of a Two-Dimensional Array

Good day guys, I would just like to share my code and wanted to do something about it. This code shows a loop inside a loop initializing
a two-dimensional array named arr with elements [2] and [5]. Here's my code:
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
int arr [2] [5];
int val = 1;


for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
arr [i][j] = val;
cout << arr [i][j] << " ";
val++;
}
cout << endl;
}



getch ();
return 0;
}

Now, what I wanted to do is to have an output showing the sums of each elements. Example, the above code has an output of:
1 2 3 4 5
6 7 8 9 10
I wanted to have more codes which would add up the elements 1 + 6, 2 + 7, 3 + 8, 4 + 9, 5 + 10 and produce and output:
7 9 11 13 15

Hope anyone would let me lend me their precious time and expertise for such matter. Thanks. :)

Hey, this is quite a straightforward program.
1
2
3
4
Int Sum[5];
For (int j=0; j <5; j++)
 For (int i=0; i <2; i++)
  Sum [j]+=arr [i][j];

And then just display it. :)
@Pratik k

You never initialised Sum, so what would happen when you do Sum[j] +=arr[i][j] is anybody's guess.

Also, what are "Int" and "For" - I'm assuming your editor capitalised "int" and "for" automatically.

Building on that though:
1
2
3
4
5
6
7
8
9
10
11
	int sum[5] = {0};
	for (int j=0; j <5; j++)
	{
		for (int i=0; i <2; i++)
		 {
			 sum [j]+=arr [i][j];
		 }
	}
	
	for (int j=0; j <5; j++) cout << sum[j] << " ";
	cout << endl;



@tipaye
Yeah they got captialized. And you're absolutely right about the initialisation part, but I assumed he'd do it on his own. It's good programming to always initialize the variable arrays to 0. However, as opposed to charcater arrays I really doubt it would pose a problem.
@Pratik K

It is a big problem. Your best case scenario is that the variable will have random data in it, and give you incorrect sums.

Try compiling and running the program.

Uninitialsed variables are such a big problem, that we have language features, patterns and idioms devoted to the problem.
Reading both of your arguments made me laugh. Hahaha. But thanks, it did help me. :)
Topic archived. No new replies allowed.