Thread: Help! with binary search

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    7

    Help! with binary search

    Code:
    #include<iostream>#include<cstdlib>
    #include<ctime>
    
    
    void selectionSort(int[], int);
    int binarySearch(const int[], int, int);
    
    
    
    
    int main() {
        const int SIZE = 10;
        const int MAX = 100;
        int Array[SIZE] = { 0 };
        time_t t;
        srand((unsigned)time(&t));
        int search, results;
    
    
        for (int i = 0; i < SIZE; i++)
        {
            Array[i] = (rand() % MAX) + 1;
            
        }
        std::cout << std::endl;
        
        std::cout << "Array is " << std::endl;
        for (int i = 0; i < SIZE; i++)
        {
            std::cout << Array[i];
        }
    
    
        std::cout << "Sorted Array is " << std::endl;
        selectionSort(Array, SIZE);
        {
            for (int j = 0; j < SIZE; j++)
                std::cout << Array[j] << std::endl;        
        }
    
    
        std::cout << "What is your number " << std::endl;
        std::cin >> search;
    
    
        results = binarySearch(Array, SIZE, search);
    
    
        if (results == -1)
            std::cout << "No" << std::endl;
        else
        {
            std::cout << "Yes" << std::endl;
        }
    
    
        system("PAUSE");
        return 0;
    }
    
    
    void selectionSort(int array[], int size)
    {
        int temp, i, j;
        for (i = 0; i < size; i++)
        {
            j = i;
            while (j > 0 && (array[j - 1] > array[j]))
            {
                temp = array[j];
                array[j] = array[j - 1];
                array[j - 1] = temp;
                j--;
            }
        }
    }
    
    
    int binarySearch(const int array[], int size, int value)
    {
        int first = 0,
            last = size - 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;
    }
    When I run this code I get my selection sort but I don't get the return of my binary search

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe you forgot an assignment?
    Code:
    $ g++ -Wall -Wextra foo.cpp
    foo.cpp: In function ‘int binarySearch(const int*, int, int)’:
    foo.cpp:97:27: warning: statement has no effect [-Wunused-value]
                 last - middle - 1;
                               ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    7
    Are you saying I need a pointer to my function?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How did you come up with that?

    Read the error message.

    "no effect" means you may as well have written
    Code:
            else if (array[middle] > value)
                ; // do nothing
            else first = middle + 1;
    Let me spell it out.
    Write
    last = middle - 1;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  2. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  3. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM

Tags for this Thread