How can I accumulate the values in only ONE dimension of a 2-dimensional array?

How can I accumulate the values in one dimension of a 2-d array, without accumulating the values from both dimensions? I know that in order to access a 2-d array i need to type myarray[2][2]. But is there a way of accessing ONE dimension and accumulating the values for just that dimension only?

Can anyone give me a hint on doing that?

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
#include <iostream>

using namespace std;

int main ()

{

    int days [2][2] = {1,1,    1,1};
    int total = 0; //accumulator for first dimension
    int total1= 0; //accumulator for second dimension

        for (int m = 0; m <= 29; m++)
        
          total += days[m];
        

        for (int n = 0; n <= 5; n++)
        
            total1 += days[n];
        


cout << total1;
cout << total;
Last edited on
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
#include <iostream>

using namespace std;

int main ()

{

    int days [2][2] = {1,1,    1,1};
    int total = 0; //accumulator for first dimension
    int total1= 0; //accumulator for second dimension

        for (int m = 0; m <= 29; m++)
        
          total += days[m][0];
        //total += days[0][m];
        //total += days[m][1]
        //total += days[m][2]

        for (int n = 0; n <= 5; n++)
        
            total1 += days[n][0];
        // total1 += days[0][n];


cout << total1;
cout << total;
Last edited on
I was kind of hoping for just a hint and a explanation on how accumulate in one dimension but thank you anyway.
It was a hint I didn't give you the answers...try out the different combinations see what happens :D haha
Topic archived. No new replies allowed.