Can someone give me a hint on how to add the values inside a 2-dimensional arrays?

I have an assignment that requires me to add the values inside a two dimensional array. If someone can give me a hint on how to do so I would really appreciate it. So far, the code below is all I have managed for now.


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


#include <iostream>

using namespace std;

int main ()

{

 int values[20][10] = {1,1,1,1,1,1,1,1,1,1,     1,1,1,1,1,1,1,1,1,1,   1,1,1,1,1,1,1,1,1,1,    1,1,1,1,1,1,1,1,1,1};
 int total = 0;



for (int n = 0; n<= 10; n++)

       for (int m = 0 ; m <= 20; m++)
        {
        total += values[m][n];
        }


cout << total;

return 0;



}






p.s Please don't show me any code, I just want a hint on how to add the values
Last edited on
What's wrong with what you have?

Edit: You might have to switch the [10] and [20]. Hmmm, maybe not though... that code should just iterate through the first "column" of each "row", then move on to the next column. Should work as far as I can tell.
Last edited on
Yes, i forgot to mention I want to accumulate the values into one variable. But I think I'm having more trouble using the correct formula inside the nested loops

Edit: Everytime I try to compile it in code blocks it would give me a 6-digit negative number. It's not showing the expected output, which is just adding the 1's.
Last edited on
I believe you are initializing it improperly.

Check out this for more info on how to brace initialize 2d (or more!) arrays. :)

http://www.learncpp.com/cpp-tutorial/65-multidimensional-arrays/
You were right, I did initialize it improperly. It was probably because the array I declared was pretty big and I didn't put the time and effort to store all the values for each index of the array.

I even reduced the size of the array to [2] [2] and the numbers seemed to have added properly.

One more question. Can you give me another hint on accumulating the values in each dimension individually in a 2-dimensional array? Here is what I tried so far..

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 [29][5];
    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;





For some reason it just won't add the dimensions individually, can you give me any hints?
Last edited on
If you have a 2D array, you'd want to access it in a 2D way.
As in,
total += days[m][n];
Hint: a for loop inside a for loop.

Edit: Oh, it looks like you were doing something like that in your first post, why did you change it?
1
2
3
4
5
6
for (int n = 0; n<= 10; n++)

       for (int m = 0 ; m <= 20; m++)
        {
        total += values[m][n];
        }

A note though: You do not want <=, you would just want < here.
values[20][10] is NOT within the bounds of the array.
Likewise, days[29][5] is not within the bounds of your latest post's array.

Also, you never initialize the values for your array in post above me.
Last edited on
I'm just experimenting with the code I wrote so I can understand how to properly use arrays, which is the reason why I changed it.

Now the only thing I want to know now is how i can access one dimension in a 2-dimension array without accessing the other. And accumulate the values in one dimension without touching the other.

and yes you're right, I forgot to initialize the values for the array.

Here is my updated code


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];
        

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


cout << total1;
cout << total;

Last edited on
int days [2][2] = {1,1, 1,1};

Unfortunately, that's still incorrect. You need a separate brace set for each additional array element in the 2nd dimension so it should look like this:

in days [2][2] = {1,1},{1,1}

Basically the array can be thought of like this:

1
2
[0,0][0,1]
[1,0][1,1]


where the number correspond to that elements location within the array. The first brace set initializes the values in 0,0 and 0,1, and the 2nd set initializes the values in 1,0 and 1,1. This is why the 2 sets have to each have their own matching pair of braces and be comma separated. (Well, actually it's because the standard calls for it to be done that way , but ya. :p)
Last edited on
Yes I know now haha, it's just that I was busy trying to experiment with the code that I completely forgot to initialize the array with the proper braces. But thank you, I finally understand quite a bit about arrays.
Topic archived. No new replies allowed.