Filling a 2D array

So my question is how does one fill a 2D array full of numbers. Not random numbers. In my particular case i'm trying to fill it with zeros

static const unsigned int MAX_BOARD = 30;
currentLife[MAX_BOARD][MAX_BOARD]
Last edited on
One way would be to loop over it.

Here is a visualztion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main(){

  int rows = 10;
  int cols = 10;

  //first loop for col
  for (int j = 0; j < cols; ++j) {

    // second loop for rows
    for (int i = 0; i < rows; ++i) {
      cout << " (" << i << "," << j << ") " ;
    }
    cout << endl;
  }

  return 0;
}


 (0,0)  (1,0)  (2,0)  (3,0)  (4,0)  (5,0)  (6,0)  (7,0)  (8,0)  (9,0) 
 (0,1)  (1,1)  (2,1)  (3,1)  (4,1)  (5,1)  (6,1)  (7,1)  (8,1)  (9,1) 
 (0,2)  (1,2)  (2,2)  (3,2)  (4,2)  (5,2)  (6,2)  (7,2)  (8,2)  (9,2) 
 (0,3)  (1,3)  (2,3)  (3,3)  (4,3)  (5,3)  (6,3)  (7,3)  (8,3)  (9,3) 
 (0,4)  (1,4)  (2,4)  (3,4)  (4,4)  (5,4)  (6,4)  (7,4)  (8,4)  (9,4) 
 (0,5)  (1,5)  (2,5)  (3,5)  (4,5)  (5,5)  (6,5)  (7,5)  (8,5)  (9,5) 
 (0,6)  (1,6)  (2,6)  (3,6)  (4,6)  (5,6)  (6,6)  (7,6)  (8,6)  (9,6) 
 (0,7)  (1,7)  (2,7)  (3,7)  (4,7)  (5,7)  (6,7)  (7,7)  (8,7)  (9,7) 
 (0,8)  (1,8)  (2,8)  (3,8)  (4,8)  (5,8)  (6,8)  (7,8)  (8,8)  (9,8) 
 (0,9)  (1,9)  (2,9)  (3,9)  (4,9)  (5,9)  (6,9)  (7,9)  (8,9)  (9,9) 


Giving us this program.

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
#include <iostream>
using namespace std;

int main(){

  static const unsigned int MAX_BOARD = 30;
  int currentLife[MAX_BOARD][MAX_BOARD];

  //first loop for col
  for (int j = 0; j < MAX_BOARD; ++j) {

    // second loop for rows
    for (int i = 0; i < MAX_BOARD; ++i) {
      currentLife[i][j] = 0 ;
    }
    cout << endl;
  }


  //first loop for col
  for (int j = 0; j < MAX_BOARD; ++j) {

    // second loop for rows
    for (int i = 0; i < MAX_BOARD; ++i) {
      cout << currentLife[i][j];
    }
    cout << endl;
  }



000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000


Last edited on
Does this work?
currentLife[MAX_BOARD][MAX_BOARD] = {{0}};

Does this work?
currentLife[MAX_BOARD][MAX_BOARD] = {{0}};

Only for initialisation, but a (slightly) shorter version would be
 
int currentLife[MAX_BOARD][MAX_BOARD]{};
you can cast it around and force it into a 1d array and memset it. But that is kind of ugly unless you are in a hurry to fill it up.

the loop is

for(r = 0; r < maxboard; r++)
for(c = 0; c< maxboard; c++)
currentLife[r][c] = 0;

Last edited on
So my next step is to implement a rule where if a 'o' has more than 3 'o' that are horizontally, vertically, or diagonally adjacent to it, that particular cell will die, turning into '-'. Any tip on where to begin?
make a function that checks it using a for loop..
(not sure if this is the right way but usually I check something with a for loop)
Last edited on
Topic archived. No new replies allowed.