Multi-dimensional Arrays in C
Multi-dimensional Arrays in C
The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant order.
Syntax
The general form of a multidimensional array can be written as;
datatype array_name [size 1] [size 2] [size 3] … [size N]; Or datatype array_name [number of tables] [size_of_row][size of column];
Where;
- Data type:It is the data type present in the C standard in which the array elements will be stores. Any data type in the C standard is applicable such as, int, float, double, and so on.
- array_name: It is the name of the array which is user specified.
- size 1, size 2, size 3,... size N: These are the size of the dimension which will represent an array.
- Number of tables: It is the total number of tables an array can accept such as, a 2D array can consist of up to single table while other multidimensional might have more of it.
- size_of_row: It represents the elements a row can store.
- size_of_column: It represents the elements a column can store.
Consider a three-dimensional array, and it can be represented as;
int threeD [5][6][3];
A two-dimensional array is represented as;
int twoD [5][6];
The total elements that will be stored in a multidimensional array can be calculated by just multiplying the size of all the dimensions that the user will declare.
E.g.:
int threeD [5][6][3];
The above-mentioned array can store a total of (5 * 6 * 3) elements, i.e., 90 elements.
In the same manner, an array int a[10][20] can store a total of (10 * 20) elements, i.e., 200 elements.
Two-dimensional array
It is one of the simplest forms of the multidimensional array. A two-dimensional array or a 2D array is a list of a one-dimensional array. Hence to declare a 2D array integer array of size [m][n], the declaration would be something like; int twoDimensional [m][n]; .
A two-dimensional array can also be called as a matrix.There are two ways of initializing a two-dimensional array during the time of declaration.
int twoDim [3][3] = { {2, 5, 6}, {4, 1, 9}, {3, 4, 8} };
Or
int twoDim[3][3] = { 2, 5, 6, 4, 1, 9, 3, 4, 8};
The above two declarations are the ones where the values are decided while writing the program itself. In order to make that user input, for loops can be used and then can be displayed and more operations on it can be performed.
E.g.:
#include<stdio.h> int main() { /* 2D array declaration*/ int disp[2][3]; /*Counter variables for the loop*/ int i, j; for(i=0; i<2; i++) { for(j=0;j<3;j++) { printf("Enter value for disp[%d][%d]:", i, j); scanf("%d", &disp[i][j]); } } //Displaying the array elements printf("Two Dimensional array elements:\n"); for(i=0; i<2; i++) { for(j=0;j<3;j++) { printf("%d ", disp[i][j]); if(j==2) { printf("\n"); } } } return 0; }
Output
Three-dimensional array
Initializing a three-dimensional array is as same as that of the two-dimensional array. Hence, the difference is that the number of dimensions increases so that the number of nested braces will also increase.
Syntax
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
While accessing the elements in the 3D(three dimensional) array, which is mostly similar to the 2D array, one has to make use of the three loops instead of the two loops for one additional dimensional in the 3D array.
E.g.:
// C Program to store and print 12 values entered by the user #include <stdio.h> int main() { int test[2][3][2]; printf("Enter 12 values: \n"); for (int i = 0; i< 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 2; ++k) { scanf("%d", &test[i][j][k]); } } } // Printing values with proper index. printf("\nDisplaying values:\n"); for (int i = 0; i< 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 2; ++k) { printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]); } } } return 0; }