I am trying to create a 2 dimation array for a game...
The table will look like that!!!

**0 1 2 3 4 5 6 7 **

**0|0 0 0 0 9 0 0 0 **
**1|0 0 0 0 0 0 0 0 **
**2|9 0 9 0 0 0 9 0 **
**3|0 0 0 0 0 0 0 0 **
**4|0 9 0 9 0 0 0 0 **
**5|0 9 0 0 9 0 9 0 **
**6|0 0 0 0 0 0 0 0 **
**7|0 0 0 0 0 0 9 0 **

Below it's my code my serioue problem is that it's prints 7 rows insted of 8 and
it's set the number '9' only into the first row, besiclly it's ingnore the other rows
... Any help appritiate..!!

Thank you in advance!!!

 int main(){

                             int counter2 = 0;
                             int counter3 =0;
                             int board[8][8];



                        cout<<""<<endl;
                        cout<<"  0 1 2 3 4 5 6 7 "<<endl;
                        cout<<"  - - - - - - - - "<<endl;

                        for(int x=0;x<7;x++){
                            if(x==0)
                          cout<<counter2++<<"|0 0 0 0 9 0 0 0";
                            else
                            if (x==1 || x == 3 || x== 6)
                            cout<<counter2++<<"|0 0 0 0 0 0 0 0";
                            else


                            for(int y=0;y<8;y++){
                               if(y==0 )
                                cout<<counter2++<<"|0 0 0 0 0 0 0 0";
                                if(board[x][y] == 0 )
                                    cout<<setw(1);
                            }
                            cout <<endl;
                        }
            return 0;
}

Recommended Answers

All 10 Replies

for(int x=0;x<7;x++)

Should be X < 8 OR X <= 7 for the condition.. 0 counts as the first row of course.. you have 9 columns.. only showing 8.

Thank you for your reply... 0 - 7 = 8 rows and 8 col
since arrays stars array[8];

array = {0,1,2,3,4,5,6,7} = 7+0(0 counts as 1) = 8

I have already fixed the problem with the output with col and rows

Here is the code::: (What about the NUMBER 9 ?? Any ideas?? Thank you!!! )

int main(){

          int counter2 =0;
          int board[8][8];

                        cout<<""<<endl;
                        cout<<"  0 1 2 3 4 5 6 7 "<<endl;
                        cout<<"  - - - - - - - - "<<endl;


                       for(int x=0;x<7;x++){
                            if(x==0)
                         cout<<counter2++<<"|0 0 0 0 9 0 0 0"<<endl;

                           if (x==1 || x ==6 || x == 3)
                            cout<<counter2++<<"|0 0 0 0 0 0 0 0";
                           else


                            for(int y=0;y<8;y++){
                              if(y==1)
                                cout<<counter2++<<"|9 0 9 0 0 0 9 0";
                                if(board[x][y] == 0 )
                                    cout<<setw(1);
                            }
                            cout <<endl;
                        }
            return 0;
}

The row and colum labels can be dealt with separately from the game board itself.

You can't assign a whole row at a time because the board is an array of type int, not an array of strings.

You will need to give each cell of the board a value one at a time either at initalization or with assignment to each cell.

Option 1:
int simpleBoard[3] = {0, 9, 0}; //assign all desired values at initialization.

Option 2:
int simpleBoard[3] = 0; //initialize board to default value.
simplBoard[1] = 9; //assign 9 to a given cell of the board.

Lerner

Thank you for your reply.....
can you please be more specific??

when you declare an array without initializing or assigning values to each element of the array, the values contained in the elements have a random, or junk value. Therefore when you declare board thus:

int board[8][8];

it has no meaning, just memory to hold up to 64 ints. You could assign a default value to all members thus:

int board[8][8] = 0; 

gives all 64 elements the value of zero. The alternative to get a default value to all elements would be to assign each element a value of zero after declaration of board, thusly:

int board[8][8];
for(int i = 0; i < 8; ++i)
  for (int j = 0; j < 8; ++i)
    board[8][8];

Since most of the elements of board have value of 0 it makes sense to default and then reassign any desired element to 9. For example, if you want the 3 element of the 5 row to be 9 instead of zero do this:

board[2][4] = 9;

Repeat that process for each 9 you want to put on the board and you should be ready to go. Alternatively you could hard code all 64 elements of board on initialization like this:

