#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
void TAKING_VALUE(int DATA[50],int x)
{
int i;
for(i=1;i<=x;i++)
{
printf("DATA[%d]=",i);
scanf("%d",&DATA[i]);
}
}
void INSERT(int DATA[50],int x)
{
int loc,v,i,k,t;
INSERT:
printf("\nENTER LOCATION TO INSERT:\t");
scanf("%d",&loc);
if(loc>x)
{
printf("\n\aSORRY OOCATION IS OUT OF RANGE. TRY AGAIN..");
goto INSERT;
}
printf("\nENTER VALUE:\t");
scanf("%d",&v);
printf("\nAFTER INSERT..");
for(i=1;i<=x+1;i++)
{
if(loc==i)
{
for(k=i;k<=x+1;k++)
{
t=DATA[k];
DATA[k]=v;
v=t;
}
}
printf("\nDATA[%d]=%d",i,DATA[i]);
}
}
void main()
{
int i,n,k,t,DATA[50],loc,v;
char c;
clrscr();
printf("HOW MANY ELEMENT YOU WANT:\t");
scanf("%d",&n);
TAKING_VALUE(DATA,n);
AGAIN:
INSERT(DATA,n);
printf("\nINSERT AGAIN ? (y/n)..");
c=getch();
if(c=='y'||c=='Y')
{
n=n+1;
goto AGAIN;
}
else
{
printf("\n\n\t\t****** GOOD BYE ******");
delay(2000);
exit(1);
}
}
Category: Programming
Programming
Linear Search by Using Function in C
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void LINEAER_SEARCH(int DATA[50],int x);
void TAKING_VALUE(int DATA[50],int x)
{
int i;
printf("START ENTERING VALUE\n");
for(i=1;i<=x;i++)
{
printf("DATA[%d]=",i);
scanf("%d",&DATA[i]);
}
LINEAER_SEARCH(DATA,x);
}
void LINEAER_SEARCH(int DATA[50],int x)
{
int i,loc,v,k=0;
printf("ENTER VALUE TO SEARCH:\t");
scanf("%d",&v);
for(i=1;i<=x;i++)
{
if(DATA[i]==v)
{
k=1;
printf("%d IS AT LOCATION %d",DATA[i],i);
break;
}
}
if(k==0)
{
printf("\n\aSORRY. %d CANN'T FIND",v);
}
}
void main()
{
int i,n,DATA[50];
char c;
clrscr();
printf("HOW MANY ELEMENT YOU WANT:\t");
scanf("%d",&n);
TAKING_VALUE(DATA,n);
getch();
}
Multi-Insert by Using Function in C
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void READ_VALUE(int data[50],int n1)
{
int i;
for(i=1;i<=n1;i++)
{
printf("data[%d]= ",i);
scanf("%d",&data[i]);
}
}
void MULTI_INSERT(int data[50],int n1,int l,int n2)
{
int i,j,a[50];
for(i=1;i<=n2;i++)
{
printf("a[%d]= ",i); scanf("%d",&a[i]);
}
//shifting//
for(i=(n1+n2),j=0;i>=n2+l;i--,j++)
{
data[i]=data[n1-j];
}
//entering value//
for(i=0;i<n2;i++)
{
data[l+i]=a[i+1];
}
//display//
for(i=1;i<=n1+n2;i++)
{
printf("data[%d]=%d\n",i,data[i]);
}
}
void main()
{
clrscr();
int n1,n2,l,data[50];
char c;
printf("how many element u want:\t"); scanf("%d",&n1);
READ_VALUE(data,n1);
again:
printf("\nEnter location to insert:\t"); scanf("%d",&l);
printf("\nHow many element u want:\t"); scanf("%d",&n2);
MULTI_INSERT(data,n1,l,n2);
printf("\nInsert again ? (y/n)..");
c=getch();
if(c=='y')
{
n1=n1+n2;
goto again;
}
else
exit(0);
}
Normal Insert in C
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
void main()
{
int i,n,k,t,DATA[50],loc,v;
char c;
clrscr();
printf("HOW MANY ELEMENT YOU WANT:\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("DATA[%d]=",i); scanf("%d",&DATA[i]);
}
INSERT:
printf("\nENTER LOCATION TO INSERT:\t"); scanf("%d",&loc);
if(loc>n)
{
printf("\n\aSORRY OOCATION IS OUT OF RANGE. TRY AGAIN...");
goto INSERT;
}
printf("\nENTER VALUE:\t"); scanf("%d",&v);
printf("\nAFTER INSERT..");
for(i=1;i<=n+1;i++)
{
if(loc==i)
{
for(k=i;k<=n+1;k++)
{
t=DATA[k];
DATA[k]=v;
v=t;
}
}
printf("\nDATA[%d]=%d",i,DATA[i]);
}
printf("\nINSERT AGAIN ? (y/n)..");
c=getch();
if(c=='y'||c=='Y')
{
n=n+1;
goto INSERT;
}
else
{
printf("\n\n\t\t****** GOOD BEY ******");
delay(2500);
exit(0);
}
}
Linear Search in C
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int i,n,DATA[50],index,v,l=1;
char c;
clrscr();
printf("How many element you want :\t"); scanf("%d",&n);
clrscr();
printf("Start entering value:\n");
for(i=1;i<=n;i++)
{
printf("DATA[%d]=",i); scanf("%d",&DATA[i]);
}
printf("\nDO YOU WANT TO SEARCH ANY DATA.(y/n)");
c=getch();
if(c=='y')
{
SEARCH:
printf("\nENTER VALUE TO SEARCH: ");scanf("%d",&v);
for(i=1;i<=n;i++)
{
if(DATA[i]==v)
{
l=0;
index=i;
printf("\nINDEX NUMBER OF %d IS %d",v,index);
break;
}
}
if(l==1)
{
printf("\n\aSORRY THERE ISN'T SUCH KIND OF VALUE.TRY AGAIN...");
goto SEARCH;
}
printf("\nDO IT AGAIN ?(y/n)");
c=getch();
if(c=='y')
{
goto SEARCH;
}
}
else
exit(1);
}
Binary Search in C
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
int i,t,j,n,beg,end,mid,DATA[50],loc,v;
char c;
printf("HOW MANY ELEMENT YOU WANT:\t"); scanf("%d",&n);
//READING VALUE//
for(i=1;i<=n;i++)
{
printf("DATA[%d]=",i); scanf("%d",&DATA[i]);
}
//START SORTING//
for(i=1;i<=n-1;i++)
{
for(j=i+1;j<=n;j++)
{
if(DATA[i]>DATA[j])
{
t=DATA[i];
DATA[i]=DATA[j];
DATA[j]=t;
}
}
}
printf("AFTER SORTING..\n");
for(i=1;i<=n;i++)
{
printf("\nDATA[%d]=%d",i,DATA[i]);
}
BINARY_SEARCH:
printf("\nENTER VALUE TO FIND:\t"); scanf("%d",&v);
beg=1; end=n; mid=(beg+end)/2;
while(DATA[mid]!=v && beg<=end)
{
if(v>DATA[mid])
{
beg=mid+1;
}
else
{
end=mid-1;
}
mid=(beg+end)/2;
}
if(DATA[mid]==v)
{
printf("\n%d IS AT LOCATION %d",v,mid);
}
else
{
printf("\n\aLOCATION IS NOT FIND");
}
printf("\nTRY AGAIN ? (Y/N)..");
c=getch();
if(c=='y'||c=='Y')
{
goto BINARY_SEARCH;
}
else
exit(0);
}