Need help with undefined reference
Here is the assignment:
Code:
1. The program first creates an array of 20 random positive numbers between 1 and 100. Make sure there
are no duplicates in the array.
2. Then apply selection sort to sort the array in ascending order, then display the entire array.
3. Now, ask user to enter any number between 1 and 100.
4. Use binary search to check if the user’s number is contained in the array. If so, display ‘yes’ with the
index of the matching element. If not, display ‘no’.
5. Ask if the user wants to continue. If yes, return to Step 3. If no, terminate program.
Here is the code:
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void sort(int a[], int n);
void print5(int a[]);
int binarySearch (int arr[], int value, int size);
const int ArraySize = 20;
int arr[ArraySize];
int main()
{
int numberEntered;
srand(time(NULL));
for (int i = 0; i < ArraySize;i++) {
arr[i] = (2 * rand() % 100);
}
sort(arr,ArraySize);
print5(arr);
cout << "Please enter a number:" << endl;
cin >> numberEntered;
int search = binarySearch(arr, ArraySize, numberEntered);
if (search == -1)
cout << "That number was not found";
else {
cout << "That number is found at element: " << arr << endl;
}
return 0;
}
int binarySearch(int arr[], int value, int size, int right, int left) {
while (left <= right) {
int middle = (left + right) / 2;
if (arr[middle] == value)
return middle;
else if (arr[middle] > value)
right = middle - 1;
else
left = middle + 1;
}
return -1;
}
void sort(int a[], int n)
{
int temp;
for (int x = 1; x <= n ; x++)
{
for (int i = 0; i+1 < n; i++)
{
if (a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
}
void print5(int a[])
{
for (int c = 0; c < 20; c++)
{
cout << a[c] << endl;
}
}
Here is the error:
Code:
/tmp/ccFLEPP7.o: In function `main':
:(.text.startup+0x99): undefined reference to `binarySearch(int*, int, int)'
collect2: error: ld returned 1 exit status
Could someone help me and tell me what I'm doing wrong? I've tried everything and can't seem to make it work.
Re: Need help with undefined reference
You have forward declared and are using a function binarySearch with 3 parameters, but the definition of the method in your code has 5 parameters. Thus the error message.
Re: Need help with undefined reference
A couple of further comments.
Your code for populating the array will allow for duplicates as no check is made to ensure that the values are unique as required
Code:
cout << "That number is found at element: " << arr << endl;
arr is the array, not the found element position. Don't you mean search?
Code:
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
rather than this, you could use the STL std::swap() see http://www.cplusplus.com/reference/algorithm/swap/
Code:
swap(a[i], a[i + 1]);
NB I know this is an exercise, but just for info there are standard STL algorithms to sort [sort()] and search [binary_search()]. See
http://www.cplusplus.com/reference/a...binary_search/
http://www.cplusplus.com/reference/algorithm/sort/