Not sure how loops iterate over multidimensional arrays.

Hello, I'm learning c++ and an assignment was to write a program that adds the elements in multidimensional arrays. I have completed the program but I'm not exactly sure how the for loops iterate over the arrays and their elements. If someone could explain I would be most thankful because I don't like moving on until I fully understand how something works.

Here is the program:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>

using namespace std;
#define space cout << endl;

int main()
{
	//Define A
	int multiA[2][3] = 
	{
		{ -5, 2, 8 },
		{ 1, 0, 0 }
	};

	//Define B
	int multiB[2][3] = 
	{
		{ 1, 0, 2 },
		{ 0, 3, -6 }
	};

	//Define C, the empy one to store sum of A and B
	int multiC[2][3];
	


	//Assigns the sum of A and B to C
	for (int i = 0; i < 2; ++i)
	{
		for (int j = 0; j < 3; ++j)
		{
		    multiC[i][j] = multiA[i][j] + multiB[i][j];
		}
	}



	//Shows A
	cout << "A = " << endl;
	for (int i = 0; i < 2; ++i)
	{
		for (int j = 0; j < 3; ++j)
		{
			cout << multiA[i][j] << " ";
		}
		space
	}
	space


	//shows B
	cout << "B = " << endl;
	for (int i = 0; i < 2; ++i)
	{
		for (int j = 0; j < 3; ++j)
		{
			cout << multiB[i][j]<< " ";
		}
		space
	}
	space


	//shows C which is the sum of a and b
	cout << "A + B = " << endl;
	for (int i = 0; i < 2; ++i)
	{
		for (int j = 0; j < 3; ++j)
		{
			cout << multiC[i][j] << " ";
		}
		space
	}
	space

	system("Pause");
}
When dealing with 2D arrays, remember that the first coordinate is vertical, followed by the horizontal, and not the reverse as we tend to think of it.
Here's a simple example:
1
2
3
4
5
6
7
8
9
10
11
    int arrayA[2][3]={{3,1,9},{2,4,6}};
    int arrayB[2][3]={{7,0,5},{8,3,7}};
    int arrayC[2][3]={};//initialized to zeroes
    for (int y=0;y<2;y++)
    {       //outer loop iterates vertically, executing inner loop repeatedly
        for (int x=0;x<3;x++)
        {   //inner loop goes horizontally, totaling values, storing and outputting
            arrayC[y][x]=arrayA[y][x]+arrayB[y][x];
            cout<<arrayC[y][x]<<" ";//put spaces between totals
        }
    }
10 1 14 10 7 13

Although, looking at your code, it appears you used i and j correctly. I just find x and y more intuitive, I suppose. What kind of output are you getting? Or, is it more about just understanding the logic? Basically, when structured like this, it scans from left to right through each line before moving to the next, just like you reading this post.
Last edited on
1
2
3
4
for (int j = 0; j < 4; ++j)
  {
    std::cout << j << std::endl;
  }

This loop executes the line 3 for four times. Each time the value of j is different. We could unroll the loop:
1
2
3
4
5
6
7
8
int j = 0;
std::cout << j << std::endl;
++j;
std::cout << j << std::endl;
++j;
std::cout << j << std::endl;
++j;
std::cout << j << std::endl;

You do use j as an index to an array to access an element of the array.
I've figured it out now.
My program works correctly, I was just not understanding why in the loop it outputs the right numbers instead of what j is during the loop. For example: in the program the loop that shows A is:

1
2
3
4
5
6
7
8
9
10
11
        //Shows A
	cout << "A = " << endl;
	for (int i = 0; i < 2; ++i)
	{
		for (int j = 0; j < 3; ++j)
		{
			cout << multiA[i][j] << " ";
		}
		space
	}
	space


It outputs:

1
2
-5, 2, 8
1, 0, 0


I was wondering why it didn't show what j was assigned instead which would be 0, 1, 2.
Now I realize i and j isn't just for the loop but part of the array kind of, so during the first iteration of both loops it's the same as:

 
cout << multiA[0][0];


Not sure while I didn't understand that earlier, I feel stupid now....

There is also another program, I couldn't figure it out but now I'm more confused after looking at the answer than I was before. The program is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main()
{
	unsigned char myChar;
	int count = 0;

	for (int i = 33; i <= 255; ++i)
	{
		if (count % 10 == 0)
		{
			cout << endl;
		}

		myChar = i;
		cout << i << ": " << myChar << " ";
		++count;
	}

	system("Pause");
}


I have no idea how this ends up showing ASCII symbols. Especially because:

1
2
               myChar = i;
		cout << i << ": " << myChar << " ";


During that part it assigns i to myChar and then during the output statement i outputs the number it is currently at and myChar shows an ASCII. Instead of the ASCII why doesn't it output the number that i outputted because:

 
myChar = i;
Type. myChar has a character type. The print function is made to write a value as "number" if its type is integer, but as a "symbol" if the type is character. The latter looks up symbols from the ASCII table.

Look at http://www.cplusplus.com/reference/ios/hex/
See how even the conversion from value to "number" has more than one way.

Type. myChar has a character type. The print function is made to write a value as "number" if its type is integer, but as a "symbol" if the type is character. The latter looks up symbols from the ASCII table.

Look at http://www.cplusplus.com/reference/ios/hex/
See how even the conversion from value to "number" has more than one way.


Alright thanks. Would've been nice if one of my classes mentioned that before I was told to write a program with it.

I have a feeling I'll be making a lot more posts on here before I've learned C++.
Topic archived. No new replies allowed.