I have array[2][2] and I want to initialize this with all 9 segments having a value of 0. Is there a way to set the entire array to 0, without manually entering a line of code which says
array[0][3]=0;
for every segment?
I have array[2][2] and I want to initialize this with all 9 segments having a value of 0. Is there a way to set the entire array to 0, without manually entering a line of code which says
array[0][3]=0;
for every segment?
AIM: MarderIII
Loop trough the array, and set the value to 0:
Code:int myArr[3][3]; int i, j; for (i=0; i<3; i++) for (j=0; j<3; j++) myArr[i][j] = 0;
int array[2][2] = {0};
--edit--
This is what you put when declareing the array. all elements will be set to 0.
The keyboard is the standard device used to cause computer errors!
Thanks, I love this board.
AIM: MarderIII
Is the below proper syntax:
also, when i do:Code:if (maploc[x][y]==[0][0]) { cout<<"hi"; }
int maploc3x3[3][3]={0};
i get a warning about my initialization not being properly bracketed
AIM: MarderIII
>maploc[x][y]==[0][0]
A no no maploc[x][y] is one element, so you just need:
Also try the way I wrote here to initialise the array, you even can make a function to initialise arrays.Code:if (maploc[x][y] == 0)
Well what if I want to do something only when for maploc[x][y] x=3 and y=2? Im trying to make a map btw, but now im trying to figure out some way I could use an array and maybe a loop to simplify things
Last edited by Noobie; 04-30-2003 at 08:25 AM.
AIM: MarderIII
>Well what if I want to do something only when for maploc[x][y] x=3 and y=2?
Ok, let's suppose we have an 2D array. We want to loop trough all the elements of this array, if it is 1, we print the message "one" if it is 2, we print the message "two" and same with 3.
Of course here you could use a 'switch' to make the code more readable.Code:int myArr[3][3]; //I suppose you fill the array alone... int x,y; .... .... for (x=0; x<3; x++) for (y=0; y<3; y++) { if (myArr[x][y] == 1) puts("One"); else if (myArr[x][y] == 2) puts("Two"); else if (myArr[x][y] == 3) puts("Three"); }
Im not understanding something, what if the array doesnt equal {2} but it equals [1][3] ?
AIM: MarderIII
You're not understanding the point of 2D arrays. Look, I'm passing over ALL elements of this array.
[0][0] is one element
[0][1] is another
[0][2] is another
[1][0] is another
I check wether in [0][0] it contains the value 1, 2 or 3.
I check wether in [0][1] it contains the value 1, 2 or 3.
I check wether in [0][2] it contains the value 1, 2 or 3.
I check wether in [1][0] it contains the value 1, 2 or 3.
Last edited by serruya; 04-30-2003 at 08:41 AM.
Is there no way to check that it has [0][1] and not [1][0] ?
AIM: MarderIII
yes and I did check it
Lets say you just want to check [0][0], [0][1] and [0][2]. Put in your mind, they are ONE element, even if to acess them you write 2 numbers, they're one element and can hold ONLY one integer.
Code:for (i=0; i <= 0 ; i++) for (j=0; j<3; j++) { if (myArr[i][j] == 1) //do something else if (myArr[i][j] == 2) //do something ...... }
So [1][1] is the same thing as [1][0] ?
AIM: MarderIII
No, I told you... [1][1] is one element, [0][1] is another. Draw this in your mind as an matrix:
Now check by yourself if [0][1] is the same as [1][1]Code:[0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2]
what Im trying to do is create a map
00 01 02
10 11 12
20 21 22
and when
you are at 00 it does one thing, like
cout<<"You are at 0 0."
and when at 22 it does another thing, like
cout<<"You are at 2 2."
Do you see what Im confused about?
AIM: MarderIII