# Thread: Linear and binary search in c functions

1. ## 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;
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 )

}

```

appreciate any help ... thank you... 2. Let me guess: It warns you because both functions don't always return something. Remember that if a functions ends its execution before a "return" statement, it returns garbage to the caller. 3. Originally Posted by GReaper Let me guess: It warns you because both functions don't always return something. Remember that if a functions ends its execution before a "return" statement, it returns garbage to the caller.

yes you are RIGHT !
i got rid of warnings now .. but still have the other problems

thanks !! 4. 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;
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 )

return -1;
}```
when i search for 100 (which is inside the array)
it search for 102 !!
why it's not searching for the exact number i entered ?[/FONT] 5. What input are you giving the program?

Code:
```scanf("%d\n",&number[i]);

// ...

scanf("%d\n",&search);```
Try taking the '\n' out of the format strings there. 6. Originally Posted by Matticus What input are you giving the program?

Code:
```scanf("%d\n",&number[i]);

// ...

scanf("%d\n",&search);```
Try taking the '\n' out of the format strings there.
this is what i get .. searching for number 3

Please enter 10 positive numbers in ascending order:
¼¼§Ï1
¼¼§Ï2
¼¼§Ï3
¼¼§Ï4
¼¼§Ï5
¼¼§Ï6
¼¼§Ï7
¼¼§Ï8
¼¼§Ï9
¼¼§Ï10
ÏÏ§ÏWhat number you want to search for ? :
¼¼§Ï3
ÏÏ§Ï3 is found at location 3
ÏÏ§Ï3 found at location 3.
ÏÏ§Ï

i got rid of \n as you advised ..apparently sth is wrong i can't find it 7. What is the value returned by the search functions supposed to be?

Hint: Returning a -1 is likely supposed to be the value to indicate the search failed.

Tim S. 8. Originally Posted by stahta01 What is the value returned by the search functions supposed to be?

Hint: Returning a -1 is likely supposed to be the value to indicate the search failed.

Tim S.
you are right , it must return -1 and a msg says number isn't found if the search failed ... and return the location of the number if number was found. 9. i'm still getting wrong output .. if any one could help me fixing the problem..

input example
Please enter 10 positive numbers in ascending order:
¼¼§Ï12
¼¼§Ï14
¼¼§Ï20
¼¼§Ï30
¼¼§Ï36
¼¼§Ï100
¼¼§Ï102
¼¼§Ï200
¼¼§Ï201
¼¼§Ï202
ÏÏ§ÏWhat number you want to search for ? :
¼¼§Ï100
ÏÏ§Ï100 is found at location 6
ÏÏ§Ï100 found at location 6.
ÏÏ§Ï Because you told it to do it.
Because you put this INSIDE the loop doing the searching.

Compare with your other function, which only prints things after the loop is run. 11. Originally Posted by Salem Because you told it to do it.
Because you put this INSIDE the loop doing the searching.

Compare with your other function, which only prints things after the loop is run.
i tried to use curly braces
Code:
` {}`
before and after the print statement ... i tried using break; statement nothing work !
can you please be more specific and tell me what to do to avoid that ? thank you .. 12. Originally Posted by Hana Nasser
Code:
```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;
}```
Each iteration prints something, either found or not found. That is what Salem is referring to. Compare this function with your search binary function and note where the difference is. 13. Originally Posted by AndrewHunter Each iteration prints something, either found or not found. That is what Salem is referring to. Compare this function with your search binary function and note where the difference is.
i tried using break statement after each printf statement
Code:
```for(i=0;i<10;i++)
if (list[i]==s)
{
printf("%d is found at location %d\n", s,i+1);
break;
}
else
{
break;
}
return -1;
}
```

searching for number 50 (which is in location 5)
gives me this as an output:
Code:

Code:
```ÏWhat number you want to search for ? :
¼¼§Ï50
ÏÏ§Ï50 found at location 5.
ÏÏ§Ï
```

i understand what you and salem trying to say but i tried out every possible
solution 14. So why do you think that is? What happens in your loop if the number is not found? Right now your for loop basically says:
Code:
```for(all numbers in array)
{
if( number is found)
{
print found
stop searching
}
{
stop searching
}
}```
Is this the logic you really want? 15. Originally Posted by AndrewHunter So why do you think that is? What happens in your loop if the number is not found? Right now your for loop basically says:
Code:
```for(all numbers in array)
{
if( number is found)
{
print found
stop searching
} Popular pages Recent additions 