/* Binary search using Structures */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
struct students
{
int rno,pno;
char name[100];
};
int n,i,j,r,t,low,mid,high;
struct students info[100];
clrscr();
printf("\n\n How many students : ");
scanf("%d",&n);
if(n<=0 || n>100)
{
printf("\n\n Enter a valid digit between 1 & 100");
getch();
exit(0);
}
for(i=0;i<n;i++)
{
printf("\n\n Enter the name of %d student : ",i+1);
scanf("%s",info[i].name);
printf("\n\n His roll numbers : ");
scanf("%d",&info[i].rno);
printf("\n\n His phone numbers : ");
scanf("%d",&info[i].pno);
}
printf("\n\n\n Name \t\t Roll no \t\t Phone no");
for(i=0;i<n;i++)
{
printf("\n\n %s \t\t %d \t\t\t %d",info[i].name,info[i].rno,info[i].pno);
}
printf("\n\n Enter the roll no to be searched : ");
scanf("%d",&r);
for(i=0;i<n;i++)
for(j=0;j<n-1-i;j++)
{
if(info[j].rno > info[j+1].rno)
{
t = info[j].rno;
info[j].rno = info[j+1].rno;
info[j+1].rno = t;
}
}
low = 1; high = n;
do{
mid = (low + high)/2;
if(r>info[mid].rno)
low = mid + 1;
else if(r<info[mid].rno)
high = mid - 1;
}
while( r!=info[mid].rno && low<=high);
if(r==info[mid].rno)
printf("\n\n The roll number %d is found",r);
else
printf("\n\n The roll number %d is not found",r);
getch();
}
Comments
Post a Comment