Multi-dimensional Arrays in C: A Comprehensive Guide
Explore the concept of multi-dimensional arrays in C, which allow you to efficiently store and manipulate data in a structured format.
Multi-dimensional Arrays in C
An array with one size value in square brackets is a one-dimensional array. Each element is identified by its index. In C, you can use more indices to create two, three, or more dimensions.
Declaration
Multi-dimensional arrays are declared with multiple sizes:
Syntax
type name[size1][size2]...[sizeN];
Example of a three-dimensional array:
Syntax
int threedim[2][3][4];
Two-dimensional Array in C
A two-dimensional array is an array of one-dimensional arrays, like a table with rows and columns. Each element is identified by its row and column indices, both starting at 0.
Declaration and Initialization
Declare and initialize a two-dimensional array:
Syntax
int arr[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Each row can be separately enclosed in curly brackets for readability:
Syntax
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
Example: Printing Elements
Print row and column indices of a 2D array:
Code
#include <stdio.h>
int main() {
int a[3][2] = { {1, 2}, {3, 4}, {5, 6} };
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
printf("a[%d][%d] = %d\n", i, j, a[i][j]);
}
}
return 0;
}
Output
a[0][0] = 1
a[0][1] = 2
a[1][0] = 3
a[1][1] = 4
a[2][0] = 5
a[2][1] = 6
Example: Printing as Matrix
Print a 2D array as a matrix:
Code
#include <stdio.h>
int main() {
int arr[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
printf("%4d", arr[i][j]);
}
printf("\n");
}
return 0;
}
Output
1 2 3 4
5 6 7 8
9 10 11 12
Three-dimensional Array in C
A three-dimensional array is an array of two-dimensional arrays, needing three indices: depth, row, and column.
Example: Printing 3D Array Elements
Print elements of a 3D array:
Code
#include <stdio.h>
int main() {
int arr[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
int i, j, k;
printf("Printing 3D Array Elements\n");
for (i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
for (k = 0; k < 2; k++) {
printf("%4d", arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
Output
Printing 3D Array Elements
1 2
3 4
5 6
7 8
Row-wise Sum of Elements
Calculate and display the sum of each row in a 2D array:
Code
#include <stdio.h>
int main() {
int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
int i, j, sum;
for (i = 0; i < 3; i++) {
sum = 0;
for (j = 0; j < 4; j++) {
sum += arr[i][j];
}
printf("Sum of row %d: %d\n", i, sum);
}
return 0;
}
Output
Sum of row 0: 10
Sum of row 1: 26
Sum of row 2: 42
Matrix Multiplication
Matrix multiplication involves multiplying rows of the first matrix by columns of the second matrix. The resulting matrix has the number of rows of the first matrix and the number of columns of the second matrix.
Example: Multiplying Two Matrices
Code
#include <stdio.h>
int main() {
int mat1[2][3] = { {1, 2, 3}, {4, 5, 6} };
int mat2[3][2] = { {7, 8}, {9, 10}, {11, 12} };
int mat3[2][2] = {0};
int i, j, k;
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
for (k = 0; k < 3; k++) {
mat3[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
printf("Matrix 1:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", mat1[i][j]);
}
printf("\n");
}
printf("Matrix 2:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", mat2[i][j]);
}
printf("\n");
}
printf("Result of Multiplication:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", mat3[i][j]);
}
printf("\n");
}
return 0;
}
Output
Matrix 1:
1 2 3
4 5 6
Matrix 2:
7 8
9 10
11 12
Result of Multiplication:
58 64
139 154