#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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;
}
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?
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.
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 constint 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().