Home > Back-end >  How can I initialize a 2D array with a value different than 0?
How can I initialize a 2D array with a value different than 0?

Time:09-22

I've tried many methods and this one is one of them:

void init_table(int _table[][MAX_COLUMNS]) {
    for (int i = 0; i < MAX_COLUMNS; i  ) {
        for (int j = 0; j < MAX_ROWS; j  ) {
            _table[i][j] = -1;
        }
    }
}

I am just trying to figure out how to initialize my array with -1 rather than 0 it defaults to.

CodePudding user response:

If you need to fill by the same value, use std::fill:

std::fill(_table, _table   MAX_ROWS*MAX_COLUMNS, -1);

Of course, if you use padding or other advanced techniques, you should take this into account and adjust your code, but this is more advanced topics and should be considered separately.

  •  Tags:  
  • c
  • Related