Operations on an Array in C

 

In Computer Programming,an Array,also known as a Vector or List,is one of the simplest data structure.Array holds equally sized data elements, generally of the same data type.In this individual elements are accessed by index(for ex arry[0],arry[2] here 0 and 2 are the index).

 

Intialization of arrays….

Type array_name[size]={list of values};

The values in the list are separated by commas(,separator {3,5,6}),for example the statement

Int number[3]={4,5,2};

Operation Performed on Arrays

$11)      Traversal—Processing each element in the array

$12)      Search-----Finding the location of an element with a given value

$13)      Insertion—Adding a new element to an array

$14)      Deletion---Removing an element from an array.

$15)      Sorting-----Organizing the elements in some order.

$16)      Merging---Combining two arrays into a single array.

$17)      Reversing—Reversing the elements of an array

Let us now see a program how to perform these operations on an Array.

#include<stdio.h>

#include<conio.h>

#define MAXNO 5

void inserrt(int *,int pos,int numbr);

void delet(int *,int pos);

void revrsee(int *);

void displly(int *);

void srcch(int *,int numbr);

int main()

                {

                                int arry[5];

                                system("cls");

               

                                inserrt(arry,1,11);

                                inserrt(arry,2,12);

                                inserrt(arry,3,13);

                                inserrt(arry,4,14);

                                inserrt(arry,5,15);

                                printf("Elements of an array..\n");

                                displly(arry);

               

                                delet(arry,5);

                                delet(arry,2);

                                printf("After deletetion...\n");

                                displly(arry);

                                inserrt(arry,2,222);

                                inserrt(arry,5,555);

                                printf("All the Elements(array) After insertion(new value)..\n");

                                displly(arry);

                                printf(" Elements After reversing(swapping)..\n");

                                displly(arry);

                                srcch(arry,222);

                                srcch(arry,666);

                                return 0;

                }

void inserrt(int *arry,int pos,int numbr)

                {

                                int ii;

                                for(ii=MAXNO-1;ii>=pos;ii--)

                                arry[ii]=arry[ii-1];

                                arry[ii]=numbr;

                }

void delet(int *arry,int pos)

                {

                                int ii;

                                for(ii=pos;ii<MAXNO;ii++)

                                arry[ii-1]=arry[ii];

                                arry[ii-1]=0;

                }

/*reverse the entire array elements*/

void revrsee(int *arry)

                {

                int ii;

                for(ii=0;ii<MAXNO/2;ii++)

                                {

                                                int temp=arry[ii];

                                                arry[ii]=arry[MAXNO-1-ii];

                                                arry[MAXNO-1-ii]=temp;

                                }

                }

void srcch(int *arry,int numbr) /*searches array for a given element*/

                {             

                                int ii;

                                for(ii=0;ii<MAXNO;ii++)

                                {

                                                if(arry[ii]==numbr)

                                                {

                                                                printf("The element %d is present at %dth at positionn.\n\n",numbr,ii+1);

                                                                return;

                                                }

                                }

                                if(ii==MAXNO)

                                                printf("OOh!Sorry! The element %d is not present iin the array.\n\n",numbr);

                }

void displly(int *arry)

                {

                                int ii;

                                for(ii=0;ii<MAXNO;ii++)

                                printf("%d\t",arry[ii]);

                                printf("\n");

                }

Explanation...

In this program we created an array arry which contains 5 ints.Then the base address of this array is passed to functions like void inserrt (),void delet(),void revrsee(),void srcch() to perform different operation on the array.

void inserrt(int *,int pos,int numbr);

The void inserrt() function takes 2 arguments(int *,int pos,int numbr), the position (pos) at which the new number has to be inserted and the number (numbr) that has to be inserted. In this function(insert()), first through a loop, we have shifted the numbers, from the specified position, one place to the right of their existing position. Then we have placed the number( numbr) at the vacant place.

void delet(int *,int pos);

This function(delet()) deletes(remove) the element present at the given position pos. While doing so, we have shifted the numbers placed after the position from where the number is to be deleted, one place to the left of their existing positions. The place that is vacant after deletion of an element is filled with 0(zero)

void revrsee(int *);

In revrsee() we have reversed the entire array by swapping the elements,like arr[0] with arr[4],arr[1] with arr[3] so on.

void displly(int *);

In this function the entire array is traversed. As the list is traversed the function displays the elements of the array.

void srcch(int *,int numbr);

This function(srcch()) searches the array for the specified number(single number). In this function, the 0th element has been compared with the given number numbr. If the element compared happens to be same then the function displays the position(0,5,4,2) at which the number is found. Otherwise, the comparison is carried out until either the list is exhausted or a match is found. If the match(number) is not found(the number which u want to search) then the function displays the relevant message.

Array Operations