Thread: Checking 2d array...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    39

    Checking 2d array...

    New to this board...

    I 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:

    Check to see if five of the same letter is found vertically, horizontally or diagonals...and report...


    Code:
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
        
      char board[5] [5];
      srand(time(0));
      
    
      for (int i = 0; i < 5; i++)
      {
      
        for (int j = 0; j < 5; j++)
        {
          board[i] [j] = static_cast<char>('a' + rand() % ('z' - 'a' + 1));
          cout << setw (3) << board[i] [j];
    
        }
    
        cout << endl;
        
      }
    
      // Check rows
      for (int i = 0; i < 5; i++)
      {
        bool same = true;
        for (int j = 1; j < 5; j++)
        {
          if (board[i] [0] != board[i] [j])
          {
            same = false; break;
          }
        }
        if (same) cout << "All " << board[i] [0] << "'s on row " << i << endl;
      }
    
      // Check columns
      for (int j = 0; j < 5; j++)
      {
        bool same = true;
        for (int i = 1; i < 5; i++)
        {
          if (board[0] [j] != board[i] [j])
          {
            same = false; break;
          }
    
        }
        if (same) cout << "All " << board[0] [j] << "'s on column " << j << endl;
      }
    
      // Check major diagonal
      bool same = true;
      for (int i = 1; i < 5; i++)
      {
        if (board[0] [0] == board[i] [i])
        {
          same = false; break;
        }
      }
      if (same) cout << "All " << board[0] [0] << "'s on major diagonal" << endl;
    
      // Check subdiagonal
      same = true;
      for (int i = 1; i < 5; i++)
      {
        if (board[0] [4] == board[i] [4 - i])
        {
          same = false; break;
        }
      }
      if (same) cout << "All " << board[0] [0] << "'s on subdiagonal" << endl;
    
    /* Scaffolding code for testing purposes */
    cin.ignore(256, '\n');
    cout << "Press ENTER to continue..." << endl;
    cin.get();
    /* End Scaffolding */
    
      return 0;
    }
    It does random place value into a 5 by 5 array however when I check it is wrong:

    Any insight would be appreciated...Thanks

    2 Sample Runs:

    m q h o q
    n k t f d
    l n d m q
    e y r n n
    v v h l k
    All m's on major diagonal
    All m's on subdiagonal

    w g j m g
    h a k u s
    i v b v i
    b e s i r
    d i q a i
    All w's on major diagonal
    All w's on subdiagonal

  2. #2
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Code:
        if (board[0] [0] == board[i] [i])
        {
          same = false; break;
        }
    Here's your condition for major diagonal. See anything wrong?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    39
    Should I be checking for != vs. == ?

    That's the only thing I can see...

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    39
    Figured it out thanks...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM