0

What have I done wrong? The error I'm receiving is saying that I can't use bWidth and bHeight to set the size of the array unless they're constant, but I very much need them to be variable. Have I formatted this poorly?

int random_state(int bWidth, int bHeight) {



int board_state[bWidth][bHeight];

for (int i = 0; i <= bWidth * bHeight; i++) {

    srand(time(NULL));

    int randomOut = rand() % 2 - 1;

    board_state[i][i] = randomOut;
    cout << board_state[i][i];

}


cout << "Test";

} enter image description here

Phronesis
  • 1
  • 1
  • 1
    either look at dynamic arrays or (if you can) use std::vector – BlueLightning42 Dec 06 '20 at 22:49
  • `int random_state(int bWidth, int bHeight) { int* board_state = NULL; int n1, n2; n1 = bWidth; n2 = bHeight; board_state = new int[n1][n2]; for (int fi = 0; fi <= n1 && fi <= n2; fi++) { board_state[fi] = 0; } for (int i = 0; i <= bWidth * bHeight; i++) { srand(time(NULL)); int randomOut = rand() % 2 - 1; board_state[i][i] = randomOut; cout << board_state[i][i]; } cout << "Test"; }` I'm encountering the same issue as before, but now only with n2. – Phronesis Dec 06 '20 at 22:59
  • If you absolutely need a 2d plain c array https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new – BlueLightning42 Dec 06 '20 at 23:05
  • keep in mind you'll have to write a similar loop to delete each row afterwards- You could also use a 1d array and pretend its a 2d array by manually calculating the offset – BlueLightning42 Dec 06 '20 at 23:07

0 Answers0