MultiD Array[][]

This is a discussion on MultiD Array[][] within the C++ Programming forums, part of the General Programming Boards category; Hello! Can i use a for() loop this way or should i use two for() loop? It compiles, executes but ...

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

    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;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  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
    817
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you also have 2 board arrays - 1 not initialized outside the loop
    and second you initialize on each iteration inside the loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    817
    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 10:40 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    first index should be j - second i, you have it mixed up
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 08:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21