What is an array? Discuss its type.

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

We can define an array in C as

int arr[50];

It means an array arr that has the capacity to hold 50 integers. The benefits of an array are, now we don’t need to write int variable_name 50 times to store the 50 integer variable. All elements hold by the Array is accessed by the index number. Index of an array starts from 0 to Size_of_Array-1, and each index has some value.

In the above example in array arr[50], arr[0] is the first element of this Array, arr[1] is second and ………….., arr[50] is the last element.

The memory address of the first element of an array is called the beginning/opening address.
We can store elements in an array in a different way.

Example 1:

int arr[50];
arr[0] = 06;
arr[1] = 22;
arr[2] = 30;
arr[3] = 56;
.
.
.
.
arr[49] = 11;

Example 2:

int array[5] = {10,12,44,21,54};

Example 3:

(int i = 0;i<50;i++){
scanf(“%d”, &arr[i]);
}

This loop will run 50 times and will store data in an array. We can print the elements of the array directly by accessing the index number.

printf(“%d”,arr[index_number]);

or

(int i = 0;i<100;i++){
printf(“%d”, arr[i];
}

1-Dimensional or single Dimensional Array

Declaration:- An array is declared with the bracket punctuators [ ], as shown in the following syntax:

Data_Type Array_Name[Number_Of_Elements] 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 either one by one or using a single statement as follows −

One by one example:

int arr[50];

arr[0]=12;

arr[1]= 43;

arr[2] = 14;

.

.

.

.

.

arr[49] = 54;

Or using single statement:

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

Program in C to implement One dimensional 

#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 &amp;amp;lt; 50; i++ ) {
      n[ i ] = i + 50; /* set element at location i to 
	  i + 50 */
   }   
   /* output each array element's value */
   for (j = 0; j &amp;amp;lt; 50; j++ ) {
      printf("Element[%d] = %d\n", j, n[j] );
   }
 
   return 0;
}

Two Dimensional Array or Matrix

We can define a two-dimensional array as an array of arrays. In Two Dimensional Array, elements of the Array are arranged in the form of rows and columns. This is also used for representing the Matrix. A Two Dimensional Array uses the two subscripts for declaring the elements of the Array.

For example :

int arr[5][5]

Above is the Example of the Two Dimensional Array where the first 5 represents the total number of Rows and the Second 5 represents the total number of Columns. And by multiplying this number of rows and columns, we can find the total number of elements that are stored by two-dimensional arrays.

Declaration of two dimensional Array in C

Data_Type Array_Name[rows][columns];

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 

rows: number of the rows 

columns: number of the columns

Consider the following example

int arr[5][4];

Here, 5 is the number of rows, and 4 is the number of columns.

Initialization of 2D Array in C

We can initialize a two-dimensional array in the following way.

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

Declaration and Initialization

int a[3][4] = {  
   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};

Program in C to implement Two-dimesional 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&amp;amp;lt;5;i++){    
 for(j=0;j&amp;amp;lt;4;j++){    
   printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);    
 }//end of j    
}//end of i    
return 0;  
} 

Multi-dimensional Arrays

We can define a Multi-dimensional array as an array of arrays. Two Dimensional arrays is also a multi-dimensional array. In a Multi-Dimensional Array, elements of an array are arranged in the form of rows and columns.
In multi-dimensional arrays, data are stored in tabular form, also known as in row-major order.

Declaring Multi-dimensional arrays

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

Initialization of the multidimensional 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 multidimensional 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 &amp;amp;lt; 2; ++i) {
        for (j = 0; j &amp;amp;lt; 3; ++j) {
            for(k = 0; k &amp;amp;lt; 4; ++k ) {
                scanf("%d", &amp;amp;amp;test[i][j][k]);
            }
        }
    } 
    printf("\nDisplaying the values of 3-D array:\n");
    for(i = 0; i &amp;amp;lt; 2; ++i) {
        for (j = 0; j &amp;amp;lt; 3; ++j) {
            for(k = 0; k &amp;amp;lt; 4; ++k ) {
                printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
            }
        }
    }
    return 0;
}

Share On :