Please explain my output

So I clearly don't understand how arrays are working.

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>
using namespace std;
/*
Write a program that creates a 3x3 matrix and then prints to 
the screen the transpose of the matrix. ex:
     1 2 3    1 4 7
     4 5 6    2 5 8 
     7 8 9    3 6 9
*/
int main (void)
{
int i = 0;
int j = 0;
int myarray [i][j];
	for (i = 1; i <= 7 ; i+=3)
	{
		for (j = i+1; j <= i+2 ; j++)
		{
		cout << myarray [i][j] << "\t";
		}
		cout << endl;
	}
return 1;
}

Which returns:

9	9	
32767	1606420320	
0	0	


I don't even know what these numbers mean.

You need to assign values to your array first. Right now, your array is filled with random numbers.
They values printed are just random data that happened to be in memory.

Your program is invalid.
These lines are a problem:
1
2
3
int i = 0;
int j = 0;
int myarray [i][j];

First, i and j need to be constant. Second, the array needs to be 3x3, not 0x0.
1
2
3
const int rows = 3;
const int cols = 3;
int myarray [rows][cols];


This will at least give you a valid array with 9 cells.

But there's no point trying to print it out until you have assigned values to all the elements.
Last edited on
So I tried 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
26
27
28
29
30
#include <iostream>
using namespace std;
/*
Write a program that creates a 3x3 matrix and then prints to 
the screen the transpose of the matrix. ex:
     1 2 3    1 4 7
     4 5 6    2 5 8 
     7 8 9    3 6 9
*/
int main (void)
{
int i = 0;
int j = 0;
int myarray [3][3] = {
						{1,2,3},
						{4,5,6},
						{7,8,9},
					};
	for (i = 1; i <= 7 ; i+=3)
	{
	//cout << i << "\t";
		for (j = i+1; j <= i+2 ; j++)
		{
		//cout << myarray [i][j] << "\t";
		}
		//cout << endl;
	}
			cout << myarray[3][3] << endl;
return 1;
}

Which gives:


1606417048
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
int main (void)
{
    const int rows = 3;
    const int cols = 3;

    int myarray [rows][cols] =
    {
        {1,2,3},
        {4,5,6},
        {7,8,9},
    };

    int i;
    int j;

    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            cout << myarray [i][j] << "\t";
        }
        cout << endl;
    }

    return 0;
}
Last edited on
It does work, and I thank thee, but I'd rather understand why my code is putting out what it is, instead of cheating my way to the end =)

Also, I would like to populate the array with just the loop, instead of defining it like

1
2
3
4
5
6
int myarray [rows][cols] =
    {
        {1,2,3},
        {4,5,6},
        {7,8,9},
    };


Can this be done?
Last edited on
Well, your last version had only one output statement:
cout << myarray[3][3] << endl;
The valid subscripts are 0, 1 and 2 in both dimensions.
[3][3] is outside your array, again it contains random data.

For similar reasons, this could only give incorrect results, if 3 is outside, then where is 7?

for (i = 1; i <= 7 ; i+=3)
Last edited on
Ah. Please delete this thread, to hide my shame.
Yes, you can use cin to fill the array, just as you use cout to display it.

Don't worry about making mistakes. We've all done it. In fact it's the only way to learn. You discover more of how things work that way.
Last edited on
for (i = 1; i <= 7 ; i+=3)


Was because I started by building two nested loops that would display the pattern I wanted. I thought there might be a way to print them into the matrix if I started there.
Ah, I see. Actually, you could use a data file, or better, a stringstream. Then create (by any method you wish) the values you want in the stringstream (or file). Lastly, use a loop to read the values back from the stream into your array.
Ok:
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
#include <iostream>
#define SIZE 3
using namespace std;

/*
Write a program that creates a 3x3 matrix and then prints to 
the screen the transpose of the matrix. ex:
     1 2 3    1 4 7
     4 5 6    2 5 8 
     7 8 9    3 6 9
*/

void Transpose (int myarray[][SIZE], int size)
{
	for (int x = 0; x < SIZE ; x++)
		{
		for (int y = 0; y < SIZE ; y++)
			{
			cout << myarray [y][x] << "\t";
			}
			cout << endl;
		}
	return;
}

int main (void)
{
int i = 0;
int j = 0;
int myarray [SIZE][SIZE] = {
						{1,2,3},
						{4,5,6},
						{7,8,9},
					};
	for (i = 0; i < SIZE ; i++)
	{
		for (j = 0; j < SIZE ; j++)
		{
		cout << myarray [i][j] << "\t";
		}
		cout << endl;
	}
	cout << "Flipadelphia" << endl;
	Transpose (myarray, SIZE);
	
return 1;
}




1	2	3	
4	5	6	
7	8	9	
Flipadelphia
1	4	7	
2	5	8	
3	6	9	


But I mostly copied and pasted from my reference.

I do not understand
 
void Transpose (int myarray[][SIZE], int size)


Why is there nothing in the first bracket? Why the ", int size" afterwards? What information is this giving to the compiler?

What does the preprocessor directive "#define SIZE 3." do? Is this the same a global variable? Why not just put it in the header then?
void Transpose (int myarray[][SIZE], int size)
When you pass a one-dimensional array to a function, the compiler just passes the address. But for multidimensional arrays (two or more dimensions), the compiler needs to know the size of all but the first dimension. Otherwise, it wouldn't know how long each row is, and wouldn't be able to calculate the correct address corresponding to a given element.

What does the preprocessor directive "#define SIZE 3." do? Is this the same a global variable? Why not just put it in the header then?

It's the same as using this const int SIZE = 3; but the latter is to be preferred as the compiler can do type checking, whereas #define simply replaces the text "SIZE" with the text "3" wherever it occurs. Perhaps not always, but often #define might be considered the old, 'C' way of doing things, and the use of const is C++.

As for it being global, well it needs to be available not just in main(), but also in transpose().
Last edited on
Thank you!
Topic archived. No new replies allowed.