Random Checkerboard Help

• 05-10-2009
ninety3gd
Random Checkerboard Help
have a C++ book I am currently looking at and trying to make some modifications...

Here is the code I modified from C++ text I have:
Have a checkerboard of random 0s and 1s and check to see if of the same number is found vertically, horizontally or diagonals...

diagonals apparently do not work..
Runs:
Code:

```11101001 01100101 11110110 10000011 11111111 11001010 01001100 11001001 All 1's on row 4 No same numbers on the same column No same numbers on the same diagonal No same numbers on the same subdiagonal 11011011 00000001 00000011 11110001 11100111 10000001 10110101 01001101 All 1's on column 7 All 1's on sub-diagonal No same numbers on the same row No same numbers on the same diagonal```
The above is wrong...

Here is the code...I cant see the problem...thanks for help

Code:

```#include <iostream> #include <ctime> // for time function #include <cstdlib> // for rand and srand functions using namespace std; int main() {   srand(time(0));   int board[8][8];   bool isSameOnARow = false, isSameOnAColumn = false,       isSameOnADiagonal = false, isSameOnASubdiagonal = false;   for (int i = 0; i < 8; i++)   {     for (int j = 0; j < 8; j++)     {       board[i] [j] = rand() % 2;       cout << board[i] [j];     }     cout << endl;   }   // Check rows   for (int i = 0; i < 8; i++)   {     bool same = true;     for (int j = 1; j < 8; j++)     {       if (board[i][0] != board[i] [j])       {         same = false; break;       }     }     if (same) {       cout << "All " << board[i] [0] << "'s on row " << i << endl;       isSameOnARow = true;     }   }   // Check columns   for (int j = 0; j < 8; j++)   {     bool same = true;     for (int i = 1; i < 8; i++)     {       if (board[0] [j] != board[i] [j])       {         same = false; break;       }     }     if (same) {       cout << "All " << board[0] [j] << "'s on column " << j << endl;       isSameOnAColumn = true;     }   }   // Check major diagonal   bool same = true;   for (int i = 1; i < 8; i++)   {     if (board[0] [0] == board[i] [i])     {       same = false; break;     }   }   if (same) {     cout << "All " << board[0] [0] << "'s on major diagonal" << endl;     isSameOnADiagonal = true;   }   // Check subdiagonal   same = true;   for (int i = 1; i < 8; i++)   {     if (board[0] [7] == board[i] [7 - i])     {       same = false; break;     }   }   if (same) {     cout << "All " << board[0][0] << "'s on sub-diagonal" << endl;     isSameOnASubdiagonal = true;   }   if (!isSameOnARow) {     cout << "No same numbers on the same row" << endl;   }   if (!isSameOnAColumn) {     cout << "No same numbers on the same column" << endl;   }   if (!isSameOnADiagonal) {     cout << "No same numbers on the same diagonal" << endl;   }   if (!isSameOnASubdiagonal) {     cout << "No same numbers on the same subdiagonal" << endl;   } /* Scaffolding code for testing purposes */ cin.ignore(256, '\n'); cout << "Press ENTER to continue..." << endl; cin.get(); /* End Scaffolding */   return 0; }```
• 05-11-2009
Salem
Compare your if() statement with one which works, with one that doesn't.