How could I properly turn this array into a multidimensional one?

1
int Grid[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int BattleShipPlace = rand() % 7;
for (int i = 0, n = 0; i < 10; ++i, ++n) {
    if (BattleShipPlace == i) {
        Grid[n] = 1;
        Grid[n + 1] = 1;
        Grid[n + 2] = 1;
    }
}

In this snippet of code I am placing a random 111 in the array, however when trying to convert this to a multidimensional array as my end goal is too make battleship (with 1s being ships). I came up with this.

int *Grid[5][5] = {0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 
                      0, 0, 0, 0, 0, 
                      0, 0, 0, 0, 0, };
    int BattleShipPlace = rand() % 2;
    for (int i = 0, n = 0; i < 5; ++i, ++n) {
        if (BattleShipPlace == i) {
            *Grid[n][0] = 1;
            *Grid[n + 1][0] = 1;
            *Grid[n + 2][0] = 1;
        }
    }
    for (int y = 0, x = 0; x < 5; x++, y++)
    {
        cout << Grid[x][y];
    }
    return **Grid;
}

But this came with the error "Unhandled exception at 0x00475551 in Testing battleship placement.exe: 0xC0000005: Access violation writing location 0x00000000.". How could I fix this?

c++
arrays
matrix
asked on Stack Overflow Apr 15, 2021 by Gamer Getgamed

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0