How do I write a binary search sorted array?

For some reason, the program won't output the binary search. It doesn't say there's anything wrong with the code, but when I try to run it, it keeps saying "binaryList(int*, int, int)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)"???????????

Here's my code for the binary search:
1
2
3
4
5
6
7
8
9
10
    cout<< "Enter an employee number for your search: "<<endl;
    cin>>searchValue;
    index = binaryList (empId, SIZE, searchValue);
    if (index== -1)
        cout << "The number you entered could not be found. ";
    else
    {
        cout<< "The number you entered is valid. "<<endl;
        cout<< "Employee "<<searchValue<<" worked "<<hours[index]<<" hours at $"<<payRate[index]<<" per hour and earned $"<<wages[index]<<endl;
    }

I don't see what's wrong. I did the same thing with the linear search and I had no problem.

Last edited on
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

//Prototypes
int searchList(int [], int, int);
int binaryList (int [], int, int);
void selectionSort (int [], int);



int main()
{
    //Get inputs
    const int SIZE=20;
    int empId[SIZE];
    double hours[SIZE];
    double payRate[SIZE];
    double wages[SIZE];
    string inFileName, outFileName;
    int count = 0;
    int searchValue;
    int results;
    int index;
    string name;
    
    cout << fixed << showpoint << setprecision(2);
    

    cout<<" PAYROLL PROCESSING"<<setw(20)<<endl;
    
    
    
    cout<<"Enter the name of the input file: ";
    cin>>inFileName;
    ifstream inFile;
    inFile.open(inFileName);
    
    
    if (!inFile.is_open())
    {   cout<<"Error opening file. "<<endl;
        return 1;
    }
    
    cout<<"Enter the name of the output file: ";
    cin>>outFileName;
    ofstream outFile;
    outFile.open(outFileName);
    
    
    
    while(count<SIZE && inFile>> empId[count] && empId[count] != -1)
    {   inFile >> hours[count] >> payRate[count];
        wages[count] = hours[count]*payRate[count];
        count++;
    }
    
 
    cout<<"Enter an employee number for your search: "<<endl;
    cin>>searchValue;
    results = searchList(empId, SIZE, searchValue);
    
    
    if (index == -1)
        cout << "The number you entered could not be found."<<endl;
    else
    {
        cout << "The number you entered is valid. "<<endl;
        cout<<" "<<endl;
        cout << "Employee "<<searchValue<<" worked "<<hours[results]<<" hours at $"<<payRate[results]<<" per hour and earned $"<<wages[results] << endl;
    }
    
    
    Search the array for the ID Number with binary search
    cout<< "Enter an employee number for your search: "<<endl;
    cin>>searchValue;
    index = binaryList (empId, SIZE, searchValue);
    if (index== -1)
        cout << "The number you entered could not be found. ";
    else
    {
        cout<< "The number you entered is valid. "<<endl;
        cout<< "Employee "<<searchValue<<" worked "<<hours[index]<<" hours at $"<<payRate[index]<<" per hour and earned $"<<wages[index]<<endl;
    }

    
    
    
    
    
    outFile<<"Employees in order entered:"<<endl;
    outFile<<" "<<endl;
    outFile<< "Employee Number"<<setw(20)<<"Hours Worked"<<setw(25)<<"PayRate per Hour"<<setw(22)<<"Wages"<<endl;
    

    for (int i=0; i<count; i++)
    {
        outFile <<setw(3)<< empId[i] << setw(20) << hours[i]<<setw(30)<<payRate[i]<<setw(25)<<wages[i] << endl;
        
    }
    
    
    outFile<<" "<<endl;
    outFile<<"Employee numbers in ascending order"<<endl;
    outFile<<" "<<endl;
    outFile<< "Employee Number"<<setw(20)<<"Hours Worked"<<setw(25)<<"PayRate per Hour"<<setw(22)<<"Wages"<<endl;
   
    
    
    cout<<"Programmer";
    cin>>name;
    outFile<<"Programmer"<<name;
    
    cout<<"Process Complete"<<endl;
    

    
    
    outFile.close();
    
    return 0;
}


int searchList(int list[], int numElems, int value)
{
    int index = 0;
    int position = -1;
    bool found = false;
        
    while (index < numElems && !found)
    {
        if (list[index] == value)
        {
            found = true;
            position = index;
        }
        index++;
    }
    return position;
}


int binarySearch(int array[], int numElems, int value)
{
    int first = 0,
    last = numElems - 1,
    middle,
    position = -1;
    bool found = false;
    
    while (!found && first <= last)
    {
        middle = (first + last) / 2;
        if (array[middle] == value)
        {
            found = true;
            position = middle;
        }
        else if (array[middle] > value)
            last = middle - 1;
        else
            first = middle + 1;
    }
    return position;
}

Last edited on
CODE TAGS CODE TAGS CODE TAGS CODE TAGS

http://www.cplusplus.com/articles/z13hAqkS/
http://www.cplusplus.com/articles/z13hAqkS/
http://www.cplusplus.com/articles/z13hAqkS/
http://www.cplusplus.com/articles/z13hAqkS/

I can see what the problem is, but I am not going to answer until you edit your post so it has code tags.
Last edited on
^I finally did it
When you have linker errors, it often means you haven't defined the function.

So you call binaryList , where is the definition for that? Or is something else happening?

How does the concept of binary search work? What is missing in your code to make that concept work?
But I did define it...
But I did define it...
No, binaryList(...) isn't defined. I would think that searchList(...) should be renamed as binaryList(...)?
Topic archived. No new replies allowed.