Hello, good day. :)
I got a problem with "passes occurred". This is the only problem left in my program. My program is about Naive Searching and Binary Searching. There's no problem with my "Naive Searching". The problem is the "Binary Searching".
Naive Searching: This is the result when I inputted a number to be searched in the array. There's no problem with the "passes". The "passes occurred" there is how many times does the searching occurred.
Since:
index --> 0 1 2 3 4
array --> 5 4 3 2 1
passes --> 1 2 3 4 5
[IMG]http://i168.photobucket.com/albums/u162/SHENGTON/naive.png[/IMG]
Binary Searching:
Since:
index --> 0 1 2 3 4
array --> 1 2 3 4 5
passes -->(How many times the array divided during searching)
Since I inputted "4" to be search, the passes occurred should be "2" only.
[IMG]http://i168.photobucket.com/albums/u162/SHENGTON/binary.png[/IMG]
NOTE: The numbers in the array will depend on what the user inputted in the "Input Items".
Here's my code:
#include<stdio.h>
#include<conio.h>
int naive(int a[], int size);
int binary(int a[],int size);
int display(int a[],int size);
int disp(int a[],int size);
void swap (int a[],int size);
void paws(int a[],int size);
int main()
{
int a[100],cho=0,i,n;
int pop=0;
do
{
printf("\n\n\n [1] Input Items\n");
printf(" [2] Naive Search\n");
printf(" [3] Binary Search\n");
printf(" [4] Display Items\n");
printf(" [5] Exit\n\n");
printf(" Enter your choice: ");
scanf("%d",&cho);
i = getchar();
if(cho==1)
{
clrscr();
printf("\n\n Enter integer for total numbers to be sorted: ");
scanf("%d",&n);
printf("\n\n");
for(i=0;i<=n-1;i++)
{
printf(" Enter integer for no.%d : ",i+1);
scanf("%d",&a[i]);
}
pop=1;
}
else if(cho==2 && pop)
{
naive(a,n);
}
else if(cho==3 && pop)
{
binary(a,n);
}
else if(cho==4 && pop)
{
display(a,n);
}
else
{
clrscr();
printf(" _____________________\n | \t\t |");
printf("\n | The array is empty. |\n");
printf(" |_____________________|");
}
}
while(cho != 5);
{
clrscr();
printf("\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t ___________________\n\n\t\t\t Press Enter to Exit\n");
printf("\t\t\t ___________________");
i = getchar();
++i;
}
clrscr();
return 0;
}
int display(int a[],int size)
{
int i;
clrscr();
paws(a,size);
printf("\n\n The array contains: ");
for(i=0;i<size;i++)
{
printf("%3d",a[i]);
}
}
int naive(int a[],int size)
{
int i,n,found=0,index;
clrscr();
paws(a,size);
display(a,size);
printf("\n\n Enter the element to be searched: ");
scanf("%d",&n);
for(i=0;i<=size;i++)
{
if(n==a[i])
{
found=1;
index=i;
}
}
if(found==1)
{
printf("\n %d is found at index %d, %d passes occurred",n,index,index+1);
}
else
{
printf("\n %d is not found.",n);
}
}
int binary(int a[],int size)
{
int x,i,found=0,index,p;
clrscr();
swap(a,size);
disp(a,size);
printf("\n\n Enter the element to be searched: ");
scanf("%d",&x);
if(x==a[size/2])
{
for(i=size/2;i<size;i++)
{
if(x==a[i])
{
found=1;
index=i;
}
else
{
p=1;
}
}
if(found==1)
{
printf("\n %d is found at index %d, %d passes occurred.",x,index,p);
}
else
{
printf("\n %d is not found.",x);
}
}
else if(x<a[size/2])
{
for(i=0;i<size/2;i++)
{
if(x==a[i])
{
found=1;
index=i;
}
else
{
p=2;
p++;
}
}
if(found==1)
{
printf("\n %d is found at index %d, %d passes occurred.",x,index,p);
}
else
{
printf("\n %d is not found.",x);
}
}
else if(x>a[size/2])
{
for(i=(size/2)+1;i<size;i++)
{
if(x==a[i])
{
found=1;
index=i;
}
else
{
p=2;
p++;
}
}
if(found==1)
{
printf("\n %d is found at index %d, %d passes occurred.",x,index,p);
}
else
{
printf("\n %d is not found.",x);
}
}
}
void swap(int a[],int size)
{
int i,j,t;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]>a[j])
{
t=a[j];
a[j]=a[i];
a[i]=t;
}
}
}
}
void paws(int a[],int size)
{
int i,j,t;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]<a[j])
{
t=a[j];
a[j]=a[i];
a[i]=t;
}
}
}
}
int disp(int a[],int size)
{
int i;
clrscr();
printf("\n\n The array contains: ");
for(i=0;i<size;i++)
{
printf("%3d",a[i]);
}
}