Thread: beginning array question

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    beginning array question

    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

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    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;

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    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!

  4. #4
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Thanks, I love this board.
    AIM: MarderIII

  5. #5
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Is the below proper syntax:

    Code:
    if (maploc[x][y]==[0][0])
    {
    cout<<"hi";
    }
    also, when i do:

    int maploc3x3[3][3]={0};

    i get a warning about my initialization not being properly bracketed
    AIM: MarderIII

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    >maploc[x][y]==[0][0]
    A no no maploc[x][y] is one element, so you just need:
    Code:
    if (maploc[x][y] == 0)
    Also try the way I wrote here to initialise the array, you even can make a function to initialise arrays.

  7. #7
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    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

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    >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.

    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");
       }
    Of course here you could use a 'switch' to make the code more readable.

  9. #9
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Im not understanding something, what if the array doesnt equal {2} but it equals [1][3] ?
    AIM: MarderIII

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    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.

  11. #11
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Is there no way to check that it has [0][1] and not [1][0] ?
    AIM: MarderIII

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    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
        ......
       }

  13. #13
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    So [1][1] is the same thing as [1][0] ?
    AIM: MarderIII

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    No, I told you... [1][1] is one element, [0][1] is another. Draw this in your mind as an matrix:
    Code:
    [0][0]      [0][1]      [0][2]
    [1][0]      [1][1]      [1][2]
    [2][0]      [2][1]      [2][2]
    Now check by yourself if [0][1] is the same as [1][1]

  15. #15
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM