Simple 2D array problem...In a Hurry!

This is a discussion on Simple 2D array problem...In a Hurry! within the C++ Programming forums, part of the General Programming Boards category; What exactly is wrong with this section of code. All I want to do is read in numbers into a ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    6

    Angry Simple 2D array problem...In a Hurry!

    What exactly is wrong with this section of code. All I want to do is read in numbers into a 4X4 array and then output them to the screen in a 4X4 pattern. Please let me know what I am doing wrong ASAP. Thanks!

    P.S. Is there a way to enter the numbers by row (separated by spaces) instead of each individual like I am doing? If so, how? Thanks again!


    #include <iostream.h>
    #include <iomanip.h>

    int main()
    {
    int grid[4][4]={0};
    bool magic = true;

    int row, col;
    for(row=0; row<4; row++)
    {
    for(col=0; col<4; col++)
    {
    cout << "Please enter the value for row " << row << ", col " << col << ": ";
    cin >> grid[row][col];
    }
    }



    for(row=0; row<4; row++)
    {
    for(col=0; col<4; col++);
    {
    cout << grid[row][col];
    }
    cout << endl;
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: Simple 2D array problem...In a Hurry!

    Should be like this....

    Code:
    #include <iostream.h> 
    #include <iomanip.h> 
    
    int main() 
    { 
    int grid[4][4]={0}; 
    bool magic = true; 
    
    int row, col; 
    for(row=0; row<4; row++) 
    { 
     for(col=0; col<4; col++) 
     { 
      cout << "Please enter the value for row " << row << ", col " << col << ": ";  
      cin >> grid[row][col]; 
     } 
    } 
    
    
    
     for(row=0; row<4; row++) 
     { 
      for(col=0; col<4; col++)   // THIS LINE! NO ;
      { 
       cout << grid[row][col]; 
      } 
      cout << endl; 
     } 
    return 0; 
    }
    Semicolons after for loops declarations are no-no's.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Don't practice your hand to write "; "

    Hi,
    Don't used to do that because it is bad ...

    it is bad ... because ones you used to it... even if you read your sintax for the first and the second and the third time ... you want be able to catch your mistake... but I think that the compiler should give you worning for that...
    let me try it....

    I know that you are in hurry ... but read that carfully...
    C++
    The best

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    I tried it... it works

    even with " ;" it works ... but hte compiler will not give you any kind of misstake ....

    it won't give you any kind of misstake...
    you better be carfull next time...

    I hope that helped you ....
    C++
    The best

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    ?

    What does the "{0}" do in

    int grid[4][4]={0};

    Thanks

    Gr3g
    Chance favors the prepared mind.

    Vis. C++ 6.0

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Gr3g: Fills the entire array with 0.

    for (i = 0; i < 10; i++);
    {
    stuff;
    }

    The for loop ends on the same line it is created, then the brackets are just used to specify code blocks.

    {
    stuff;
    }

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    27
    oh ok i normally just use a for loop to fill my arrarys with regular data...

    int blah[9][9];

    for(int i=0;i<9;i++)
    {
    for(int x=0;x<9;x++)
    blah[i][x]=0;
    }

    Thanks
    Chance favors the prepared mind.

    Vis. C++ 6.0

  8. #8
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    So do I
    Habit I guess...

    Oh, and also, you can't refill your arrays using the above method... you must loop through like you stated.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Good happet...

    Thanx,
    Atarting by today... I will try to write in the same way ....
    It is Great....
    but all copyright registared for you Dear....

    Thnx anyway...
    C++
    The best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ a very simple program
    By amjad in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2009, 12:59 PM
  2. Problem with 2D array
    By Stlcardinal50 in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2009, 03:23 PM
  3. Need to pass a pointer of a unknown sized 2d array
    By (Slith++) in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2007, 09:15 PM
  4. Problem with a 2D array.
    By earth_angel in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2005, 11:28 AM
  5. 2d Array Problem.
    By Drealoth in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2003, 01:04 AM

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