# Thread: Simple 2D array problem...In a Hurry!

1. ## 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. ## 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. ## 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...

4. ## 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 ....

5. ## ?

What does the "{0}" do in

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

Thanks

Gr3g

6. 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. 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

8. 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. ## 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...