Advertisement

Array Operation : Insert, Delete, Display, Count using Functions

Array Operation : Insert, Delete, Display, Count using Functions

Array Operation: Insert, Delete, Display, Count using Functions

This program demonstrates a few operations like Insert data at the first position, Deleting data from the first position, inserting data at the last position, deleting data from the last position, deleting the data from the middle position, displaying the data, and counting the number of cells having data on a 1-dimensional array. 

This C program is very useful for the college students of BCA, MCA, and BTech in CSE & IT to understand how the 1D array works. 

All the operations are handled by individual functions. Commenting is done properly to understand how and what the code is doing. 

C Program for the Array Operations

#include <stdio.h>
#include <stdlib.h>
#define size 50 //defining the max size of the array
int arr[size]; //declaring the 1D array of Integer type
int cnt=0; //count variable
int menu(); //decleration of the menu function 
void insert_at_first(); //prototype declerations
void delete_at_first();
void insert_at_end();
void delete_at_end();
void delete_at_middle();
void display();
int main() //start of the main function
{
    int choice,n;
    do{ //starting of the infinite do-while loop
    choice=menu(); //choice variable calls the menu function
    printf("Choice = %d \n", choice); //displaying the choice value for simplicity
    switch(choice)
    {
        case 1: insert_at_first(); display(); break; //calling the corresponding functions
        case 2: insert_at_end(); display(); break;
        case 3: delete_at_first(); display(); break;
        case 4: delete_at_end(); display(); break;
        case 5: delete_at_middle(); display(); break;
        case 6: display(); break;
        case 7: printf("Count=%d \n",cnt); break;
        case 8: exit(0);
    }
    }while(1); //condition is always true
    return 0;
}
int menu() //definition of the menu function
{
    int choice;
    printf("\n1: Insert at first \n"); //displaying the menu options
    printf("2: Insert at end \n");
    printf("3: Delete at first \n");
    printf("4: Delete at end \n");
    printf("5: Delete at middle \n");
    printf("6: Display \n");
    printf("7: Count \n");
    printf("8: Exit \n");
    printf("Enter Your Choice : \n");
    scanf("%d",&choice);
    if(choice<1 || choice>8) //if the user choice is not in 1 to 8 then prompt the user
    {
        printf("\n Invalid choice, enter again! \n");
        return menu();
    }
    else
        return choice; //if the user choice is in the range then return the value
}
void insert_at_first()
{
    int i;
    if(cnt==size-1){ //if the array is full then we can't insert more data
        printf("\n Array is full, can't insert!");
        return;}
    else{
    for(i=cnt;i>=0;i--) //running the loop from count value to 0
        arr[i+1]=arr[i]; //assigning the current cell's data into the next cell position [right shift]
    }
    printf("\n Enter the value to insert at first \n");
    scanf("%d",&arr[0]); //entering the new data in the first cell
    cnt++; //increase the count value by 1
}
void delete_at_first()
{
    int i;
    if(cnt==-1){ //if count value is less than 0 then array is empty
        printf("\n Array is empty, nothing to delete!");
        return;}
    else{ //otherwise delete the first value
        printf("\n Deleted %d\n", arr[0]); //deleting the first cell's value
        cnt--; //decreasing the count value by 1
        for(i=0;i<=cnt;i++) //running the loop from 0 to count value
            arr[i]=arr[i+1]; //assigning the next cell's data into previous cell position [left shift]
    }
}
void insert_at_end()
{
    if(cnt==size-1){ //if array is full then you can't insert more
        printf("\n Array is full, can't insert!");
        return;}
    else
        printf("\n Enter the value to insert at end \n"); //prompting user to give value
        scanf("%d",&arr[cnt]); // assigning the value into the count'th cell of the array
        cnt++; //increase count value by 1
}
void delete_at_end()
{
    if(cnt==-1){ //if array has no data then nothing to delete
        printf("\n Array is empty, nothing to delete!");
        return;}
    else{
        printf("\n Deleted %d \n",arr[cnt-1]); //otherwise delete the last data
        arr[cnt-1]=0; //making the last cell's data as 0
        cnt--; //decrease the count value by 1
    }
}
void delete_at_middle() //delete at the middle position function definition
{
    int i,pos;
    printf("\n Enter the position number : \n"); //take the position from user
    scanf("%d",&pos);
    if(pos<0 || pos>cnt){ //check the position is valid or not
        printf("\n Invalid position! \n"); //if invalid position then again execute the function from start
        delete_at_middle();
    }
    else if(pos==0) //if the position is 0 then call the delete data from first position function
        delete_at_first();
    else if(pos==cnt-1) //if the position is last then call the delete data from end position function
        delete_at_end();
    else{
        printf("\n Deleted %d\n", arr[pos]); //otherwise delete the data from the user given position
        cnt--; //decrease the count value as well
        for(i=pos;i<=cnt;i++) //adjust the right sided values of the deleted position
            arr[i]=arr[i+1]; //assigning the next cell's data into previous cell position [left shift]
    }
}
void display() //display function definition
{
    int i;
    printf("\n Array: \n");
    for(i=0;i<cnt;i++) //run the loop from 0th position to count'th position
        printf("%d\t",arr[i]); //display all the values of the array
}

The output of the Program

Output of the Array operations from Turbo C


Post a Comment

0 Comments