### What is Array ?

Arrays in data structures help solve some high-level problems like the "longest consecutive subsequence" program or some easy tasks like arranging the same things in ascending order. The concept is to collect many objects of the same kind.

What Are Arrays in Data Structures?

An array is a linear data structure that collects elements of the same data type and stores them in contiguous and adjacent memory locations. Arrays work on an index system starting from 0 to (n-1), where n is the size of the array.

Why Do You Need an Array in Data Structures?

Let's suppose a class consists of ten students, and the class has to publish their results. If you had declared all ten variables individually, it would be challenging to manipulate and maintain the data.

If more students were to join, it would become more difficult to declare all the variables and keep track of it. To overcome this problem, arrays came into the picture.

What Are the Types of Arrays?

There are majorly two types of arrays, they are:

One-Dimensional Arrays:

You can imagine a 1d array as a row, where elements are stored one after another.

Multi-Dimensional Arrays:

These multi-dimensional arrays are again of two types. They are:

How Do You Declare an Array?

Arrays are typically defined with square brackets with the size of the arrays as its argument.

Here is the syntax for arrays:

1D Arrays: int arr[n];

2D Arrays: int arr[m][n];

3D Arrays: int arr[m][n][o];

How Do You Initialize an Array?

You can initialize an array in four different ways:

Method 1:

int a = {2, 3, 5, 7, 11, 13};

Method 2:

int arr[]= {2, 3, 5, 7, 11};

Method 3:

int n;

scanf(“%d”,&n);

int arr[n];

for(int i=0;i<5;i++)

{

scanf(“%d”,&arr[i]);

}

Method 4:

int arr;

arr=1;

arr=2;

arr=3;

arr=4;

arr=5;

How Can You Access Elements of Arrays in Data Structures?

Arrays_in_ds-array-access-img1

You can access elements with the help of the index at which you stored them. Let's discuss it with a code:

#include<stdio.h>

int main()

{

int a = {2, 3, 5, 7, 11};

printf(“%d\n”,a); // we are accessing

printf(“%d\n”,a);

printf(“%d\n”,a);

printf(“%d\n”,a);

printf(“%d”,a);

return 0;

}

In this “Arrays in Data structures” tutorial, you have seen the basics of array implementation, now you will perform operations on arrays.

What Operations Can You Perform on an Array?

Traversal

Insertion

Deletion

Searching

Sorting

Traversing the Array:

Traversal in an array is a process of visiting each element once.

Code:

#include<stdio.h>

int main()

{

int a = {2, 3, 5, 7, 11};

for(int i=0;i<5;i++)

{

//traversing ith element in the array

printf(“%d\n”,a[i]);

}

return 0;

}

Insertion:

Insertion in an array is the process of including one or more elements in an array.

Insertion of an element can be done:

At the beginning

At the end and

At any given index of an array.

At the Beginning:

Code:

#include<stdio.h>

int main()

{

int array, n,i, item;

printf("Enter the size of array: ");

scanf("%d", &n);

printf("\nEnter Elements in array: ");

for(i=0;i<n;i++)

{

scanf("%d", &array[i]);

}

printf("\n enter the element at the beginning");

scanf("%d", &item);

n++;

for(i=n; i>1; i--)

{

array[i-1]=array[i-2];

}

array=item;

printf("resultant array element");

for(i=0;i<n;i++)

{

printf("\n%d", array[i]);

}

getch();

return 0;

}