int board[8][8] = {{0,0,0,0,0,0,0,0},
                   {0,0,9,0,0,0,0,0},
                    etc,

Whichever way floats your boat is fine.

when you declare an array without initializing or assigning values to each element of the array, the values contained in the elements have a random, or junk value. Therefore when you declare board thus:

int board[8][8];

it has no meaning, just memory to hold up to 64 ints. You could assign a default value to all members thus:

int board[8][8] = 0; 

gives all 64 elements the value of zero. The alternative to get a default value to all elements would be to assign each element a value of zero after declaration of board, thusly:

int board[8][8];
for(int i = 0; i < 8; ++i)
  for (int j = 0; j < 8; ++i)
    board[8][8];

Since most of the elements of board have value of 0 it makes sense to default and then reassign any desired element to 9. For example, if you want the 3 element of the 5 row to be 9 instead of zero do this:

board[2][4] = 9;

Repeat that process for each 9 you want to put on the board and you should be ready to go. Alternatively you could hard code all 64 elements of board on initialization like this:

int board[8][8] = {{0,0,0,0,0,0,0,0},
                   {0,0,9,0,0,0,0,0},
                    etc, 

Whichever way floats your boat is fine.

Oh, goody, now I need to figure out how to keep code indented when posting to the board since code tags don't work anymore and there is no informational sticky note or watermark instructions in the text box used to compose posts. Since you've done it several times maybe you can enlighten me. Thanks.

Thank you for your helpfull reply... Finaly I solved it but can anyone just simplify my code??
I mean my code it's a mess... And I don't know where to start to fixed it and simplify... So please give
a shoot and check my code below for sure there is better way to do that...

Thank you in advance!!!
Regards

int main(){

      int k = 8;
      int p = 8;
      int a = 8;
      int b = 8;
      int c = 8;
      int d = 8;
      int e = 8;
      int ROW1[8][8] = {{0,0,0,0,9,0,0,0}};
      int ROW2[8][8] = {{0,0,0,0,0,0,0,0}};
      int ROW3[8][8] = {{9,0,9,0,0,0,9,0}};
      int ROW4[8][8] = {{0,9,0,9,0,0,0,0}};
      int ROW5[8][8] = {{0,9,0,0,9,0,9,0}};
      int ROW6[8][8] = {{0,0,0,0,0,0,9,0}};
      cout<<""<<endl;
                    cout<<"  0 1 2 3 4 5 6 7 "<<endl;
                    cout<<"  - - - - - - - - "<<endl;


                    for(int x=0;x<8;++x)
                        cout<<"  "<<ROW1[0][x]<<"";
                    if (k==8)
                        cout<<endl;
                    for(int y=0; y<8; ++y)
                        cout<<"  "<<ROW2[y][0]<<"";
                    if (p==8)
                        cout<<endl;
                    for(int k=0; k<8; k++)
                        cout<<"  "<<ROW3[0][k]<<"";
                    if (a==8)
                        cout<<endl;
                    for(int q=0; q<8; ++q)
                        cout<<"  "<<ROW2[q][0]<<"";
                    if (b==8)
                        cout<<endl;
                    for (int s=0; s<8; ++s)
                        cout<<"  "<<ROW4[0][s]<<"";
                    if (c==8)
                        cout<<endl;
                    for (int f=0; f<8; ++f)
                        cout<<"  "<<ROW5[0][f]<<"";
                    if (d==8)
                        cout<<endl;
                        for (int g =0; g<8; ++g)
                            cout<<"  "<<ROW2[g][0]<<"";
                        if (e==8)
                            cout<<endl;
                        for (int h =0; h<8; ++h)
                            cout<<"  "<<ROW6[0][h]<<"";

        return 0;

}

int board[8][8] = {{0,0,0,0,9,0,0,0},
{0,0,0,0,0,0,0,0},
{9,0,9,0,0,0,9,0},
{0,0,0,0,0,0,0,0},
{0,9,0,9,0,0,0,0},
{0,9,0,0,9,0,9,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,9,0}};

  for(int r = 0; r < 8; ++r)
    for(int c = 0; c < 8; ++c)
      cout << board[r][c] << ' ' <<
  cout << endl;
commented: SOLVED +0

Thank you very much for your help....

SOLVED!!!

int main(){

int board[8][8] = {{0,0,0,0,9,0,0,0},
{0,0,0,0,0,0,0,0},
{9,0,9,0,0,0,9,0},
{0,0,0,0,0,0,0,0},
{0,9,0,9,0,0,0,0},
{0,9,0,0,9,0,9,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,9,0}};

int counter =0;

cout<<"\n  | 0  1  2  3  4  5  6  7 |";
cout<<"\n  | -  -  -  -  -  -  -  - |";
for(int r = 0; r < 8; ++r)
{cout<<"\n";cout<<" "<<counter++<<"|";
for(int c = 0; c < 8; ++c)
cout <<" "<<board[r][c]<<" ";
}}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.