There are two types of array categories in C language –
— One-dimensional Array
— Multi-dimensional Array
One dimensional array is also known as a linear array. it has only one row with multiple columns.
dataType array_variable_name[MAXIMUM_COLUMN_COUNT];
1 2 3 4 5 |
int arr_num[5]={10,20,30,40,50}; Or int arr_num[]={10,20,30,40,50}; |
Above array is a one-dimensional array because it has only one row but 5 columns. I have already mentioned the one-dimensional array in Array Introduction.
However, C allows many dimensional arrays like 2-D,3-D,4-D……n-D as per the compiler support but here I am explaining only 2 types of Multi-dimensional array.
— Two-Dimensional Array
— Three-Dimensional Array
A two-dimensional array is an array of one-dimensional array.
Two-dimensional array expands along both direction (x and y) i.e it has multiple row and columns.
dataType array_variable_name[ROW_COUNT][COLUMN_COUNT];
OR
dataType array_variable_name[ ][COLUMN_COUNT];
In the second Syntax if we know the column count in one row then we can calculate row count but vice-versa is not possible.
like if COLUMN_COUNT=5 and total element Count=20 then
ROW_COUNT= COLUMN_COUNT / 5 =4
1 2 3 4 5 6 7 8 9 |
int arr_num[2][5]={10,20,30,40,50,60,70,80,90,100}; Or int arr_num[][5]={10,20,30,40,50,60,70,80,90,100}; or int arr_num[2][5]={{10,20,30},{40,50,60}} |
For E.g We can access element of 1st row and 0th column like this –
Write a program to display n x m matrix where n is the row and m is the column of the matrix. a user will enter the desired number of row and column at runtime.
Suppose user enters n=2 and m=3 then matrix must represent like this – where Matrix[n][m]= any user input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <stdio.h> int main() { long int matrix[20][20]={0}; /*i have taken maximum row and column,user can take less than this*/ unsigned int row_Index,col_Index; unsigned int row_count,col_count; printf("\nProgram to display a Matrix of n * m"); printf("\nEnter matrix row count : "); scanf("%u",&row_count); printf("\nEnter matrix column count : "); scanf("%u",&col_count); for(row_Index=0;row_Index<row_count;row_Index++) { for(col_Index=0;col_Index<col_count;col_Index++) { printf("\nEnter matrix elements matrix[%u][%u] : ",row_Index,col_Index); scanf("%ld",&matrix[row_Index][col_Index]); } } printf("\nHere is your Enterd Matrix\n"); for(row_Index=0;row_Index<row_count;row_Index++) { for(col_Index=0;col_Index<col_count;col_Index++) { printf("%ld\t",matrix[row_Index][col_Index]); } printf("\n"); } return 0; } |
Output:
A three-dimensional array is an array of two-dimensional array.
A three-dimensional array has 2-dimensional blocks in all 3 directions(x, y, z).
Syntax
dataType array_variable_name[2D_Block_Count][ROW_COUNT][COLUMN_COUNT];
2D_Block_Count: Total number of 2D array Count
ROW_COUNT: Row count in each block of 2D array
COLUMN_COUNT: Column Count in each block of 2D array
1 2 3 4 5 6 7 8 9 10 11 12 |
int arr_num[2][2][3]={10,20,30,40,50,60,70,80,90,100,110,120}; or int arr_num[2][2][3]= { {10,20,30}, {40,50,60}, {70,80,90}, {100,110,120} } |
In above example-
2D_Block_Count: 2
ROW_COUNT: 2
COLUMN_COUNT : 3
In above example, you can see that there are two sets of the two-dimensional array.
We can access elements at any index of 3-D array like this –
Write a program to display b x n x m 3-D matrix where b is the 2-d block count, n is the row and m is the column of each 2-d matrix. a user will enter the desired number of blocks, row, and column at runtime.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <stdio.h> int main() { long int matrix[20][20][20]={0}; /*i have taken maximum row and column,user can take less than this*/ unsigned int block_Index,row_Index,col_Index; unsigned int block_count,row_count,col_count; printf("\nProgram to display a Matrix of b* n * m"); printf("\nEnter matrix 2-D block count : "); scanf("%u",&block_count); printf("\nEnter matrix row count : "); scanf("%u",&row_count); printf("\nEnter matrix column count : "); scanf("%u",&col_count); for(block_Index=0;block_Index<block_count;block_Index++) { for(row_Index=0;row_Index<row_count;row_Index++) { for(col_Index=0;col_Index<col_count;col_Index++) { printf("\nEnter matrix elements matrix[%u][%u][%u] : ",block_Index,row_Index,col_Index); scanf("%ld",&matrix[block_Index][row_Index][col_Index]); } } } printf("\nHere is your Enterd Matrix\n"); for(block_Index=0;block_Index<block_count;block_Index++) { for(row_Index=0;row_Index<row_count;row_Index++) { for(col_Index=0;col_Index<col_count;col_Index++) { printf("%ld\t",matrix[block_Index][row_Index][col_Index]); } printf("\n"); } printf("\n"); } return 0; } |
Output:
So here I have tried to cover all of the basics of the one-dimensional and multi-dimensional array and I will discuss the use of pointers and function in the array in upcoming chapters .you must see the example section to get your concepts more clear.