0% found this document useful (0 votes)
52 views

Document

This C program performs insertion and deletion operations on a one-dimensional array. It uses functions to insert an element into the array at a given position, delete an element from a given position, and delete an element with a given value. The main function uses a menu to allow the user to choose between these operations and prompts the user for the required inputs to call the appropriate function.

Uploaded by

PRAVEEN G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Document

This C program performs insertion and deletion operations on a one-dimensional array. It uses functions to insert an element into the array at a given position, delete an element from a given position, and delete an element with a given value. The main function uses a menu to allow the user to choose between these operations and prompts the user for the required inputs to call the appropriate function.

Uploaded by

PRAVEEN G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*program to perform operation insert and delete one dimensional array*/

#include<stdio.h>

Void insert(int arr[],int n,int pos,int x);

Void delet(int arr[],int n,int position);

Void delval(int arr[],int n,int x);

Int main()

Int array[100],position,n,cont=1,ch,x,I,c;

While(cont==1)

Printf(“1.insertion\n”);

Printf(“2.deletion by position\n”);

Printf(“3.deletion by value\n”);

Printf(“enter your choice\n”);

Scanf(“%d”,&ch);

Switch(ch)

Case 1:printf(“enter number of elements in array\n”);

Scanf(“%d”,&n);

Printf(“enter %d elements\n”,n);

For(i=0;i<n;i++)

Scanf(“%d”,&array[i]);

Printf(“enter the element to be inserted\n”);


Scanf(“%d”,&x);

Printf(“enter the location where you wish to insert elements\n”);

Scanf(“%d”,&position);

Insert(array,n,position,x);

Break;

Case 2:

Printf(“enter number of elements in array\n”);

Scanf(“%d”,&n);

Printf(“enter %d elements\n”,n);

For(c=0;c<n;c++)

Scanf(“%d”,&array[c]);

Printf(“enter the location where you wish to delet element\n”);

Scanf(“%d”,&position);

Delet(array,n,position);

Break;

Case 3:

Printf(“enter number of elements in array\n”);

Scanf(“%d”,&n);

Printf(“enter %d elements\n”,n);

For(i=0;i<n;i++)

Scanf(“%d”,&array[i]);

Printf(“enter the element to be found\n”);

Scanf(“%d”,&x);
Delval(array,n,x);

Break;

Default: printf(“wrong choice\n”);

Printf(“continue?(1 for yes,0 for no)\n”);

Scanf(“%d”,&cont);

Return 0;

/*------insert--------*/

Void insert(int arr[],int n,int pos,int x)

Int I;

Printf(“array before insert\n”);

For(i=0;i<n;i++)

Printf(“ %d”,arr[i]);

Printf(“\n”);

If((pos>0)&&(pos<=n))

/*increase the size by 1

N++;shift elements forward*/

For(i=n;i>pos-1;i--)
Arr[i]=arr[i-1];

/*insert x at pos*/

Arr[pos-1]=x;

Printf(“array after insert\n”);

For(i=0;i<=n;i++)

Printf(“ %d”,arr[i]);

Printf(“\n”);

Else

Printf(“position is out of boundry\n”);

/*------delet by pos--------*/

Void delet(int array[],int n,int position)

Int I;

If((position>n)||(position<=0))

Printf(“deletion not possile.\n”);

Else

For(i=position-1;i<n-1;i++)

Array[i]=array[i+1];

Printf(“resultant array.\n”);

For(i=0;i<n-1;i++)
Printf(“ %d\n”,array[i]);

/*-----delet by value--------*/

Void delval(int array[],int n,int x)

Int found=0,I,p;

For(i=0;i<n;i++)

If(x==array[i])

P=i+1;

Found=1;

Break;

If(found==1)

If((p>=n+1)||(p<=0))

Printf(“deletion not possible.\n”);

Else

For(i=p-1;i<n-1;i++)

{ array[i]=array[i+1];}

Printf(“resultant array:\n”);
For(i=0;i<n-1;i++)

Printf(“ %d\n”,array[i]);

Else

Printf(“element not found\n");

You might also like