# 5 Different Unique Operations on arrays

This article discusses the various operations on arrays in C and C++. As we know array is an important part of data structure and hence it is important to know the operations of arrays. Let’s get started!

## Introduction

First, let’s list the various operations that can be performed in an array. The operations include traverse, insertion, deletion, search, and sort. These are the basic operations on an array. Let’s see the algorithm and the code for the same in C and C++. We will discuss the code for C and the code for C++ is made to run as both differ only by syntax.

Before performing the operations let’s see how to declare and initialize an array.

## Declaring and Initialization of arrays

The declaration of an array is easy. Just, mention the data type and the size. See the below code to understand clearly.

Syntax : data_type variablename[size] ;

``````//declaring a simple array
int a;    // array of size 10 named 'a' with int type
char b;``````

Now let’s see how to initialize the array. It is also quite simple. Always remember that declaration and initialization are not the same.

``````//initialization an array
int a={1,3,89}
double b[]={67.2,2,78}  //array size is determined based on elements count ``````

Now let’s perform these operations on the arrays.

## Operations on arrays

### Traverse operations

Traversing an array means accessing the elements of an array. Simply put, it means visiting the elements of an array. A simple example is to print the array.

#### Algorithm

The algorithm is very simple. Just iterate the key-value index to access all the elements.

#### Code

