2. 2D Arrays: Definition
A two-dimensional array (2D array) is, in essence, a list of one-dimensional arrays. It is the simplest
form of the multidimensional array. To declare a two-dimensional integer array of size x, y −
data_type array_Name [ x ] [ y ];
int 2D_array [10] [20];
What is the size of a 2D array?
The total number of elements that can be stored in a multidimensional array can be calculated
by multiplying the size of all the dimensions.
type arrayName [ x ] [ y ]; -> (x * y)
int 2D_array [10] [20]; -> (10 * 20) -> 200
int x[5][10][20] -> ?
3. 2D Arrays: Definition
A two-dimensional array can be thought as a table, which will have x number of rows and y number of
columns. A 2-dimensional array a, which contains three rows, and four columns can be shown
as below −
Thus, every element in array a is identified by an element name of the form a[ i ][ j ].
4. 2D Arrays: Initialization
2D arrays may be initialized by specifying bracketed values for each row.
• Following is an array with 3 rows and each row have 4 columns.
int 2D_array[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{2, 3, 4, 5} , /* initializers for row indexed by 1 */
{4, 5, 6, 7} /* initializers for row indexed by 2 */
};
• The nested braces, which indicate the intended row, are optional.
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
5. 2D Arrays: Accessing elements
An element in 2-dimensional array is accessed by using the subscripts
• row index and column index of the array
int val = a[2][3];
The above statement will take 4th element from the 3rd row of the array. You can verify it in the above diagram.
Row Column
6. 2D Arrays: Accessing elements
• One dimensional array is accessed via for loop (usually).
• Similarly, 2D array can be accessed with a nested for loop.
for (int row = 0; row < RowNumber; row++){
for (int column = 0; column < Colnumber; column ++){
cout << 2D_array [row][column]<< endl;
}
}
7. 2D Arrays: Accessing elements
#include<iostream>
using namespace std;
int main()
{
// initialization- an array with 3 rows and 4 columns.
int a[3][4] = {{0,1,2,3}, {2,3,4,5}, {4,5,6,7}};
int RowNumber = 3;
int ColNumber = 4;
// accessing the elements of the array output each array element's value
for (int row = 0; row < RowNumber; row++){
for (int column = 0; column < ColNumber; column++){
cout << " Element at [" << row <<"][" <<column<<"] is " << a[row][column]<<endl;
}
}
return 0;
}
8. 2D Arrays: Accessing elements (Example)
----------Code------------
#include<iostream>
using namespace std;
int main()
{
// initialization- an array with 3 rows and 4 columns.
int a[3][4] = {{0,1,2,3}, {2,3,4,5}, {4,5,6,7}};
int RowNumber = 3;
int ColNumber = 4;
// accessing the elements of the array output each array element's value
for (int row = 0; row < RowNumber; row++){
for (int column = 0; column < ColNumber; column++){
cout << " Element at [" << row <<"][" <<column<<"] is " << a[row][column]<<endl;
}
}
return 0;
}
------Output-------
Element at [0][0] is 0
Element at [0][1] is 1
Element at [0][2] is 2
Element at [0][3] is 3
Element at [1][0] is 2
Element at [1][1] is 3
Element at [1][2] is 4
Element at [1][3] is 5
Element at [2][0] is 4
Element at [2][1] is 5
Element at [2][2] is 6
Element at [2][3] is 7
9. 2D Arrays: Usages
• Data come naturally in the form of a table, e.g., spreadsheet
• Need a two-dimensional array.
• Multi dimension arrays are used if you want to put arrays inside an array.
• 10 students ----> 3 tests. You can create an array like: arr_name[10][3]
• call arr_name[0][0] -----> you the result of student 1 on lesson 1.
• Call arr_name[5][2] ------> ?
• Multi dimension is-
• easier to understand
• easier to debug
10. 2D Arrays: Common mistakes
• using 1 as the first index
• 0 for rows and/or columns.
• using array.length as the last valid row index
• array.length - 1
• using the wrong starting and ending indicies on loops.
• using array.length for both the number of rows and columns.
• use array[0].length for the number of columns.
• jumping out a loop by using one or more return statements before every value has been processed.