-3

I have to do Conway's Game of Life. I need to create a 20x80 board using a dynamic array (institutional requirements).

const int ROWS = 20
const int COLUMNS = 80

void createBoard(char board[ROWS][COLUMNS]){
    for(int i = 0; i < ROWS; i++){
        for(int j = 0; j < COLUMNS; j++){
            board[i][j] = ' ';
        }
    }
    cout << "Created board" << endl;

}

I managed to create this board, but i'm not using a dynamic array here.

I know that to create a dynamic array i have to do the following:

char *array = new char[size];

But how i would implement the dynamic array in the for loop? I cant get my head around on how to convert the alredy made function createBoard into a dynamic array

mike24
  • 3
  • 1
  • change `char board[ROWS][COLUMNS]` to `char** board`, that should be it? – Alan Birtles Sep 01 '22 at 12:54
  • `Boost.MultiArray` – user1095108 Sep 01 '22 at 12:55
  • Are you allowed to use `std::vector`? – RoQuOTriX Sep 01 '22 at 12:55
  • Requirements say that the board has to be dynamic array, just that @RoQuOTriX – mike24 Sep 01 '22 at 12:58
  • 5
    If you need a dynamic array, you want `std::vector`. Not this manual memory management nonsense. – Jesper Juhl Sep 01 '22 at 13:02
  • im just starting, sorry how may i go about doing that? @JesperJuhl – mike24 Sep 01 '22 at 13:04
  • 1
    @mike24 Consult a [good C++ text book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and be prepared for the fact that C++ is a *really complicated* language that takes several years (I'd say at least 5+ with daily practice) to master. – Jesper Juhl Sep 01 '22 at 13:08
  • Can you explain what is the subject of the chapter in your C++ textbook where this practice problem is from, this will help point you towards the right solution? You're not practicing sample code assignments from a good textbook, but trying random coding puzzles from some spam site that promises to turn you into an instant C++ uberhacker, if you only do these coding puzzles? Sorry, but you won't be able to learn C++ like that, it's just too complicated. Those web sites misled you. The only effective way to learn C++ is with a textbook, and it takes a very long time. – Sam Varshavchik Sep 01 '22 at 13:13
  • If you have some academic requirement that forces you to use `new` for this instead of a vector look here: [https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – drescherjm Sep 01 '22 at 13:18

1 Answers1

0

In the following code snippet function createBoard dynamically allocates and initializes the board as 2D array in row-major order. Function deleteBoard deallocates the board.

const int ROWS = 20;
const int COLUMNS = 80;

char ** createBoard( void )
{
    //allocate array of pointers to board's rows
    char ** board = new char *[ ROWS ];

    for( int rowIdx = 0; rowIdx < ROWS; ++rowIdx ){
        //allocate board's rowIdx-th row
        board[ rowIdx ] = new char[ COLUMNS ];

        //initialize content of allocated row
        for( int columnIdx = 0; columnIdx < COLUMNS; ++columnIdx )
            board[ rowIdx ][ columnIdx ] = ' '; //accessing element at rowIdx-th row and columnIdx-th columns
    }

    return board;
}

void deleteBoard( char ** aBoard )
{
    //dealocate every row
    for( int rowIdx = 0; rowIdx < ROWS; ++rowIdx )
        delete [] aBoard[ rowIdx ];

    //deallocate array of pointers to board's rows
    delete [] aBoard;
}
cerveka2
  • 156
  • 7