Search This Blog

Labels

c codes (6) c++ codes (1)

Monday, February 01, 2016

C PROGRAM FOR BOTH INSERTION AND DELETION




C PROGRAM FOR BOTH INSERTION AND DELETION

#include<stdio.h>
#define n 10
int main()
{
int a[n],i,b;
printf("A Tarun Sankar\tRoll No. 14115007\n");
printf("Enter elements of array:\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("\n\nWhat do you want to do??\n1.Insertion\t2.Deletion:-");
scanf("%d",&i);
if(i==1)
{
int v;
printf("Where do you want to insert the new element?\n");
printf("1.Start\t2.Any position\t3.End:-");
scanf("%d",&i);
printf("Enter the element which you want to insert:-");
scanf("%d",&v);
if(i==1)
{
for(i=0;i>n-1;i++)
a[i+1]=a[i];
a[0]=v;
}
else if(i==2)
{
printf("Enter which postion you want to insert:-");
scanf("%d",&b);
for(i=b;i>n-1; i++)
a[i+1]=a[i];
a[b-1]=v;
}
else if(i==3)
a[n-1]=v;
printf("The array after insertion is:-\n");
for(i=0;i<n; i++)
printf("%d\t",a[i]);
}
else if(i==2)
{
printf("Which element you want to delete?\n1.tart\t2.any position\t3.End:-");
scanf("%d",&i);
if(i==1)
for(i=0;i<n; i++)
a[i]=a[i+1];
else if(i==2)
{
printf("Enter which postion you want to delete:-");
scanf("%d",&b);
for(i=b-1;i<n; i++)
a[i]=a[i+1];
}
printf("The array after deletion is:-\n");
for(i=0; i<n-1; i++)
printf("%d\t",a[i]);
}
return 0;
}
Output:-


In this program coding is done in the ubuntu software .It is basically used for student to use linux type operating system.Programmers can learn linux type operating system through this programs.

C PROGRAM FOR BOTH BINARY AND LINEAR SEARCH




C PROGRAM FOR BOTH BINARY AND LINEAR SEARCH

#include<stdio.h>
#define n 10
int bRecSear(int arr[], int data, int first, int last);
int main()
{
int a[n],i,v,pos=-1,f=0,l,mid;
printf("Enter the elements of the array:\n");
for(i=0;i<n; i++)
scanf("%d",&a[i]);
printf("How do you want to search??\n1.Linear Search\t2.Binary Search:- ");
scanf("%d",&i);
printf("Enter value to search in array:- ");
scanf("%d",&v);
if(i==1)
{
for(i=0;i<n;i++)
if(a[i]==v)
pos=i;
}
else if(i==2)
{
printf("\nHw do you want to search?\t1.By Itertion\t2.By Recursion:-");
scanf("%d", &i);
if(i==1)
{
l=n-1;
mid =(f+l)/2;
while (f<=l)
{
if (a[mid]<v )
f=mid+1;
else if (a[mid]==v)
{
pos=f;
break;
}
else
l = mid - 1;
mid = (f+l)/2;
}
}
if(i==2)
{
pos=bRecSear(a,v,0,9);
}
}
if(pos==-1)
printf("\nElement not found");
else
printf("\nElement at position:\t%d",pos+1);
return 0;
}
int bRecSear(int arr[], int data, int first, int last)
{
int mid=(first+last)/2, pos=-1;
if(first<=last)
{
if(arr[mid]==data)
{
pos=mid;
}
else if(arr[mid]<data)
{
first=mid+1;
pos=bRecSear(arr, data, first, last);
}
else
{
last=mid-1;
pos=bRecSear(arr, data, first, last);
}
}
return pos;
}

OUTPUT

 


In this program coding is done in the ubuntu software .It is basically used for student to use linux type operating system.Programmers can learn linux type operating system through this programs.

Pages

Powered By Blogger