initializing 2d array code example
Example 1: c fill 2d array
// Either
int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
// Or
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
// OR
int i, j;
for (i = 0; i < HEIGHT; i++) { // iterate through rows
for (j = 0; j < WIDTH; j++) { // iterate through columns
disp[i][j] = disp[i][j];
}
}
Example 2: double array in c
int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
Example 3: Java 2d array initialization
class TwoDimensionalArray {
public static void main(String[] args) {
String[][] salutation = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Kumar"}
};
// Mr. Kumar
System.out.println(salutation[0][0] + salutation[1][0]);
// Mrs. Kumar
System.out.println(salutation[0][1] + salutation[1][0]);
}
}
The output from this program is:
Mr. Kumar
Mrs. Kumar
Example 4: initialize 2d array c++
2
3
4 using namespace std;
5
6 //remember const!
7 const int ROWS = 2;
8 const int COLS = 3;
int main(){
array<array<int, COLS>, ROWS> matrix = {
1, 2, 3,
4, 5, 6
};
return 0;
}