If you're following this ongoing C programming tutorial series, you'd be aware of the concept of arrays. To quickly refresh, arrays are used to store multiple values of the same type in continuous storage.
Multidimensional arrays in C
For example, the following is an integer array capable of storing 5 numbers.
int arr[5]
Any value stored in an array can be accessed easily using the array name and the corresponding index value. As indexes begin from 0, let's say if you want to access the second element in an array, you can do that in the following way:
arr[1]
The following program accepts 5 integers from user as input, stores them in an array, and then outputs them back to user.
#include <stdio.h>
int main()
{
int arr[5],i;
printf("Enter 5 integer values\n");
for(i=0;i<5;i++)
scanf("%d",&(arr[i]));
printf("You entered the following values:\n");
for(i=0;i<5;i++)
printf("%d\n",arr[i]);
return 0;
}
Now, this type of array is known as a single dimensional array. Yes, that means there also exist multi dimensional arrays - two dimensional arrays, three dimensional arrays, and so on. For example, following is a two dimensional array:
int arr[2][3]
You can visualize this array as a 2-D table of numbers with 2 rows and 3 columns - something like the following:
x x x
x x x
So there are a total of 6 elements this array can hold. It's worth mentioning that the total number of elements an array can hold can be easily calculated by multiplying the indices in the declaration of the array. For example, in case of 'arr', the capacity of the array can be calculated by doing 2x3, which equals 6.
Coming to the initialization part, a 2-D array like 'arr' can be initialized in the following way:
int arr [2][3] = {1,2,3,4,5,6}
As this above initialization makes it hard to visualize these values in a 2-D array, there's another (read: better) way that you can opt. Here it is:
int arr [2][3] = { {1,2,3}, {4,5,6} };
So now it's easy to visualize that numbers 1,2,3 are in one row, while 4,5,6 are in the other. Here you go:
1 2 3
4 5 6
As for how to deal with a 2-D array in C, following is a small program that accepts these 6 values from user, stores them in a 2-D array 'arr', and then finally outputs them back to user:
#include <stdio.h>
int main()
{
int arr[2][3],i,j;
printf("You are about to enter values for a 2x3 array\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter value to be stored at row %d and column %d :: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n You entered the following values:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\n Row %d and column %d = %d\n",i,j,arr[i][j]);
}
}
return 0;
}
And here's the output:
You are about to enter values for a 2x3 array
Enter value to be stored at row 0 and column 0 :: 1
Enter value to be stored at row 0 and column 1 :: 2
Enter value to be stored at row 0 and column 2 :: 3
Enter value to be stored at row 1 and column 0 :: 4
Enter value to be stored at row 1 and column 1 :: 5
Enter value to be stored at row 1 and column 2 :: 6
You entered the following values:
Row 0 and column 0 = 1
Row 0 and column 1 = 2
Row 0 and column 2 = 3
Row 1 and column 0 = 4
Row 1 and column 1 = 5
Row 1 and column 2 = 6
So that was some basic information about two dimensional arrays. What about 3-D arrays? Well, on the same lines, you can define and initialize three dimensional arrays as well. Here's an example:
int arr[2][3][4]
So how would one go about visualizing this array? Well, think of a three dimensional world (the world we live in), and then visualize three dimensions perpendicular to each other. That's how the three dimensions of this array fit in.
Carrying a capacity of 24 elements (2x3x4), this array can be initialized in the following way:
int x[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} } };
And here's a C program making use of a 3-D array:
#include <stdio.h>
int main()
{
int arr[2][3][4],i,j,k;
printf("You are about to enter values for a 2x3x4 array\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<4;k++)
{
printf("\n Enter value to be stored at arr[%d][%d][%d] :: ",i,j,k);
scanf("%d",&arr[i][j][k]);
}
}
}
printf("\n You entered the following values:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<4;k++)
{
printf("\n arr[%d][%d][%d] = %d\n",i,j,k,arr[i][j][k]);
}
}
}
return 0;
}
Conclusion
In this tutorial, we expanded upon our existing understanding of arrays by discussing the concept of multidimensional arrays. You are advised to try out examples used in this tutorial on your system (as well as create new ones) to get a better understanding of how these arrays work. In case of any doubt or query, leave a comment below.