Reading a two dimensional array with two wile loops

Hi there it seems that these two while loops can't read my 2D array. What's causing it, I know it's my faulty programming but what exactly?

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
#include <iostream>

using namespace std;

int main () {

    int y = 0;
    int x = 0;

    int someArray[2][3] = {{2,3,4},{5,6,9}}; 


    while(x < 2) {

        while ( y < 3){

        cout << someArray[x][y] << "  ";
        y++;

        }

        x++;
        cout << "\n" << endl;

    }

    return 0;
}
Last edited on
On your second iteration of the outer loop, you never reset y to zero. Therefore the inner loop only executes once.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main () 
{   int y = 0;
    int x = 0;
    int someArray[2][3] = {{2,3,4},{5,6,9}}; 

    while(x < 2) 
    {   y = 0;  //  y needs to be reset on each iteration
        while ( y < 3)
        {   cout << someArray[x][y] << "  ";
            y++;
        }
        x++;
        cout << "\n" << endl;
    }
    system ("pause");
    return 0;
}
2  3  4

5  6  9

Press any key to continue . . .

Last edited on
Thanks man, I knew it was simple and obvious but I kinda assumed every time a loop finishes completely it takes the value from the local scope where it is defined which in this case is zero. So guess I was wrong.

Maybe it would be smart to write a small function to check that, like this:

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
#include <iostream>

using namespace std;

int main () {

    int y = 0;
    int x = 0;

    int someArray[2][3] = {{2,3,4},{5,6,9}}; // [1st] is the row, [2nd] is the column


    while(x < 2) {

        y = 0;

        while ( y < 3){

        cout << someArray[x][y] << "  ";
        y++;

        }

        x++;
        cout << "Value of Y after while loop ends: " << y << endl;
        cout << "\n" << endl;

    }

    return 0;

}
Last edited on
I kinda assumed every time a loop finishes completely it takes the value from the local scope where it is defined

Nope.

You may be thinking for a for loop where there is an explicit initialization term.
1
2
    for (int x=0; x<2; x++)
             ^^^ - initialization term

Last edited on
No, not that, luckily I'm familiar with for loops and how they function unlike the while loops.

I explain stuff terribly, when I said local scope I actually meant the int y = 0; beneath int main (). And why would I think about for loops? I couldn't possibly be that dumb to confuse it with while loops, where else does the while loop have initialization of it's variable then on the start of the int main? That's what I was thinking about. Sorry for ranting here, but this really grinds my gears.

Ohhh and thanks for the help again.
Last edited on
while loops have no explicit initialization. A for loop is the preferred idiom where you want to execute the loop for a fixed number of iterations.
Ok, thanks again.
Topic archived. No new replies allowed.