### 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...

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include using namespace std; main(){ int xsize,ysize = 0; // int tempnum = 0; cout<<"Enter X size of island: "; cin>>xsize; xsize = xsize - 1; cout<>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<
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
 ``123`` ``````for (int counter1=0; counter1 <= xsize; counter1++){ for (int counter2=0; counter2<=ysize; counter2++){ cout<

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 ..

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````#include using namespace std; main(){ int xsize,ysize = 0; // int tempnum = 0; cout<<"Enter X size of island: "; cin>>xsize; cout<>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<

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