An array Data structure or Arrays is a linear type of data structure. It is used to hold a collection of an element.

What is an Array Data Structure?

Array Definition: Array is a set of similar type of data. Array only hold a homogeneous collection of data. An array stored in a systematic order is given and sequential index. Array index starts with zero. Each array element can be accessed by its index. It stores a fixed number of data.

Example: Suppose we have 5 numbers (similar type integers) are stored in array1.

Array Data Structure
Array Data Structure

As we see array1 making array for 5 elements and the index value of an element in an array will be from 0 to 4 is started continuously and its index starts with 0.

Let us see an array data structure in detail.

In another way, Array holds a collection of data in one single common name.
Before using array we need to declare the array.

How to declare an array?

Array Declaration: To declare array we need to specify the type of data we want to store, the name and size of the array.

The syntax for array declaration:

Data_type array_name[Areay_size];

Example :

To store 5 integer numbers
int num[5];

Array Declaration
Array Declaration

Note:- Once we declare an array then size and type of array cannot be changed.

How to initialize an array?

Array Initialization: This operation assigns a value to an array. It is also known as array initialization.

Syntax to initialize the array:

Array_name[0]=value;
In this way, we can assign value to each array using its index.

Example:-

num[0]=10;
num[1]=20;
num[2]=30;
num[3]=40;
num[4]=50;

There is a another way to initialize an array
You can initialize array while you are declaring an array.

The syntax for Array initialization

You can specify size and initialize the array with values.

Data_type array_name[size]={value1,value2..valueN};
Or
Data_type array_name[]={value1,value2..valueN};

In this syntax it not contains size. Compiler considers size as we initialized array with specific numbers.

Example:-

int num[5]={10,20,30,40,50};
Or
int num[]={10,20,30,40,50};

Here we haven’t specified size. Still compiler considers size 5 as we initialized array with 5 numbers.

Accessing Array element

The array has its index. We can access an array element by its index.

Syntax to access array element

Array_name[index]

Example:-

int num[]={10,20,30,40,50};
var=num[0]

Access elements out of its bound!

Suppose we declared the array with 5 numbers

int num[]={10,20,30,40,50};

We can access any element within index 0 to 4. If we try to access element other than this then we will get an unexpected error.

Example: num[6]; // Unexpected array

Types of Array

There are different types of an array in Data structure which are as follow:

  1. One dimensional Array
  2. Multidimensional Array

One dimensional array

It is also known as a single-dimensional array. One dimensional array is a linear type of array.

Syntax to declare one-dimensional array

data_type array_name[size];

One dimensional array example

int num=[5]={1,2,3,4,5};

Program: One dimensional Array in C

#include<stdio.h>
int main()
{
int arr[5],i; // Initilizing array
printf("Enter array:");
for(i=0;i<5;i++) // taking array input
{
scanf("%d",&arr[i]);
}
printf("our array: \n");
for(i=0;i<5;i++)
{
printf("%d\t",arr[i]); // Printing our array
}
return 0;
}
Output:-
One dimensional array
One dimensional array

Multidimensional array

You can create an array of arrays is known as a multidimensional array. we create a two-dimensional array or we can create a three-dimensional array.

Two-dimensional array

The two-dimensional array is also known as a 2D array or matrix. It is an array of array. A matrix can be represented as a collection of rows and columns.

Syntax to declare two dimensional array

data_type array_name[rows][cols];

Two dimensional array example

int Num[2][4];

Initialize two dimensional array

int num={ {1,2,3,4}, {5,6,7,8} };

The num is a two dimensional array which holds 8 values. Array is table consist of 2 rows and 4 columns.

Two dimensional array
Two dimensional array
Program: Two-dimensional array in c
#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 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:-
Two dimensional array
Two dimensional array

Three-dimensional array

As we created two-dimensional array similarly we can create a three-dimensional array.

Syntax to declare three dimensional array

data_type array_name[][][];

Example:

int num[2][4][3];
The above num will store 24 values.

Initialization of three dimensional array

you can initialize a three dimensional array in a similar way like a two dimensional array.
int num[2][4][3]={ {{ {3,4,5,6}, {4,5,6,1}, {22,4,2,7}}, {{9,7,8,44}, {4,6,5,6}, {3,3,2,2}}};

The operation performed on an array

  • Adding element
  • Deleting element
  • Sorting element
  • Searching element
  • Re-arranging element
  • For performing a matrix operation

Application of array in Data structure

  • Sorting Elements
  • CPU Scheduling
  • Recursive Function

Advantages of array

  1. Collection of a homogeneous element.
  2. It has an index.
  3. We can access any element by index.
  4. To implement data structure like a stack, queue.

Disadvantages of array

  1. We must know how many elements to store.
  2. An array has a fixed size that is memory allocated to an array cannot be increased or reduced.
  3. If we allocated more memory then we are using then memory will be wasted.
  4. It is stored in a consecutive memory location so insertion and deletion take more time.

Write A Comment