Tuesday, July 2, 2013

C PROGRAM FOR INSERTION, DELETION TESTING IN AN ARRAY USING FUNCTION

/*PROGRAM FOR INSERTION, DELETION TESTING IN AN ARRAY USING FUNCTION*/

#include<stdio.h>
#include<conio.h>
int b,c,d,n,i,j,v,p,m,x;
int a[100];
void insertion();
void deletion();
void main() {
clrscr();
printf("Enter array size:");
scanf("%d", &n);
printf("\nEnter values of array:\n");
for(b=0;b<n;b++) {
scanf("%d", &a[b]);
x=b+1; }
do {
printf("\nChoices are:\n");
printf("1: Insertion\n");
printf("2: Deletion\n");
printf("3: Display\n");
printf("4: Exit\n");
printf("\nEnter Your Choice:\n");
scanf("%d", &c);
switch(c) {
case 1: {
insertion();
break; }
case 2: {
deletion();
break; }
case 3: {
printf("\nThe values in the array are:");
for(d=0;d<x;d++)
printf(" %d ", a[d]);
break; }
}
}
while(c!=4);
getch();
}

void insertion() {
if(x!=n) {
printf("\nEnter insertion position 0 to n:(Enter 0 if array is empty and so on in serial number pattern):");
scanf("%d", &p);
printf("\nEnter insertion value:");
scanf("%d", &v);
for(i=x-1;i>=p;i--)
a[i+1]= a[i];
a[p]= v;
x=x+1; }
else
printf("Array is full"); }

void deletion() {
if(x!=0) {
printf("\nEnter deletion Position 0 to n:");
scanf("%d", &m);
for(j=m;j<n-1;j++)
a[j]= a[j+1];
x=x-1; }
else
printf("Array is empty"); }


/*OUTPUT:
       
Enter array size:3

Enter values of array:
1
2
3

Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
3

The values in the array are: 1  2  3
Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
2

Enter deletion Position 0 to n:1

Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
3

The values in the array are: 1  3
Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
1

Enter insertion position 0 to n:(Enter 0 if array is empty and so on in serial number pattern):1

Enter insertion value:9

Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
3

The values in the array are: 1  9  3
Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
4*/

No comments:

Post a Comment