Multi-Dimensional Arrays in C
Multi-Dimensional Arrays in C
Multi-dimensional Arrays in C
The array is declared with one value of size in square brackets, it is called one
dimensional array. In a one dimensional array, each element is identified by its index
or subscript. In C, you can declare with more indices to simulate a two, three or
multidimensional array.
Multidimensional Arrays in C
Multi-dimensional arrays can be termed as nested arrays. In such a case, each
element in the outer array is an array itself. Such type of nesting can be upto any
level. If each element in the outer array is another one-dimensional array, it forms a
two-dimensional array. In turn, the inner array is an array of another set of one
dimensional array, it is a three-dimensional array, and so on.
type name[size1][size2]...[sizeN];
For example, the following declaration creates a three dimensional integer array −
int threedim[3][3][3];
Two-dimensional Array
Three-dimensional Array
Two-dimensional Array in C
A two-dimensional array in an array of one-dimensional arrays. Each element of a
two-dimensional array is an array itself. It is like a table or a matrix. The elements
can be considered to be logically arranged in rows and columns. Hence, the location
of any element is characterised by its row number and column number. Both row and
column index start from 0.
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm 1/9
6/16/24, 12:41 PM Multi-dimensional Arrays in C
The arr array has three rows and five columns. In C, a two dimensional array is a
row−major array. The first square bracket always represents the dimension size of
rows, and the second is the number of columns. Obviously, the array has 3 X 5 = 15
elements. The array is initialized with 15 comma−separated values put inside the
curly brackets.
Elements are read into the array in a row−wise manner, which means the first 5
elements are stored in first row, and so on. Hence, the first dimension is optional in
the array declaration.
To increase the readability, elements of each row can be optionally put in curly
brackets as follows −
1 2 3 4 5
10 20 30 40 50
5 10 15 20 25
The cell with row index 1 and column index 2 has 30 in it.
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm 2/9
6/16/24, 12:41 PM Multi-dimensional Arrays in C
The program below displays the row and column indices of each element in a 2D
array −
#include <stdio.h>
int main () {
return 0;
}
Output
a[0][0] = 0
a[0][1] = 0
a[1][0] = 1
a[1][1] = 2
a[2][0] = 2
a[2][1] = 4
a[3][0] = 3
a[3][1] = 6
a[4][0] = 4
a[4][1] = 8
Even though all the elements are stored in consecutive memory locations, we can
print the elements in row and column format with the use of nested loops.
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm 3/9
6/16/24, 12:41 PM Multi-dimensional Arrays in C
#include <stdio.h>
int main() {
int arr[3][5] = {1,2,3,4,5, 10,20,30,40,50, 5,10,15,20,25};
int i, j;
Output
1 2 3 4 5
10 20 30 40 50
5 10 15 20 25
Three-dimensional Array in C
A three-dimensional array is an array of two-dimensional arrays, where each
element is a two-dimensional array. A 3D array needs three subscripts to define
depth, row, and column.
Imagine the students appearing for an exam are seated in five halls, each hall
having twenty rows of desks, and each row has 5 tables. Such an arrangement is
represented by a three dimensional array such as −
Students[hall][row][column]
To locate each student, you need to have three indices, hall number, row and column
of his table in the hall.
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm 4/9
6/16/24, 12:41 PM Multi-dimensional Arrays in C
#include<stdio.h>
int main(){
int i, j, k;
int arr[3][3][3]= {
{
{11, 12, 13},
{14, 15, 16},
{17, 18, 19}
},
{
{21, 22, 23},
{24, 25, 26},
{27, 28, 29}
},
{
{31, 32, 33},
{34, 35, 36},
{37, 38, 39}
},
};
for(i=0;i<3;i++) {
for(j=0;j<3;j++){
for(k=0;k<3;k++){
printf("%4d",arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm 5/9
6/16/24, 12:41 PM Multi-dimensional Arrays in C
21 22 23
24 25 26
27 28 29
31 32 33
34 35 36
37 38 39
#include <stdio.h>
int main() {
int arr[3][5] = {{1,2,3,4,5}, {10,20,30,40,50}, {5,10,15,20,25}};
int i, j;
int sum;
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm 6/9
6/16/24, 12:41 PM Multi-dimensional Arrays in C
Output
Sum of row 0: 15
Sum of row 1: 150
Sum of row 2: 75
Matrix Multiplication
Matrix algebra is a branch of mathematics, where a matrix is a 2D array of numbers
arranged in rows and columns. Multiplication of two matrices is possible only if the
number of columns of the first matrix is equal to the number of rows of the second
matrix. The product of two compatible matrices is equal to the dot products between
rows of the first matrix and columns of the second matrix.
If the two matrices are of size a[m][n] and b[p][q], the result of their multiplication
is a matrix of the size (the multiplication is possible only if n is equal to p) is c[m]
[q].
The product of two matrices, is the sum of the products across some row of matrix A
with the corresponding entries down some column of matrix B.
#include<stdio.h>
int main(){
int mat1[3][3] = { {2, 4, 1} , {2, 3, 9} , {3, 1, 8} };
int mat2[3][3] = { {1, 2, 3} , {3, 6, 1} , {2, 4, 7} };
int mat3[3][3], sum=0, i, j, k;
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm 7/9
6/16/24, 12:41 PM Multi-dimensional Arrays in C
printf("\nMatrix 2 ...\n");
for(i=0; i<3; i++){
for(j=0; j<3; j++)
printf("%d\t", mat2[i][j]);
printf("\n");
}
return 0;
}
Output
Matrix 1 ...
2 4 1
2 3 9
3 1 8
Matrix 2 ...
1 2 3
3 6 1
2 4 7
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm 8/9
6/16/24, 12:41 PM Multi-dimensional Arrays in C
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm 9/9