Hi guys, here is my code.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
        
void chooseNumber()
{
        int gameRow, gameColumn;
                        
        cout << "Please enter two values, the row and column of the cell "
             << "you would like to pull from the gameboard." << endl;
        cin >> gameRow;
        cin >> gameColumn;
                        
        while( gameRow < 1 && gameRow > 9)
        {
                cout << "Please enter a number between 1 and 9 for the "
                     << "row." <<endl;
                cin >> gameRow;
        } 
 
        while ( gameColumn < 1 && gameColumn > 9)
        {
                cout << "Please enter a number between 1 and 9 for the "
                     << "column." << endl;
                cin >> gameColumn;
        }
        cout << gameRow << gameColumn;
}
             
void displayBoard(int gameBoard[15][12])
{
        cout << "Here is the board. \n" << endl;
        
                for(int i= 0; i < 15; i++)
                {
                        int column = 0;   
                        for(int i= 0; i < 12; i++)
                        {
                                cout <<  gameBoard[i][column];
                        }
                        cout << endl;
                        column++;
                }

}
        
int main()
{
        int gameBoard[15][12];

        srand (  time(NULL) );

        cout << "Welcome to the Same Game. The rules are as follows: "
             << endl << "You will be given a 15x12 grid filled with "
             << "random integers spanning from 1 to 9. The object of "
             << "the game is to eliminate 'cells', or two or more "
             << "similar integers connected together vertically or "
             << " horizontally until the entire board is clear. If "
             << "you run out of moves, then it's game over. \n" << endl;
        
        for(int i= 0; i < 15; i++)
        {
                int column = 0;
                for(int i= 0; i < 12; i++)
                {
                         gameBoard[i][column] = rand() % 9 + 1;
                         
                }
                cout << endl;
                column++;
        }
          
        displayBoard(gameBoard);
        chooseNumber();
}

I am trying to pass 2D array gameBoard into the displayBoard function and display the board to the user. However, when I try to display my function, it only outputs the final row of my 2D array 12 times. Any help is appreciated!

for(int i= 0; i < 15; i++)
{
  int column = 0;
  for(int i= 0; i < 12; i++)
  {
    gameBoard[i][column] = rand() % 9 + 1;
  }
  cout << endl;
  column++;
}

I'm not sure what you think this is doing, but it's not filling the table properly. You increment column, but that's basically a no-op because then on the next iteration it gets initialized to 0 again. Try this instead (and be sure to repeat the change wherever you're making the mistake:

for(int i= 0; i < 15; i++)
{
  for(int column= 0; column < 12; column++)
  {
    gameBoard[i][column] = rand() % 9 + 1;
  }
}

Thanks, that is what I meant to do, I just didn't think of utilizing my 2nd for loop to increment column.

This article has been dead for over six months. Start a new discussion instead.