Multi dimensional arrays

If I put in this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int place[10][10] = {{0,1,2,3,4,5,6,7,8,9},{9,8,7,6,5,4,3,2,1,0}};
int i;

int main(){
    for(i=0; i<10; i++){
        cout << i << endl;
        cout << place[i][i] << endl;
        cout << place[i][i] << endl;
        cout << "\n" << endl;
    }
}


I get:


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


When I want to get:


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


So how would I correctly define the array? Also it returns no errors and it was compiled with GCC.
As you know, your array is wrong. You say it is 10x10, but you only give it 2 rows so the rest of the rows are uninitialized.
The problem isn't with the array definition, it is will your for loop. You need three for loops. One to loop through the index and print that on one line, one to loop through the 0 index and then another to loop through the 1 index. Or you could do one loop through the index value, then one outer loop through the 0 & 1 outer index, followed by an inner loop for the 10 inner elements.
Oh ok thanks.
The problem isn't with the array definition, it is will your for loop. You need three for loops. One to loop through the index and print that on one line, one to loop through the 0 index and then another to loop through the 1 index. Or you could do one loop through the index value, then one outer loop through the 0 & 1 outer index, followed by an inner loop for the 10 inner elements.


Actually the problem was the Array definition and I don't need 3 for loops

1
2
3
4
5
6
7
8
9
10
11
int place[2][10] = {{0,1,2,3,4,5,6,7,8,9},{9,8,7,6,5,4,3,2,1,0}};
int i;

int main(){
    for(i=0; i<10; i++){
        cout << i << endl;
        cout << place[0][i] << endl;
        cout << place[1][i] << endl;
        cout << "\n" << endl;
    }
}


That ^: sends the correct output.
Topic archived. No new replies allowed.