0% found this document useful (0 votes)
997 views2 pages

Dsu 1st Practical

This C program allows the user to insert and delete elements from an array. The program initializes an array and prompts the user to either: 1) input initial data, 2) insert a new element at a specified location by shifting other elements, or 3) delete an element at a specified location by shifting elements. The program displays the array after each insertion or deletion and continues until the user chooses to quit.

Uploaded by

Chetan Badgujar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
997 views2 pages

Dsu 1st Practical

This C program allows the user to insert and delete elements from an array. The program initializes an array and prompts the user to either: 1) input initial data, 2) insert a new element at a specified location by shifting other elements, or 3) delete an element at a specified location by shifting elements. The program displays the array after each insertion or deletion and continues until the user chooses to quit.

Uploaded by

Chetan Badgujar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

Insertion and Deletion Operation of An Array

#include<stdio.h>
#include<conio.h>
int main()
{
int a[30], n=0, op,i,x,loc;
clrscr();
do
{
printf("\n \n1) Read initial data in the array:");
printf("\n2) Read insert \n3) delete\n4 quit:");
printf("\n Enter Your Choice:");

scanf("%d",&op);
switch(op)
{

case 1: printf("\n Enter no. of data:");


scanf("%d",&n);
printf("\n Enter %d elements:",n);
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
break;
case 2: printf("\n Enter a location from %d to %d:",1,n+1);
scanf("%d",&loc);
if(loc>=1 & loc<=n+1)
{
printf("\n Enter the data to be inserted:");
scanf("%d",&x);
for(i=n-1;i>=loc-1;i--)
a[i+1]=a[i];
a[loc-1]=x;
n++;
printf("\n Data after insertion:");
for(i=0;i<=n;i++)
printf("%d",a[i]);
}
else
printf("\n Invalid Location:");
break;

case 3: printf("\n Enter a location from %d to %d:",1,n);


scanf("%d",&loc);
{
for(i=loc;i<=n;i++)
a[i-1]=a[i];
n--;
printf("\n Data after deletion:");
for(i=0;i<=n;i++)
printf("%d",a[i]);
}
printf("\n Invalid Location:");
break;
}
}while(op!=4);
return (0);
}

You might also like