Need help with undefined reference
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Need help with undefined reference

  1. #1
    Join Date
    Jun 2017
    Posts
    13

    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.

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,144

    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.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    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/
    Last edited by 2kaud; June 22nd, 2017 at 05:43 AM. Reason: NB
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)