1. ## 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  and . Here's my code:
Code:
```#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
int arr  ;
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.  2. How would you do it on paper? 3. Well, this:
Code:
```sum = arr + arr ;
cout << sum <<" ";```
would give you your first sum of 7, and:
Code:
```sum = arr + arr;
cout << sum <<" ";```
would give you your second sum of 9.

Just need to figure out a way to implement that into a loop. 4. Originally Posted by setleaf Well, this:
Code:
```sum = arr + arr ;
cout << sum <<" ";```
would give you your first sum of 7, and:
wrong. it should be:

Code:
```sum = arr + arr ;
cout << sum <<" ";``` Originally Posted by setleaf Code:
```sum = arr + arr;
cout << sum <<" ";```
would give you your second sum of 9.
This one is correct. 5. Whoops, good call Elkvis. I haven't had my coffee yet. 6. In addition, you should really be defining constants for the size of your array and loop iterations.

Code:
```#include <iostream>
#include <conio.h>
using namespace std;

const int rows = 2;
const int columns = 5;

int main ()
{
int arr [rows] [columns];
int val = 1;

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
//etc...```
It might also be a good idea to change i and j to row and column, respectively. Descriptive variable names are to be preferred over single-letters. 7. Here's my final code and it works. Can't do it without your help guys, thanks. Code:
```#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
int arr  ;
int val = 1;

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

int sum = {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;

getch ();
return 0;
}``` 8. Good to hear that it works for you, but you need to work on your code formatting, particularly your indentation. For example:
Code:
```#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
int arr;
int val = 1;

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

int sum = {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;

getch();
return 0;
}```
Keep in mind that <conio.h> and getch are non-standard, and that you do not actually need them here since you can either run your program from a separate command prompt window, configure your IDE to pause your program before it exits, or use standard facilities to pause your program before it exits.

Also, instead of using the magic numbers 2 and 5 all over the place, you should have defined named constants. 9. @laserlight : I see, thanks dude. Sorry for being such a newbie. :3 Popular pages Recent additions elements, sum, two-dimensional array 