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. :)

You are almost there. Do the code and show us how you might do it. Basically you are adding the values of arr[0][N] and arr[1][N] where N is 0...4.

Solved it. 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;
            val++;
            }
            }

    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;



            getch ();
            return 0;
            }

it's code

//main

int arr[10] = {0};
int sum[5] = {0};
int i;

for ( i=0; i< ( sizeof(arr) / sizeof(int) ) / 2; i++){
    arr[i] = i+1;
    arr[ ( ( sizeof(arr) / sizeof(int) ) / 2 ) + i ] = ( ( sizeof(arr) / sizeof(int) ) / 2 ) + i + 1;
    sum[i] = arr[i] + arr[ ( ( sizeof(arr) / sizeof(int) ) / 2 ) + i ];

    cout << sum[i] << " ";
}
cout << endl;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.