About us

C Program for various operations in the Array

C Program for various operations in the Array


c-program-insert-delete-display-array


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insert();
void insert_position();
void del();
void delete_position();
void display();
int array[100],i=-1,size=0,data,pos;

int main()
{
  int choice;
  clrscr();
  do
  {
    printf("\nEnter 1 for insert element in array\n");
    printf("Enter 2 for insert element at middle in array\n");
    printf("Enter 3 for delete element from array\n");
    printf("Enter 4 for delete element from middle in array\n");
    printf("Enter 5 for display elements from array\n");
    printf("Enter 6 for exit\n");
    scanf("%d",&choice);

    switch (choice)
    {
      case 1:
      insert();
      break;
    
     case 2:
      insert_position();
      break;

      case 3:
      del();
      break;

      case 4:
      delete_position();
      break;

      case 5:
      display();
      break;

      case 6:
      exit(0);

      default:
      printf("\nEnter valid position\n");
    }
  }while(choice!=6);
 return 0;
}

void insert()
{
 printf("\nEnter data\n");
 scanf("%d",&array[++i],size++);
}

void insert_position()
{
 printf("\nEnter the position\n");
 scanf("%d",&pos);

 if(pos>size)
  printf("\nEntered position is out of range\n");
 else
 {
  printf("\nEnter data\n");
  scanf("%d",&data);
  for(i=size;i>=pos-1;i--)
   array[i+1]=array[i];
  array[pos-1]=data;
  size++;
 }
}

void del()
{
 if(size==0)
  printf("\nArray is Empty\n");
 else
  size--;
}

void delete_position()
{
 if(size==0)
  printf("\nArray is Empty\n");
 else
 {
  printf("\nEnter the position\n");
  scanf("%d",&pos);
  if(pos>size)
  {
   printf("\nEntered position is out of range\n");
   return;
  }
  for(i=pos-1;i<size;i++)
  array[i]=array[i+1];
  size--;
 }
}

void display()
{
 int i=0;
 if(size==0)
  printf("\nEmpty\n");
 else
 {
  printf("Element: [ ");
  while(i<size)
   printf("%d ",array[i++]);
  printf("]");
 }
}

OUTPUT

c-program-insert-delete-display-array


------------------------------------------------------------------------------------------------------------

You can also visit:-