Explain One and Multidimensional Array with Example

An array is a collection of similar data types (like int, float, or char), which takes memory in a contiguous fashion in Primary memory locations.

We can declare an array in C as
int arr[50];
Where arr is an array with can hold 50 elements of integer types.

Two Type of array:

1). Single/One Dimensional array

2). Multi-Dimensional Array

One Dimensional Array

Declaration of one dimensional array:

An array can be declared with the bracket punctuators [ ], as shown in the below syntax:

Data_Type Array_Name[Number_Of_Elements]

one dimensional array

The below example shows a declaration of an array having the capacity to hold 50 elements of type integers and the name of that array is arr :

int arr[50];

Initializing the array:

We can initialize an array in C by assigning value to each index one by one or by using a single statement as follows −

Example 1 :

Assign one value each time to the array
int arr[50];

arr[0]=12;
arr[1]= 43;
arr[2] = 14;
.
.
.
.
.
arr[49] = 54;

Example 2:

Using a single statement

int arr[5] = {10,32,11,45,38};

Program in C to implement One dimensional array:

#include <stdio.h> 
int main () { 
   int n[50]; /* n is an array of 50 integers */
   int i,j; 
   /* initialize elements of array n to 0 */         
   for ( i = 0; i < 50; i++ ) {
      n[ i ] = i + 50; /* set element at location i to i + 50 */
   }   
   /* output each array element's value */
   for (j = 0; j < 50; j++ ) {
      printf("Element[%d] = %d\n", j, n[j] );
   } 
   return 0;
}

Multidimensional Arrays

We can say a Multidimensional array is an array of arrays. Two Dimensional arrays is also a multidimensional array. In a Multi-Dimensional array, elements of an array are arranged in the form of rows and columns.
Multidimensional array stores elements in tabular form which is also known as in row-major order or column-major order.

Declaration of Multi-dimensional Array

data_type array_name[size1][size2]….[sizeN];

data_type: It defines the type of data that can be held by an array. Here data_type can be int, char, float. etc.

array_name: Name of the array

size1, size2,… ,sizeN: Sizes of the dimensions

Pictorial representation of a multidimensional array

number_of_arrays: number of arrays which flow in the z-axis

rows: number of the rows in the y-axis

columns: number of the columns flow in the x-axis

Two-dimensional array

int arr1[10][20];

two dimensional array

Three dimensional array

int arr2[10][20][30];

three dimensional array

Initialization of the Multidimensional array

Two-dimensional array

int arr[5][4]={
	{1,2,3},
	{2,3,4},
	{3,4,5},
	{4,5,6},
	{5,6,7}
	}; 

Program in C to implement Two dimensional Array

#include<stdio.h> 
int main(){      
int i=0,j=0;    
int arr[5][4]={{1,2,3},{2,3,4},{3,4,5},{4,5,6},{5,6,7}};     
//traversing 2D array    
for(i=0;i<5;i++){    
 for(j=0;j<4;j++){    
   printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);    
 }//end of j    
}//end of i    
return 0;  
}    

Three Dimensional Array

int test[2][3][4] = { 
                     { {5, 6, 1, 9}, {10, -3, 91, 61}, {73, 32, 83, 22} },
                     { {13, 43, 56, 36}, {25, 9, 93, 55}, {39, 21, 74, 6} }
                 };

Program in C to implement Three-dimensional array

#include <stdio.h>
int main()
{
    int i, j, k, test[2][3][4];
    printf("Enter 24 values for 3-D array: \n");
    for(i = 0; i < 2; ++i) {
        for (j = 0; j < 3; ++j) {
            for(k = 0; k < 4; ++k ) {
                scanf("%d", &test[i][j][k]);
            }
        }
    } 
    printf("\nDisplaying the values of 3-D array:\n");
    for(i = 0; i < 2; ++i) {
        for (j = 0; j < 3; ++j) {
            for(k = 0; k < 4; ++k ) {
                printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
            }
        }
    }
    return 0;
}

Share On :