Binary Search help

Hello, I am having trouble compiling this code that finds a number in a given array using a binary search. I was able to get most of it with help from my book
but for some reason I keep getting syntax errors on line 57. "expected primary expression for "else" ". I keep looking for my error but I cannot find it. Any tips and advice would be greatly appreciated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>            // needed for Cin and Cout
#include <string>              // needed for the String class
#include <math.h>              // math functions
#include <stdlib.h>  
#include <iomanip>     
using namespace std;


int binarySearch(const int [], int, int);
const int size = 20;


int main()
{
    // variable list
    int searchNumber;                 // the number you are searching for in the array
    int intArray [20] = {2, 4, 5, 8, 9, 12, 16, 18, 19, 23, 33, 36, 38, 38, 45, 48, 59, 66, 79, 666};
    int results; 
    
    
    cout << "please enter a number to search for: ";
    cin >> searchNumber;
    
    results = binarySearch(intArray, size, searchNumber);
    
    if (results == -1)
    {
                cout << " That number does not exist in the array \n";
                

    else 
    {
         cout << " That number is found at element " << results;
         cout << " in the array. \n";
    }
    return 0;

}
    
    /***************************
    *     print array
    ***************************/
    for (int x=0; x<20; x++)
    {
      if(x%5 ==0)
      {
        cout << "\n";
      }
      cout << setw(5) << intArray[x];
    }
    cout << "\n\n";
    
    
    
    int binarySearch( int intArray[], int size, int value);
    {
    int value;
    int first = 0;                   //First Array element 
    int last = size -1;              // Last Array element
    int middle;                     //  Mid-point of search
    int position = -1;              //postion of search
    bool found = false;            // flag
    
    while (!found && first <= last)
    {
          middle = (first + last) /2;
          if (intArray[middle] == value)
          { 
                found = true; 
                position = middle; 
          }
          else if (intArray[middle] > value)
             last = middle -1;
          else 
             first = middle +1;
    }
    return position;
}
                               
    
   
    system("pause");
    return 0;
}
{}
Last edited on
31:5: error: expected '}' before 'else'

There's an opening { at line 27, but no matching close } on line 30 before the else.

1
2
3
4
43:5: error: expected unqualified-id before 'for' 
43:19: error: 'x' does not name a type 
43:25: error: 'x' does not name a type 
51:5: error: 'cout' does not name a type 


Lines 40 - 51 - is this supposed to be in the main function, or will this be a separate print function? Line 36-38 end the main function, so this code is just sitting out in global space right now. There's also what seems to be the end of main at line 82-84.

56:5: error: expected unqualified-id before '{' token

Remove the ; at the end of line 55.

In function 'int binarySearch(int*, int, int)': 60:9: error: declaration of 'int value' shadows a parameter

In line 55, you have an int value as one of the parameters. Line 57 then declares a new int value. If you want to use the value sent into the function as value, remove line 57.
hey wildblue, thanks for the response.

The matching close for line 27 would be on line 38 no?
I removed line 55 and now it is telling me that value is undelcared, and im still getting an error before "else".

Edit :
Okay I got rid of the error with "else" and i rearranged some things, but I am still getting
a " [Linker error] undefined reference to `binarySearch(int const*, int, int)' "


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>            // needed for Cin and Cout
#include <string>              // needed for the String class
#include <math.h>              // math functions
#include <stdlib.h>  
#include <iomanip>     
using namespace std;


/*************************************
*     function prototype
*************************************/
int binarySearch(const int [], int, int);
const int size = 20;

int main()
{
    // variable list
    int searchNumber;                 // the number you are searching for in the array
    int intArray [size] = {2, 4, 5, 8, 9, 12, 16, 18, 19, 23, 33, 36, 38, 38, 45, 48, 59, 66, 79, 666};
    int results; 
     
    cout << "please enter a number to search for: ";
    cin >> searchNumber;
    
    // search for number 
    results = binarySearch(intArray, size, searchNumber);
    
    if (results == -1)
    cout << " That number does not exist in the array \n";
    else 
    { 
         cout << " That number is found at element " << results;
         cout << " in the array. \n";
         }    
    
    
    /***************************
    *     print your array
    ***************************/
    for (int x=0; x<20; x++)
    {
      if(x%5 ==0)
      {
        cout << "\n";
      }
      cout << setw(5) << intArray[x];
    }
    cout << "\n\n";  
                                                   
    return 0;
}
int binarySearch( int intArray[], int size, int value)
    {
    int first = 0;                   //First Array element 
    int last = size -1;              // Last Array element
    int middle;                     //  Mid-point of search
    int position = -1;              //postion of search
    bool found = false;            // flag
    
    while (!found && first <= last)
    {
          middle = (first + last) /2;
          if (intArray[middle] == value)
          { 
                found = true; 
                position = middle; 
          }
          else if (intArray[middle] > value)
             last = middle -1;
          else 
             first = middle +1;
    }
    return position;
}
Last edited on
The matching close for line 27 would be on line 38 no?


No - the compiler is expecting a closing } for the if before the else.

Line 55, just remove the ; at the end of the line, and add const to match your prototype of the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int binarySearch( const int intArray[], int size, int value); // remove ; at end and add const to match prototype at line 9
    {
    int value;  // remove redeclaration of value
    int first = 0;                   //First Array element 
    int last = size -1;              // Last Array element
    int middle;                     //  Mid-point of search
    int position = -1;              //postion of search
    bool found = false;            // flag
    
    while (!found && first <= last)
    {
          middle = (first + last) /2;
          if (intArray[middle] == value)
          { 
                found = true; 
                position = middle; 
          }
          else if (intArray[middle] > value)
             last = middle -1;
          else 
             first = middle +1;
    }
    return position;
}
I found the problem I just had to put "const" in front of int intArray[] in line 52. Thanks again
[Linker error] undefined reference to `binarySearch(int const*, int, int)' "


That linker error is because the function prototype and definition don't match on the const keyword.

1
2
int binarySearch(const int [], int, int);
int binarySearch( int intArray[], int size, int value)

Topic archived. No new replies allowed.