2D array initializing problems

Simple homework problem.. I am having a problem with the second bridge for mouse island I commented the problem.

oh and I know I have my x and y backwards...

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

using namespace std;

main(){

int xsize,ysize = 0;
// int tempnum = 0;

cout<<"Enter X size of island: ";
cin>>xsize;
xsize = xsize - 1;
cout<<endl<<"Enter Y size of island: ";
cin>>ysize;
ysize = ysize - 1;

bool mouseisland[xsize][ysize];

for (int counter1=0; counter1<=xsize; counter1++){
    for (int counter2=0; counter2<=ysize; counter2++){
        if ((counter1==0) || (counter2==0))
            mouseisland[counter1][counter2] = 0;
        else if ((counter2 == ysize))
            mouseisland[counter1][counter2] = 0;
        else if ((counter1 == xsize))
            mouseisland[counter1][counter2] = 0;
        //else if ((counter1 == xsize - 2) && (counter2 == ysize))
        //     mouseisland[counter1][counter2] = 1;
        else mouseisland[counter1][counter2] = 1;
    }
}

//// ************************ THIS IS DRIVING ME CRAZY!!!!
//// using a 10 X 10


mouseisland[xsize][1] = 1;       // <--- this part works fine
mouseisland[xsize-2][ysize] = 1; //why does this make [xsize-2][ysize] AND [xsize-1][0] TRUE?!?!?!?!!?!?!?
//mouseisland[xsize-1][0]=0;

/////DRIVING ME MAD!!!



for (int counter1=0; counter1 <= xsize; counter1++){
    for (int counter2=0; counter2<=ysize; counter2++){
        cout<<mouseisland[counter1][counter2];

    }
    cout<<endl;

}




cout<<"hello cruel world";
return 0;
}
why does this make [xsize-2][ysize] AND [xsize-1][0] TRUE?

I think because you equal line 38 to 1. Not sure about that but 1 is supposed to be true and 0 false in boolean language. Can help more if I knew what you were after.
its basically making a board

this is what i am aiming for :

0000000000
0111111110
0111111110
0111111110
0111111110
0111111110
0111111110
0111111111
0111111110
0100000000

this is what i am getting :

0000000000
0111111110
0111111110
0111111110
0111111110
0111111110
0111111110
0111111111
1111111110
0100000000

i have no idea what you want to do there, but if you set up an array
bool mouseisland[xsize][ysize];

you cant use
1
2
3
for (int counter1=0; counter1 <= xsize; counter1++){
    for (int counter2=0; counter2<=ysize; counter2++){
        cout<<mouseisland[counter1][counter2];


it has to be counter < size
darkmaster that works fine, I'm having trouble changing two locations in the array to TRUE.

mouseisland[xsize][1] = 1; // <--- this part works fine
mouseisland[xsize-2][ysize] = 1; //why does this make [xsize-2][ysize] AND [xsize-1][0] TRUE?!?!?!?!!?!?!?

is giving me trouble not outputting the array.
i tried to compile this and the first thing i get is your main function has no type (int, void, ...). i dont know how you can run this.

next thing, your array size is defined at runtime. that also doesn't work

also int xsize,ysize = 0; is not the same as int xsize=0,ysize=0;
Last edited on
everything compiles fine in codeblocks.
i am wondering why when i say

mouseisland[7][9] = 1;

mouseisland[8][0] also becomes 1???

anyone?

@Darkmaster: TC is probably using an older compiler that accepts the nonstandard typeless main().

@TC: I'd therefore recommend either updating your compiler or switching to something more recent.

As for your question: mouseisland[7][9] and [8][0] are the same location. Each row has only 9 elements (ranging from [7][0] to [7][8]), so [7][9] is one past the end of that row. That happens to overflow into the next row.
problem solved ..


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

using namespace std;



main(){

int xsize,ysize = 0;
// int tempnum = 0;

cout<<"Enter X size of island: ";
cin>>xsize;

cout<<endl<<"Enter Y size of island: ";
cin>>ysize;


bool mouseisland[xsize][ysize];
xsize = xsize - 1;
ysize = ysize - 1;
for (int counter1=0; counter1<=xsize; counter1++){
    for (int counter2=0; counter2<=ysize; counter2++){
        if ((counter1==0) || (counter2==0))
            mouseisland[counter1][counter2] = 0;
        else if ((counter2 == ysize))
            mouseisland[counter1][counter2] = 0;
        else if ((counter1 == xsize))
            mouseisland[counter1][counter2] = 0;
        //else if ((counter1 == xsize - 2) && (counter2 == ysize))
        //     mouseisland[counter1][counter2] = 1;
        else mouseisland[counter1][counter2] = 1;
    }
}

//// ************************ THIS IS DRIVING ME CRAZY!!!!
//// using a 10 X 10

mouseisland[7][9] = 1; //  <--- why does this also make mouseisland[8][0] also = 1???????
/////DRIVING ME MAD!!!



for (int counter1=0; counter1 <= xsize; counter1++){
    for (int counter2=0; counter2<=ysize; counter2++){
        cout<<mouseisland[counter1][counter2];

    }
    cout<<endl;

}


cout<<"hello cruel world";
return 0;
}


moving the x and y decrement to below the 2darray call fixes the problem.
Topic archived. No new replies allowed.