TeachMeTWz
TeachMeTWz

Reputation: 51

How can I initialize a 2D array with a value different than 0?

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.

Upvotes: 0

Views: 96

Answers (1)

Damir Tenishev
Damir Tenishev

Reputation: 3272

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.

Upvotes: 3

Related Questions