WE can use fill_n function to initialize 1D array with value.
int table[20];
fill_n(table, 20, 100);
But how can we initialize 2D array with same values.
int table[20][20];
fill_n(table, sizeof(table), 100); //this gives error
Using fill_n you can write:
std::fill_n(&table[0][0], sizeof(table) / sizeof(**table), 100);
Use std::vector:
std::vector<std::vector<int>> table(20, std::vector<int>(20, 100));
All done and "filled" at the declaration. No more code needed.
std::array that would be even better.
20 * sizeof(int)entries, which is well beyond the bounds of the array.