Please Help

Write a program that creates an array with 34 integers
like this: 0, 3, 6, 9, 12, 15, ..., 99
Ask the user to guess a number from 0 to 99.
Use a binary search to search for the number. Let the
user keep guessing until the user's guess is found.

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
  #include <iostream>
using namespace std;
int binarySearch(int[], int, int);

int main()
{
   const int SIZE = 34;
   int array[SIZE] = { 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 
                      48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 
                      93, 96, 99 };
   int number, results;
   bool valid = false;
   
   cout << "Enter a number between 0 and 99: ";
   cin >> number;

   results = binarySearch(array, SIZE, number);
  
      if (results == -1)
      {
         cout << "Enter a number between 0 and 99: ";
         cin >> number;
      }

      else
      {
         cout << "right " << results;
      }
   
   system("pause");
   return 0;
}

int binarySearch(int list[], 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 (list[middle] == value)
      {
         found = true;
         position = middle;
      }

      else if (list[middle] > value)
      {
         last = middle - 1;
      }

      else
      {
         first = middle + 1;
      }
   }
   return position; 
}


My problem is that I don't know how to keep the users guessing a number until the ends up guessing one in the array.
Last edited on
If you want a piece of code to run until a certain condition is met, then you need to use loops.
I suggest that you replace everything from lines 14 through 29 (including 14 and 29) with this code.
1
2
3
4
5
6
7
8
9
10
11
12
while(true){
   cout << "Enter a number between 0 and 99: ";
   cin >> number;

   results = binarySearch(array, SIZE, number);
  
      if (results != -1)
      {
         cout << "right " << results;
         break;
      }
} 


You want to ask user to enter a number until a specific condition is met and hence I put that piece of code in the while loop.
Now, I set the condition of while loop to true which means that it would loop infinitely.
Then, if the user's entry matches one in array, then it would display the right # and then the break keyword would break out of the infinite loop.

Hope that helps!
Last edited on
For the binarySearch function, why dont you just make a for-loop that goes through the entire array and looks for the value? Once it finds the value, it returns the position. That seems like a much simpler way of doing it than what you have...
@Arslan7041
What you're describing is a linear search, not a binary search.
Oh, sorry. Im actually not familiar with binary search. Is there a particular advantage of one over the other?
Topic archived. No new replies allowed.