``````#include<stdio.h>
void main()
{
int a[]={4,67,89,2,6};
int n=5;
printf("The elements are:");
for(int i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}``````

Run the code here

Run the code here for C++.

NOTE: Only the syntax differ for both C and C++. The code remains the same.

#### Code Explanation

1. First, initialize and declare the array with size.
2. Then run a for loop starting from 0 till size(n) using a variable i.
3. The value of the variable is incremented by 1 so as to access the next element.
4. Print the elements of the array inside for loop
5. Elements get printed.

### Insertion operations

Insertion operation is nothing but inserting an element into an array. Inserting can be done either at the front or at a specified index or at the end. This insertion is done with the help of the index value.

Let’s see the algorithm and the code for all the types of insertion.

#### Algorithm

The approach is that adding the elements at the specified value and changing the index value. At last, we need to change the size of the array. When we run the code you will get the idea still more clearly.

#### Code

``````//pgm for insertion operations
#include<stdio.h>
void main()
{
//declaring and initializing array
int a={1,4,2};
int n=3;     //element count

//insertion at front
int front=89;   //value to be added at front
printf("The elements before insertion:");
for(int i=0;i<n;i++)
printf("%d ",a[i]);
for(int i=n-1;i>=0;i--)
a[i+1]=a[i];
a=front;
n++;
printf("The elements after insertion at first:");
for(int i=0;i<n;i++)
printf("%d ",a[i]);

//insertion at last
int last=6;
//element to be added at last
a[n]=last;
n++;
printf("\nThe elements after insertion at last:");
for(int i=0;i<n;i++)
printf("%d ",a[i]);

//insertion at a index
int middle=55;    //element to be added in middle
int index=2;     //index value
for(int i=n-1;i>=index;i--)
a[i+1]=a[i];
a[index]=middle;
n++;
printf("\nThe elements after insertion in middle:");
for(int i=0;i<n;i++)
printf("%d ",a[i]);
}
``````

Run the code here

Run the code here in C++

#### Code Explanation

Insert at first

• After initializing and declaring the array, decide a value to insert at first
• Keep the number of elements in a variable called ‘n’.
• Now print the elements in the array to see how the program changes.
• Run a for loop to print the array.
• Now, to insert run a for loop from the value n-1 till 0. Also, decrease the iterative variable i by 1. This shifts the elements down by 1.
• Now change the value at index 0 to the desired value.
• Increase the elements count by 1 as a new element is added.
• Now run a for loop to print the elements. Insertion operation is done.

Insert at last

• Now decide a value to be inserted at last.
• Change the value of a[n] to the desired value. This adds the element at last.
• Increment the value of n.
• Print using a for loop to see the changes.

Insert at index

• Like the other insertions, decide the value to be inserted.
• We also need another one value – index. Here index and the positions are not the same. Position + 1 =index.
• So like the insertion at first we shift down the elements but from the index alone.
• To do this, run a for loop from n-1 till index. This is the only change here
• Print the array to see the change.

### Deletion Operations

#### Algorithm

The approach of the deletion is that traverse the array till the desired index. Delete the element and decrease the count of element values. Finally, change the index value accordingly.

#### Code

``````//pgm to delete element array
#include<stdio.h>
void main()
{
int a={23, 45, 67,1};
int n=4;  //element count
//deleting element- 45
printf("The elements before deletion :");
for(int i=0;i<=n-1;i++)
printf("%d ",a[i]);
int s=1;    //index value of deleting element(45)
for(int s=1;s<=n-1;s++)
a[s]=a[s+1];
n--;
printf("\nThe elements after deletion:");
for(int i=0;i<=n-1;i++)
printf("%d ",a[i]);
}
``````

Run the code here

Run the code here for C++

#### Code Explanation

• The first step is as usual declaring and initializing the array.
• Decide the element to be deleted, say 45
• Print the array using for loop.
• Store the index of the element in a variable. If you store the position then, store it as position-1.
• Now run a for loop from the index value till n-1.
• Inside the for loop, assign the values a[i]=a[i+1].
• Decrement the value of the count of elements as we deleted an element.
• Again print the array to see the change.

### Search Operations

Searching operation is looking for a specific element in the array. Finding out if it is present or not.

#### Algorithm

The approach is that traversing an array and checking for the element presence. Here changing any of the values is not required.

#### Code

``````//pgm for searching the element
#include<stdio.h>
void main()
{
int a={21, 34, 45,1};
int search=21;  //element to be searched
int n=4;
printf("\nThe elements of the array:");
for(int i=0;i<=n-1;i++)
printf("%d ",a[i]);
int found=0;
for(int i=0;i<=n-1;i++)
{
if(a[i]==search)
{
found=1;
break;
}
}
if(found==1)
printf("\nElement found");
elseAtr
} ``````

Run the code here

Run the code here for C++

#### Code Explanation

• First, create an array
• Store the value to be stored in variable ‘search’.
• Print the array if you need it.
• We need to keep a variable ‘found’. This acts as a flag.
• Run a for loop from 0 till n-1.
• Inside the for loop check for the condition using If statement. The condition a[index]==element needs to be checked. If the condition is met, the flag variable is incremented.
• Check for another condition. The element is present if flag==1.
• Else, the element is not present.
• Print the statement accordingly.

### Sorting Operations

The array we initialize may not be in sorted order. The values may be in any way. Sorting operations means making the elements either in ascending or descending order.

#### Algorithm

The basic approach for this is swapping of the elements with the first element of the array as reference. Then according to our wish, we can swap either in ascending or descending order.

#### Code

``````//pgm for sorting array
#include<stdio.h>
void main()
{
int a={12, 78,1, 10, 34};
int n=5;
int b=0;
printf("The elements are:");
for(int i=0;i<n;i++)
printf("%d ",a[i];

//ascending order
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
{
b=a[i];
a[i]=a[j];
a[j]=b;
}
}
printf("Sorted array in ascending order:");
for(int i=0;i<n;i++)
printf("%d ",a[i]);

//descending order
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
if(a[i]<a[j])
{
b=a[i];
a[i]=a[j];
a[j]=b;
}
}
printf("\nSorted array in descending order:");
for(int i=0;i<n;i++)
printf("%d ",a[i]);
}
``````

Run the code here

Run the code here for C++

#### Code Explanation

• Create an array as usual.
• Print the array using for loop.
• To sort the array for ascending order, run a for loop from range o till n.
• Run a nested for loop again, starting from i+1 till n. Here i+1 is to start the range from the next element.
• Check if the current element is greater than the next element. If it is greater, swap the elements using a third variable. Here we use ‘b’.
• Here swapping takes by assigning the greater element index to the third variable and the lesser element index is assigned as the current element index. And now the lesser element index becomes the third variable now. This is how the elements are swapped.
• The elements are swapped only if the condition is satisfied.
• Do this process till the end of the array.
• Now print the array. It will be in ascending order.
• To sort the array in descending order, follow the same process from the 1st for loop. However, only the condition changes. The condition is the current element should be lesser than the next element.
• Print the array to see the result.