Linear and binary search in c functions
hi i have this HW
it partially works ..
the program must ask the user for 10 positive integers in ascending order.. then search for a number given by user ... search must be accomplished through two functions : linear and binary
so far i have 3 problems :
1- i keep getting two warning
2- linear search is not working although the algorithm is fine
3- binary search is printing the wrong"search" number.
here is my code
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
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);
}
appreciate any help ... thank you...