this is the last modification :
Code:
#include<stdio.h>
int searchLinear(int s,int *list, int n); //definition of first function..
int searchBinary(int s,int *list, int n); //definition of second function..
int main(void)
{
int number[10];
int i,search;
int count=10;
printf("Please enter 10 positive numbers in ascending order:\n");
for(i=0; i<count; i++)
scanf("%d\n",&number[i]);
printf("What number you want to search for ? :\n");
scanf("%d\n",&search);
searchLinear(search, number,count);
searchBinary(search, number,count);
return 0;
}
int searchLinear(int s,int *list, int n)
{
int i;
for(i=0; i<10; i++)
if (list[i]==s)
printf("%d is found at location %d\n", s,i+1);
else
printf("%d is not found\n",s);
return -1;
}
int searchBinary(int s,int *list,int n)
{
int first = 0;
int last = n - 1;
int middle = (first+last)/2;
while( first <= last )
{
if ( list[middle] < s )
first = middle + 1;
else if ( list[middle] == s )
{
printf("%d found at location %d.\n", s, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", s);
return -1;
}
when i search for 100 (which is inside the array)
it search for 102 !!
and prints "not found"
why it's not searching for the exact number i entered ?[/FONT]