Help for code on rand and board

I am quite stuck on a homework assignment. I have a function that has to recognize 3 boards and randomly display one and if the user gets the answer wrong then they will display another board<- this is in beginGame(). I do not know what code to put and how it would randomly display. Any help would be appreciated. I am imagining there are multiple things wrong with my current program, but im just fixated on this one area and unable to think. And I have to use only the three libraries.


#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int beginGame(int playedBoard[]);
void displayBoard(int board[][3]);
bool testWinner(int ans, int boardNum, int ansBoard[]);
void instructions();

int main()
{
int board1[4][3] = { 38, 11, 83,
15, 6, 33,
10, 2, 20,
86, NULL, 95 };
int board2[4][3] = { 28, 10, 55,
89, 17, 98,
22, 4, 31,
60, NULL, 78 };
int board3[4][3] = { 90, 9, 45,
66, 12, 48,
34, 7, 70,
44, NULL, 26 };

int ansArray[3] = { 14, 15, 8 };
int usedBoards[3] = { NULL};
int answer, guess;
int win = 0;
int tries = 0;


instructions();

cout << " Enter Guess or 0 to exit" << endl;
cin >> guess;


}


int beginGame(int playedBoard[])
{
int randomNum = (rand() % (3-1 + 1)) + 1;
if playedBoard[r] == 0;
return r;


return randomNum;
}

void displayBoard(int board[][3])
{


}

bool testWinner(int ans, int boardNum, int ansBoard[])
{


}
Something along these lines, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

const int NROWS = 4 ;
const int NCOLS = 3 ;
using board = int[NROWS][NCOLS] ;

void display_board( const board& brd )
{
    for( const auto& row : brd )
    {
        for( int v : row )
        {
            if( v == 0 ) std::cout << " NULL " ;
            else std::cout << std::setw(4) << v << "  " ;
        }
        std::cout << '\n' ;
    }
}

int display_random_board( const board brds[], int num_boards )
{
    if( num_boards < 1 ) return num_boards ;

    const int random_board_number = std::rand() % num_boards ;
    std::cout << "board #" << random_board_number << "\n--------\n" ;
    display_board( brds[random_board_number] ) ;
    return random_board_number ;
}

void begin_game( board brds[], int num_boards )
{
    [[maybe_unused]] const int chosen_board = display_random_board( brds, num_boards ) ;

    // ...
}

int main()
{
    std::srand( std::time(nullptr) ) ;

    const int NBOARDS = 3 ;
    board game_boards[NBOARDS] =
    { { 38, 11, 83, 15, 6, 33, 10, 2, 20, 86, 0, 95 }, // note: brace elision
      { 28, 10, 55, 89, 17, 98, 22, 4, 31, 60, 0, 78 },
      { 90, 9, 45, 66, 12, 48, 34, 7, 70, 44, 0, 26 }
    };

    begin_game( game_boards, NBOARDS ) ;
}

http://coliru.stacked-crooked.com/a/eaae5b3f8e9da4ec
NULL is meant for pointers. If you mean zero you should write 0, or use some constant with the value zero, but not NULL.
Last edited on
Topic archived. No new replies allowed.