Thread: MultiD Array[][]

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    MultiD Array[][]

    Hello!

    Can i use a for() loop this way or should i use two for() loop?
    It compiles, executes but nothing gets printed out.

    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
    
         char board[2][33];
         for(int i = 0, j = 0; board[i][j] < '\n'; i++,j++){
    
         char board[2][33] ={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0}};
    
              if(i<33) cout << board[i][j];
    
              else     cout << endl;
       }
    
    //system("pause");
    return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And what do you expect? There's very little here that makes sense. E.g

    Code:
    board[i][j] < '\n'
    Why do you think comparing uninitialized values against 13 (or 10) would be relevant to the loop?

    Yes, you'll need two nested loops. One that goes from 0 to 2 and the other from 0 to 33 (not including 2 and 33)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Code:
    board[i][j] < '\n'
    I saw that in somebody's code and though surprised, i assumed it was right.

    Thanks for clearing things up.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you also have 2 board arrays - 1 not initialized outside the loop
    and second you initialize on each iteration inside the loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks Vart!

    I also made the mistake by thinking that the first value was the Width. Its the other way around.

    char board [HEIGHT][WIDTH];
    Last edited by Ducky; 03-21-2009 at 09:40 AM.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    first index should be j - second i, you have it mixed up
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing point to array[][] to a function
    By gazsux in forum C Programming
    Replies: 3
    Last Post: 04-29-2003, 07:45 AM