Sunday 24 June 2012

A program fir insert, edit, display, bubble sort and delete


Credite: Jakir Hossain, Noman bhai.



#include<stdio.h>
long a[100];
int n=0;
void insertion()
{int i,l,m,h,k;
long j;
printf("1.New insertion\n2.Edit\nEnter your choice:");
scanf("%d",&k);
if(k==1)
{
s:printf("How many numbers do you want to add:");
scanf("%d",&m);
if(m<=100-n)
{
for(i=0;i<m;i++)
{
printf("Insert the value:");
scanf("%ld",&j);
a[n]=j;
n=n+1;
printf("The number is successfully added.\n");
}
}
else
{
printf("There is not enough space for %d numbers.Available space is %d\n",m,100-n);
goto s;
}
}
else if(k==2)
{
r:printf("How many numbers do you want to add:");
scanf("%d",&m);
if(m<=100-n)
{
for(i=0;i<m;i++)
{
printf("Enter the desired position:");
scanf("%d",&l);
while(l>n)
{
printf("Out of range\n");
printf("Enter the desired position:");
scanf("%d",&l);
}
printf("Insert the value:");
scanf("%ld",&j);
for(h=n-1;h>=l-1;h--)
{
a[h+1]=a[h];
}
a[l-1]=j;
n=n+1;
printf("The number is successfully added.\n");
}
}

else
{
printf("There is not enough space for %d numbers.Available space is %d\n",m,100-n);
goto r;
}
}
}

void deletation()
{
int h,k;
{
printf("Insert the desired position:");
scanf("%d",&h);
while(h>n)
{
printf("the desired position is out of range.\n");
printf("Insert the desired position:");
scanf("%d",&h);
}
for(k=h-1;k<n;k++)
{
a[k]=a[k+1];
}
n=n-1;
printf("The number is successfully deleted.\n");
}
}
void bobble()
{
int i,j,k,l;
printf("In which order do you want to sort the numbers?\nPress '1' for Accending\npress '2' for Decending\nEnter your choice:");
scanf("%d",&l);
if(l==1)
{
for(i=1;i<=n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
}
}
printf("The numbers are successfully sorted in accending order :)\n");
}
else if(l==2)
{
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if (a[j]<a[j+1])
{
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
}
}
printf("The numbers are successfully sorted in decending order :)\n");
}
}
void display()
{
int i=0;
while(i<n)
{
printf("%ld ",a[i]);
i++;
}
printf("\n\n");
}
void binary()
{
int b,c,l=n,m,p=1;
long j;
printf("Numbers are\n1.Sorted\n2.Unsorted\nEnter your choice:");
scanf("%d",&c);
if(c==1)
{
printf("Sorting order is\n1.Accending\n2.Decending\nInter your choice:");
scanf("%d",&b);
if(b==1)
{
printf("Inter the value you want to search:");
scanf("%ld",&j);
do
{
m=(p+l)/2;
if(j<a[m-1])
l=m-1;
else if(j>a[m-1])
p=m+1;
}
while(j!=a[m-1]&&p<=l);
if(a[m-1]==j)
printf("The number is found in the list :)\n\n");
else
printf("The number is not found in the list :(\n\n");
}
else if(b==2)
{
printf("Inter the value you want to search:");
scanf("%ld",&j);
do
{
m=(p+l)/2;
if(j<a[m-1])
p=m+1;
else if(j>a[m-1])
l=m-1;
}
while(j!=a[m-1]&&p<=l);
if(a[m-1]==j)
printf("The number is found in the list :)\n\n");
else
printf("The number is not found in the list :(\n\n");
}
}
else if(c==2)
{
long i,j,k=0;
printf("Inter the value you want to search:");
scanf("%ld",&j);
for(i=0;i<n;i++)
{
if(a[i]==j)
{
k=1;
}
}
if(k==1)
printf("The number is found in the list :)\n\n");
else
printf("The number is not found in the list :(\n\n");
}
}
void main()
{
char z;
do
{
x:printf("a.Insertion\nb.Deletion\nc.Bobble sort\nd.Search\ne.Display\nx.Quit\nInsert your Choice:");
scanf("%s",&z);
}
while(z!='a'&&z!='b'&&z!='c'&&z!='d'&&z!='e'&&z!='x');
printf("\n");
switch(z)
{
case 'a':insertion();
goto x;
case 'b':deletation();
goto x;
case 'c':bobble();
goto x;
case 'd':binary();
goto x;
case 'e':display();
goto x;
case 'x':break;
default:goto x;
}
}

0 comments :

Post a Comment