# Logical errors with seach function

• 09-18-2006
Taka
Logical errors with seach function
I am working on a homework assignment to make a program that searches using binary and linear searches. It compiles but the logical errors mean it doesn't work at all. I'm not really sure where I've gone wrong so any help would be great. Thanks.

Code:

```#include <stdio.h> #define SIZE 15 void binarySearch(int numbers[], int value); void linearSearch(int numbers[], int value); int main() {   int numbers[] =  { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };   printf("Searching for -1 in numbers using linear search\n");   linearSearch(numbers, -1);   printf("Searching for -1 in numbers using binary search\n");   binarySearch(numbers, -1);   printf("\nSearching for 0 in numbers using linear search\n");   linearSearch(numbers, 0);   printf("Searching for 0 in numbers using binary search\n");   binarySearch(numbers, 0);   printf("\nSearching for 7 in numbers using linear search\n");   linearSearch(numbers, 7);   printf("Searching for 7 in numbers using binary search\n");   binarySearch(numbers, 7);   printf("\nSearching for 11 in numbers using linear search\n");   linearSearch(numbers, 11);   printf("Searching for 11 in numbers using binary search\n");   binarySearch(numbers, 11);   printf("\nSearching for 15 in numbers using linear search\n");   linearSearch(numbers, 15);   printf("Searching for 15 in numbers using binary search\n");   binarySearch(numbers, 15);   printf("\nSearching for 22 in numbers using linear search\n");   linearSearch(numbers, 22);   printf("Searching for 22 in numbers using binary search\n");   binarySearch(numbers, 22);   return 0; } void binarySearch(int numbers[], int value) {   int step = 1, left = 0, right = SIZE-1, mid;   while (left < right)   {       mid = (left + right) / 2;       if (numbers[mid] == value)       {         printf("Number found after %d steps\n", step);         return;       }       else if (numbers[mid] < value)       {         left = mid+1;         step++;       }       else         right = mid-1;         step++;    }   printf("Number not found after %d steps\n", step); } void linearSearch(int numbers[], int value) {   /* Use linear search to find value in numbers.       Print the number of steps it takes to find or not find value   */    int steps, i;   for (i=0; i < SIZE; i++)   {       if (value == numbers[i])       {           printf("Number found after %d steps\n");           return;       }       printf("Number not found after %d steps\n");   } }```
This was the result I got when I tested it:
Quote:

Searching for -1 in numbers using linear search
Searching for -1 in numbers using binary search

Searching for 0 in numbers using linear search
Number found after 0 steps
Searching for 0 in numbers using binary search

Searching for 7 in numbers using linear search
Number found after 0 steps
Searching for 7 in numbers using binary search
Number found after 1 steps

Searching for 11 in numbers using linear search
Number found after 0 steps
Searching for 11 in numbers using binary search
Number found after 3 steps

Searching for 15 in numbers using linear search
Searching for 15 in numbers using binary search

Searching for 22 in numbers using linear search
Searching for 22 in numbers using binary search

Expected result
Searching for -1 in numbers using linear search
Searching for -1 in numbers using binary search

Searching for 0 in numbers using linear search
Number found after 1 steps
Searching for 0 in numbers using binary search
Number found after 4 steps

Searching for 7 in numbers using linear search
Number found after 8 steps
Searching for 7 in numbers using binary search
Number found after 1 steps

Searching for 11 in numbers using linear search
Number found after 12 steps
Searching for 11 in numbers using binary search
Number found after 2 steps

Searching for 15 in numbers using linear search
Number found after 16 steps
Searching for 15 in numbers using binary search
Number found after 5 steps

Searching for 22 in numbers using linear search
Searching for 22 in numbers using binary search
• 09-18-2006
quzah
Quote:

Expected result
Searching for -1 in numbers using linear search
Since your list is sorted, it should know on the first match that -1 isn't in the list, since the first element is the lowest.
Quote:

Code:

```if (value == numbers[i])       {           printf("Number found after %d steps\n");           return;       }       printf("Number not found after %d steps\n");```

You never increment 'steps'. You never supply arguments for your specifiers for printf. Also, you apparently completely ignore your compiler as it screams at you for doing so...

Quzah.
• 09-18-2006
In your binary search, you can't find 15, because it's the 16th number in your number array.
But you've defined SIZE as 15.

0 through 15 == 16 numbers. :)

In the array initialization, you should remove the comma after the 15. Commas separate entries, they don't go after the last one in the set.

And your problem with the linear search, you have already been given.

That should get things searching aright. :) :)

• 09-18-2006
quzah
Quote:

In the array initialization, you should remove the comma after the 15. Commas separate entries, they don't go after the last one in the set.

They can go after the last one. It has no effect. It's allowed by the standard.

Quzah.
• 09-18-2006