Multidimensional Arrays
Two-dimensional and multidimensional arrays in C
An array stores multiple values of the same data type. Based on the number of dimensions, arrays can be classified as one-dimensional, two-dimensional, and multidimensional arrays.
- One-dimensional array: Stores elements in a single sequence.
- Two-dimensional array: Stores elements in rows and columns, like a table.
- Multidimensional array: Has three or more dimensions, such as a three-dimensional array.
Two-dimensional array in C
Definition
A two-dimensional array is a collection of elements of the same data type arranged in rows and columns. Each element is accessed using two indexes: one for the row and one for the column.
For example, a two-dimensional array can represent students' marks in different subjects.
Visual representation: int a[2][3]
The first index identifies the row; the second identifies the column. Both indexes start at 0.
Declaration of a two-dimensional array
Syntax:
data_type array_name[rows][columns];
Example:
int a[2][3];
Here:
- int is the data type.
- a is the array name.
- 2 represents the number of rows.
- 3 represents the number of columns.
- Total elements = 2 × 3 = 6.
Initialization of a two-dimensional array
Method 1: Using nested braces
int a[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
Method 2: Using a single list
int a[2][3] = {10, 20, 30, 40, 50, 60};
In both examples, the elements are initialized in row-major order: the first row is filled first, followed by the second row.
Accessing elements
Use two indexes to access an element.
a[0][0] // First row, first column: 10
a[0][2] // First row, third column: 30
a[1][1] // Second row, second column: 50
For int a[2][3], valid row indexes are 0–1 and valid column indexes are 0–2.
Example: C Program to read and display a matrix
#include
int main(void)
{
int a[2][3];
int i, j;
printf("Enter 6 elements:\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("The matrix is:\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
Sample input:
10 20 30
40 50 60
Output:
The matrix is:
10 20 30
40 50 60
How does it work?
- The outer loop (i) controls the rows.
- The inner loop (j) controls the columns.
scanf("%d", &a[i][j])stores each input value.- The second pair of loops displays the array row by row.
Multidimensional arrays in C
Definition
A multidimensional array is an array with more than one dimension. In C, arrays can have two, three, or more dimensions.
Examples include:
int a[2][3]; // Two-dimensional array
int b[2][3][4]; // Three-dimensional array
int c[2][3][4][5]; // Four-dimensional array
Three-dimensional array
A three-dimensional array can be understood as a collection of two-dimensional tables or layers.
Visual representation: int a[2][2][3]
Each element is identified by three indexes: layer, row, and column.
Declaration and initialization
Syntax:
data_type array_name[size1][size2][size3];
Example:
int a[2][2][3] = {
{
{1, 2, 3},
{4, 5, 6}
},
{
{7, 8, 9},
{10, 11, 12}
}
};
Here:
- First dimension: 2 layers.
- Second dimension: 2 rows per layer.
- Third dimension: 3 columns per row.
- Total elements = 2 × 2 × 3 = 12.
To access the element 11, write:
a[1][1][1]
Example: C Program to display a three-dimensional array
#include
int main(void)
{
int a[2][2][3] = {
{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}
};
int i, j, k;
for (i = 0; i < 2; i++)
{
printf("Layer %d:\n", i);
for (j = 0; j < 2; j++)
{
for (k = 0; k < 3; k++)
{
printf("%d ", a[i][j][k]);
}
printf("\n");
}
}
return 0;
}
Output:
Layer 0:
1 2 3
4 5 6
Layer 1:
7 8 9
10 11 12
The three nested loops traverse the layers, rows, and columns respectively.
Difference between two-dimensional and multidimensional arrays
| Feature | Two-dimensional array | Three-dimensional array |
|---|---|---|
| Dimensions | 2 | 3 |
| Declaration | int a[2][3]; | int a[2][3][4]; |
| Indexes needed | 2 | 3 |
| Total elements | Rows × columns | Layers × rows × columns |
| Example application | Matrix of numbers | Multiple matrices or layered data |
Index range checking
C does not automatically check whether array indexes are within bounds. The programmer must ensure that every index is valid.
For example:
int a[2][3];
Valid indexes:
- Row: 0 to 1
- Column: 0 to 2
This is valid:
if (i >= 0 && i < 2 && j >= 0 && j < 3)
{
printf("%d", a[i][j]);
}
This is invalid:
printf("%d", a[2][0]); // Row index is out of range
Accessing an element outside the array bounds causes undefined behavior.
Practice questions
- Define a two-dimensional array. Explain its declaration, initialization, and accessing elements with an example.
- Write a C program to read and display a matrix using a two-dimensional array.
- What is a multidimensional array? Explain three-dimensional arrays with a suitable program.
- Differentiate between one-dimensional, two-dimensional, and multidimensional arrays.
- Explain index range checking for two-dimensional arrays with an example.