Don't work binary search.

Can someone help me with code. I cant get to work my binary search.
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
#include <cstdlib>
#include <iostream>
using namespace std;
//-----------------------------------------------
void printNumbers (int Arr[], int count);
void binarySearch (int Arr[], int left, int right);
void readNumbers  (int Arr[], int count);
//---------------------------------------------
int main()
{
    int Arr[] = {1, 2, 3, 4, 5, 6, 7}; 
    int left = Arr[0]; 
    int right = Arr[6]; 
    int count = sizeof(Arr) / sizeof(Arr[0]); 
    cout << left << " " << right;
    for (;;)
    {
        cout << endl
             << "Work with numbers:"      << endl
             << "  1 - Print numbers" << endl
             << "  2 - Binary search"             << endl
             << "  3 - Edit numbers"              << endl
             << endl
             << "  0 - Exit"   << endl;
        string key;
        cin >> key;
             if (key == "0") return 0;
        else if (key == "1") printNumbers (Arr, count);
        else if (key == "2") binarySearch (Arr, left, right);
        else if (key == "3") readNumbers  (Arr, count);
        else cout << endl << "Bad command..." << endl;
    }
}
//-------------------------------------------------
void readNumbers (int Arr[], int count)
{
    cout << "Insert Numbers:" << endl;
    for (int i = 0;  i < count;  i++)
    {
        cout << (i+1) << "/" << count << ": ";
        cin  >> Arr[i];
    }
}
//------------------------------------------------
void binarySearch(int Arr[], int left, int right) {
     int value;
     int middle;
     cout << "Which number to find: ";
     cin >> value;
      while (left <= right) {
            middle = (left + right) / 2;
            if (Arr[middle] == value){
            middle = middle;
            }
            else if (Arr[middle] > value){
                  right = middle - 1;}
            else if (Arr[middle] < value){
                  left = middle + 1;}
            else cout << "Where isn't such number";
      }
      cout << value << " are " << middle << " array element";
}
//------------------------------------------------
void printNumbers (int Arr[], int count)
{
    for (int i = 0;  i < count;  i++)
        cout << Arr[i] << "  ";
    cout << "\n";
}
It looks okay to me, except that it will always print like it succeeded, even when it didn't. Could you be more specific as to what you want help with?

EDIT:
I think your problem is in main(). The parameters "left" and "right" for the binary search function are array subscripts, not values in the array.
Last edited on
Then I starting to search, I can't get any result.
Did you try changing "left" and "right" to array subscripts instead of values in the array?
Yes. It doesn't give anything. I still can't get ANY result.
Anyone?
Topic archived. No new replies allowed.