Multidimensional Array in C
Single dimensional array only stores single data or information like marks of the student. But in some cases, you have to store complex data which have rows and columns. So, a single dimensional array can’t have this type of feature. That’s when a multidimensional array comes into play. You can manipulate the arrays by rearranging the elements by using functions like reshape, squeeze etc.
What is a Multidimensional array in C?
To put it short, with the help of a multidimensional array, you can create an array of arrays. In multidimensional arrays, data is stored in tabular format. Multidimensional arrays in C are used in computer research and analysis.
Types of C Multidimensional Array:-
Below are the types of multidimensional arrays:-
1. Two Dimensional Array
2. Three Dimensional Array
3. Four Dimensional Array
Declaring multidimensional arrays in C
1. Two Dimensional Array:-
datatype name[size_1][size_2];
Example:-
int num1[2][3]; float num2[5][6];
2. Three Dimensional Array:-
datatype name[size_1][size_2][size_3];
Example:-
int students[2][3][4]; float bills[3][4][3];
3. N-Dimensional Array:-
General Declaration of multidimensional array:-
type name[size1][size2]...[sizeN];
1. type:– It denotes data type names like int, float etc.
2. name:– It denotes the name of the multidimensional array.
3. row-size:– Number of row elements.
4. column-size:- Number of column elements.
Implementation of Multidimensional Array in C
1. Two Dimensional Array in C
Two Dimensional arrays are implemented using rows and columns. A 2-d array is a collection of 1-d arrays. Default format is row-major. It is the simplest form of multidimensional array.
Syntax:-
datatype name[size_1][size_2];
Example:-
int num1[2][3];
In the above example, 2 is the number of rows and 3 is the number of columns.
Initialization of two dimensional array in C:-
There are two ways in which you can initialize a two dimensional array:-
Method 1:-
int x[2][4] = {1 ,2 ,3 ,4 , 5 , 6 , 7 , 8}
Above array has 2 rows and 4 columns. And the elements will be stored from left to right.
Method 2:-
int x[2][4] = {{1,2,3,4}, {5,6,7,8}};
This method is a better one. Because it uses nested braces. In this, each set of inner braces represents one row. In the above array, there are 2 rows that’s why there are 2 inner braces.
Some other examples of initialization:-
int m [2][3] = {{5,3} {4,7} {1,4} {10,4} {9,4}}; int c[2][3] = {{2, 5, 0}, {-3, -5, 8}}; int c[2][3] = {{1, -3, 1}, {1, 7, 9}}; int c[2][3] = {1, -3, 1, -1, 7, 3};
Note:- It is very important to assign the second index so that the compiler can understand about the ending and start of the row.
Memory allocation of 2D Array:-
Column 0 | Column 1 | Column 2 | |
Row 0 | [0][0] | [0][1] | [0][2] |
Row 1 | [1][0] | [1][1] | [1][2] |
Row 2 | [2][0] | [2][1] | [2][2] |
You can calculate the number of elements in the array by multiplying the number of rows and columns. From the above table, you can clearly see that 3 rows and 3 columns are present. So, the number of elements will be 9.
Suppose, you have a two dimensional array like this x[i][j]. Here, i is the row number and j is the column number.
Accessing elements of two-dimensional array:-
In a 2d array, elements are accessed using rows and columns.
Example:- Accessing elements of 2d array
#include<stdio.h> int main() { // array of 3 rows and 4 columns! int x[3][4] = {{1,2,-3}, {7,3,8}, {-1,7,5}}; printf("TechVidvan Tutorials: Accessing elements of 2d array!"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++){ printf("Value of Element[%d][%d]: %d\n",i,j,x[i][j]); } } return 0; }
Output
Value of Element[0][1]: 2
Value of Element[1][0]: 7
Value of Element[1][1]: 3
Value of Element[2][0]: -1
Value of Element[2][1]: 7
In the above example, we have used nested for loops to output all the elements of the array. One for loop is to traverse the rows and the other one is to traverse the columns.
Example:- Sum of two matrices
#include <stdio.h> int main() { int a[2][2]={{2,4},{4,8}}; int b[2][2]={{3,5},{6,-1}}; int final[2][2]; printf("Welcome to TechVidvan Tutorials..\n\n"); // adding two arrays for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { final[i][j] = a[i][j] + b[i][j]; } printf("Sum Of Matrix:\n"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("%d\t", final[i][j]); if (j == 1) printf("\n"); } return 0; }
Output
5 9
10 7
Three Dimensional Array:-
It is defined as an array of arrays. You need more than three dimensions to implement a 3d array. If you are a beginner then it will look messy to you. But once you keep practicing then it will get easier to understand.
Syntax:-
datatype name[size_1][size_2][size_3];
Example:-
int marks[2][3][2];
Here, the array named marks hold 12 elements.
1. Initialization of 3 dimensional array in C
3d array initialization is the same way as the initialization of two dimensional arrays. The only difference is that in a 3d array, the number of dimensions increases. Due to that, the number of nested braces will also increase.
Method 1:-
int x[2][2][2] = {9, 10, 5, 6, 7, 9, 2, 2};
Method 2:- (A Better one)
int x[2][2][2] = {{{9,10},{5,6}},{{7,9},{2,2}}};
Accessing elements in three dimensional array:-
Accessing elements in a three dimensional array is pretty much the same as a two dimensional array. The only difference is that you have to use three loops for the extra addition of a dimension in a three dimensional array.
Basic example of a three dimensional array:-
#include<stdio.h> int main() { int x[2][2][2] = {{{9,10},{5,6}},{{7,9},{2,2}}}; // implementing a 3d array! printf("TechVidvan Tutorial: Accessing elements of 3d array!"); for (int i = 0; i < 2; ++i){ for (int j = 0; j < 2; ++j){ for (int k = 0; k < 2; ++k){ printf("Value of Element[%d][%d][%d]: %d\n",i,j,k,x[i][j][k]); } } } return 0; }
Output
Value of Element[0][0][1]: 10
Value of Element[0][1][0]: 5
Value of Element[0][1][1]: 6
Value of Element[1][0][0]: 7
Value of Element[1][0][1]: 9
Value of Element[1][1][0]: 2
Value of Element[1][1][1]: 2
In the above example, we have used nested for loops to output all the elements of the array.
Example:- Sum of matrices in a 3d array
#include<stdio.h> int main() { int a[2][2][2]={{{9,1},{5,-6}},{{7,9},{2,5}}}; int b[2][2][2]={{{0,-9},{4,6}},{{2,9},{3,2}}}; int final[2][2][2]; printf("TechVidvan Tutorial: Sum of matrices in a 3d array!"); // adding 3d array for (int i = 0; i < 2; ++i){ for (int j = 0; j < 2; ++j){ for(int k=0; k<2; ++k){ final[i][j][k] = a[i][j][k] + b[i][j][k]; } } } printf("Sum Of Matrix:\n"); for (int i = 0; i < 2; ++i){ for (int j = 0; j < 2; ++j){ for (int k=0; k < 2; ++k) { printf("%d\t", final[i][j][k]); } } } return 0; }
Output
9 -8 9 0 9 18 5 7
Four-Dimensional Array in C
It is very difficult to implement. It is an array of three dimensional arrays. It’s a messy one.
Syntax:-
datatype name[size_1][size_2][size_3][size_4];
Example:-
int students [4][2][7][5];
Basic Example of 4d array in C:-
#include <stdio.h> int main() { int a, b, c, d, f; int s[2][2][2][2]; s[0][0][0][0] = 0; s[0][0][0][1] = 3; s[0][0][1][0] = 5; s[0][0][1][1] = 6; s[0][1][0][0] = 1; s[0][1][0][1] = 12; s[0][1][1][0] = -1; s[0][1][1][1] = 2; s[1][0][0][0] = 4; s[1][0][0][1] = 9; s[1][0][1][0] = 7; s[1][0][1][1] = 1; s[1][1][0][0] = 0; s[1][1][0][1] = 7; s[1][1][1][0] = -5; s[1][1][1][1] = 2; printf("TechVidvan Tutorial: Basic example of 4d array!"); for (a = 0; a < 2; a++) { for (b = 0; b < 2; b++) { for (c = 0; c < 2; c++) { for (d = 0; d < 2; d++) { printf("Value of Elements[%d][%d][%d][%d]: %d ", a, b, c, d, s[a][b][c][d]); printf("\n"); } } } } return 0; }
Output
Value of Elements[0][0][0][1]: 3
Value of Elements[0][0][1][0]: 5
Value of Elements[0][0][1][1]: 6
Value of Elements[0][1][0][0]: 1
Value of Elements[0][1][0][1]: 12
Value of Elements[0][1][1][0]: -1
Value of Elements[0][1][1][1]: 2
Value of Elements[1][0][0][0]: 4
Value of Elements[1][0][0][1]: 9
Value of Elements[1][0][1][0]: 7
Value of Elements[1][0][1][1]: 1
Value of Elements[1][1][0][0]: 0
Value of Elements[1][1][0][1]: 7
Value of Elements[1][1][1][0]: -5
Value of Elements[1][1][1][1]: 2
Summary
This was all about Multidimensional array in C. Most used multidimensional array in C is Two Dimensional Array. In these types of arrays, indices provide a key role as they define an element.
Four dimensional array is a messy one. It’s difficult to implement. Multidimensional arrays in C are used in computer research and analysis.