In our last 2 tutorials about C programming, we have learned about arrays without multidimensional arrays. We learned about 1D arrays and how to use a 1D array also how to insert and delete an element from an array at any position.
Now we will learn about multidimensional array in C programming with examples.
After this post, you will be able to learn everything you need to know about all types of arrays. So, after learning this tutorial you can move to the problem-solving phase if you are following my guideline for contest programming. If you haven’t read my previous tutorial about insertion and deletion in an array with an example
you can read this.
Multidimensional Array And Its Uses:
A multidimensional array means an array consisting of multiple arrays. A 1D array is considered a row of elements. So, you can consider a multidimensional array as consisting of multiple rows having 1 or more columns. So, a multidimensional array is considered an array of arrays.
When you need to access elements in 2d form you can use a 2D array. When you need to access elements in 3 dimensionally then you should use a 3D array and so on.
Syntax to declare a 2D array: data type array_name[n][m];
Syntax to declare a 3D array: data type array_name[n][m][k];
So the number of elements in a 2D array is n*m, and the number of elements in a 3D array is n*m*k.
Now let’s see how elements in a 2D array are stored. See the following program to store and print a 2D array.
- #include<stdio.h>
- int main()
- {
- int n,m,i,j,k,l;
- scanf(“%d%d”,&n,&m);//taking the row and column size
- int arr[n][m];
- for(i=0; i<n; i++)
- {
- for(j=0; j<m; j++)
- {
- scanf(“%d”,&arr[i][j]); //storing each element at array in i-th row and j-th column
- }
- }
- printf(“Elements of the array are:n”);
- for(i=0; i<n; i++)
- {
- for(j=0; j<m; j++)
- {
- printf(“%d “,arr[i][j]);
- }
- printf(“n”);
- }
- return 0;
- }
Input:
4 5
12 23 43 12 11 23 45 65 31 34 56 78 98 91 21 2 3 4 5 143
Output:
12 23 43 12 11
23 45 65 31 34
56 78 98 91 21
2 3 4 5 143
Here, we are using a 2d array and named it arr consisting of n rows and m columns. Then we have used nested for loop 2 for loop actually to take the input and store them in the array arr. First for loop is used to indicate the row of the array and the second for loop is to indicate the column of the array. So, when we take input in arr[i][j], this means the element is stored at the i-th row and j-th column.
Just like how we take elements in a matrix. So, you can consider a 2d array as a matrix too.
The above input elements are stored in the array in the following manner:
Row column
|
0 |
1 |
2 |
3 |
4 |
0 |
12
|
23
|
43
|
12
|
11
|
1 |
23
|
45
|
65
|
31
|
34
|
2 |
56
|
78
|
98
|
91
|
21
|
3 |
2
|
3
|
4
|
5
|
143
|
As we know an array index is started from 0 instead of 1 that so why in our array elements will be stored in 0 to (n-1)-th indexed row and 0 to (m-1)-th indexed column. We have used another nested for loop to print the array arr. I hope you understand now how actually a 2d array is used.
You can easily access an array element in a 2d array at the i-th row and j-th column by typing arr[i][j].
The total number of elements in this array is 20 which is equal to 4*5 i.e. n*m.
The time complexity to access a 2D array is O(n*m). O(n2) if n=m.
Similarly see the program for a 3D array:
- #include<stdio.h>
- int main()
- {
- int n,m,i,j,k,p;
- scanf(“%d%d%d”,&n,&m,&k);//taking the number of elements in x,y,and z axis.
- int ar[n][m][k];
- for(i=0; i<n; i++)
- {
- for(j=0; j<m; j++)
- {
- for(p=0; p<k; p++)
- {
- scanf(“%d”,&ar[i][j][p]); //storing each element in array at (i,j,p) position.
- }
- }
- }
- printf(“Elements of the array are:n”);
- for(i=0; i<n; i++)
- {
- for(j=0; j<m; j++)
- {
- for(p=0; p<k; p++)
- {
- printf(“%d “,ar[i][j][p]);
- }
- printf(“n”);
- }
- printf(“n”);
- }
- return 0;
- }
Input:
3 2 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Output:
Elements of the array are:
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
As we can’t show you a 3d model in a white sheet that so why we can’t represent the 3D array here appropriately how it looks like. Just consider we are using 3 axes in geometry x, y, and z-axis. So, each elements are stored in the array ar in x,y,z position that is ar[i][j][p] position. For the given input we have 3 different sequences of elements. First, we have 3 rows. Each row has 2 other components. Let’s say 1 is the column and the other is the peak. So, the index i refer to row and index j refers to column and the index p prefers to peak. The 2 component of each row is shown in 2 lines in each row. Here column represents k and p represents as peak. See the following table for a better understanding:
(i,j)
HH
|
pFF
|
P=0
|
P=1
|
P=2
|
i=0 j=0
|
1
|
2
|
3
|
i=0 j=1
|
4
|
5
|
6
|
I tried to show the visual representation for the first row elements. I hope you understood it.
The number of elements in this array is 18=3*2*3=n*m*k.
So, the complexity to access a 3D array is O(n*m*k). O(n3) if n=m=k.
In this way, you can use any kind of multidimensional array. But we only need 1D and 2D arrays for the real-life explanation. Sometimes 3D array can be required.
Difference Between Single And Multidimensional Arrays:
The difference between 1D and multidimensional arrays is shown below.
Single dimensional array
|
Multidimensional array
|
A single dimensional array contains elements in
A single row.
|
A multidimensional array contains elements in nD format. Where n is the dimension of the array.
|
An example of a 1D array is arr[n].
|
Example of a 3D array is arr[n][m][k].
|
It has n number of elements so the time complexity of a 1D array is O(n).
|
The time complexity of a K dimensional array is O(nk).
|
Insertion and deletion at any position in a 1D array are easier compared to multidimensional arrays.
|
Insertion and deletion in a multidimensional array are more difficult than in a 1D array.
|
Represents the collection of data in a single row of the same type.
|
Represents multiple data of the same type in the form of “rows of row and column”.
|
Sorting and any type of operation are easier.
|
Sorting and any type of operation are difficult.
|
I hope this tutorial is helpful for you to get detailed knowledge about multidimensional arrays in c. So, I hope after learning the last 3 tutorials including this one you have learned everything you need to learn about array in c. Now, if you are following my tutorial sequentially to start your programming career create an account in any codding platform like
Codeforces,
Hackerrank,
UVA online judge, etc.
My recommendation is to create an account on Codeforces only now. In Codeforces at the beginning just solve the problems having a rating of 500 or below.
For multidimensional array practice, you can try to solve the following problems.