Learn C Programming
Multidimensional Arrays
In C programming, a multidimensional array in C is an array that contains more than one dimension. It is also known as a โ2D array,โ โ3D array,โ and so on, depending on the number of dimensions it holds. Multidimensional arrays are used to store data in tabular form or in matrices.
Declaring Multidimensional Arrays:
To declare a multidimensional array in C, you need to specify the size of each dimension. The general syntax is as follows:
Syntax:
data_type array_name[size1][size2]...[sizeN];
Here, data_type represents the type of elements the array will hold, and size1, size2, โฆ, sizeN represent the size of each dimension. Keep in mind that the sizes must be positive integers.
For example, to create a 2D array to store integers with 3 rows and 4 columns:
For Instance:
int matrix[3][4];
Initializing Multidimensional Arrays:
You can initialize a multidimensional array during declaration using nested curly braces:
data_type array_name[rows][columns] = { {val1, val2, ..., valN},
{val1, val2, ..., valN}, ... };
Hereโs an example of a 2D array initialization:
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
Hereโs an example of a 3D array initialization:
int cube[2][3][4] = {
{ {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} },
{ {13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24} }
};
Accessing Elements of Multidimensional Arrays:
To access elements in a multidimensional array, you use the square bracket notation. For a 2D array, youโll need to provide two indices: one for the row and one for the column.
int element = matrix[row_index][column_index];
For Instance:
int value = matrix[1][2];
/*Accesses the element in the second row and
third column (value = 7).*/
Multidimensional Array as Function Parameters:
When passing a multidimensional array to a function, you need to specify the number of columns (except for the first dimension) explicitly. This is because arrays decay into pointers when passed to functions.
For a 2D array, the function parameter looks like this:
For Instance:
void function_name(data_type array_name[][columns], int rows) {
// Function code
}
Iterating through Multidimensional Arrays:
You can use nested loops to traverse a multidimensional array:
For Instance:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
// Access and process matrix[i][j] here
}
}
Examples:
Example 1: Sum of elements in a 2D array
#include <stdio.h>
int main() {
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int rows = 3;
int columns = 4;
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sum += matrix[i][j];
}
}
printf("Sum of elements: %d\n", sum);
return 0;
}
Output:
Sum of elements: 78
Example 2: Function to print a 2D array
#include <stdio.h>
void printMatrix(int array[][3], int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
}
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int rows = 3;
int columns = 3;
printMatrix(matrix, rows, columns);
return 0;
}
Output:
1 2 3
4 5 6
7 8 9
Example 3: Sum of elements in a 3D array
#include <stdio.h>
int main() {
int cube[2][3][3] = {
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
},
{
{10, 11, 12},
{13, 14, 15},
{16, 17, 18}
}
};
int depth = 2;
int rows = 3;
int columns = 3;
int sum = 0;
for (int d = 0; d < depth; d++) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sum += cube[d][i][j];
}
}
}
printf("Sum of elements: %d\n", sum);
return 0;
}
Output:
Sum of elements: 171
Conclusion:
So, multidimensional arrays in C provide a way to organize and store data in multiple dimensions, making them suitable for handling matrices and other tabular data. Therefore, understanding how to declare, initialize, access, and work with multidimensional arrays is an essential skill for C programmers.