find number

Hi there :)

I am trying to create game find a number.
User picks number from range up to 100. Lets say 21.
Program should ask user if number he/she selected is less then half of the number range ie 50 or less.

if user answer yes, then it should be asked another question.
Is your number less then (half of 50) 25.
Users say yes.
So next question would be: Is the number (half of 25) 12/13 or less? Lets go with 12 for now.

Answer would be NO:
So our range is then 12 - 25. Half of this range would be 19.
So is your number smaller then 19??

Answer would be NO.

Range then changes to 19 - 25 and half of the range would be 21?

which was correct answer.

Problems with this code I have
1. Should I use maybe modulo operator or percentage?
2. There if I dive by half at some point I end up odd number and I can't divide it in half so what should be done for example 7 should i treat this as 3 and 4?
3. If I got to correct answer how to break loop and display custom message?
Something like: "Your number was...."
4. what if user pick an easy number ie 50 or 100 or 1 is there a way that will allow to skip some question and provide answer?(i am assuming there is not, as we can only be certain if we follow algorithm, unless we give option to user that they can type my_number for displayed number like 50 (half of initial set), 25(quarter of initial set), 75 (3 quarters of initial set)

I suspect that once I got algorithm correctly I would be able to answer most (if not all questions on my own) but until that happens I need a hint :)

thanks :)




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
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int size = 100;
	vector<int>OneHundryt;
	string answer = "";

	for (int i = 0; i < size; i++)
	{
		OneHundryt.push_back(i + 1); // filling vector with numbers 1-100
	}

	while (true) // needs to be replaces with better loop not sure yet what
	{
		cout << "Is your number eaqul or less then " << OneHundryt[OneHundryt.size() / 2]-1 << endl;
		cout << "(Yes/No)" << endl;
		cin >> answer;

		if (answer == "yes" || answer == "Yes")
		{
			size = (OneHundryt[OneHundryt.size() / 2])-1;

			for (int i = 0; i < size; i++)
			{
				OneHundryt.resize(size); // filling vector with numbers half of the initial value
				cout << OneHundryt[i] << ' ';
			}
			cout << endl;
		}
		else
		{
			for (int i = (size / 2); i < size; i++)
			{
				OneHundryt.push_back(i + 1);
				cout << OneHundryt[i] << ' ';

			}
			cout << endl;
		}
	}

	system("pause");
	return 0;
}

Last edited on
oh well maybe next time ;)
What you trying to implement is the binary search algorithm.

1. Should I use maybe modulo operator or percentage?
Neither: it will be a simple integer division.

2. There if I dive by half at some point I end up odd number and I can't divide it in half so what should be done for example 7 should i treat this as 3 and 4?
It doesn't matter. I.e. take whatever the integer division returns.

3. If I got to correct answer how to break loop and display custom message?
Something like: "Your number was...."
If you use a loop, it is exactly that break;

4. what if user pick an easy number ie 50 or 100 or 1 is there a way that will allow to skip some question and provide answer?(i am assuming there is not, as we can only be certain if we follow algorithm, unless we give option to user that they can type my_number for displayed number like 50 (half of initial set), 25(quarter of initial set), 75 (3 quarters of initial set)
These numbers are not easier then any other numbers, so no there is no way to skip.
Took a few liberties with this one, but here is how I'd approach the problem. We could apply a few algorithms from the STL to cut this down a bit.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <set>
#include <string>

using namespace std;

bool ValidateInput(const string& input, const size_t& index);
bool Quit(const string& input);
int CalculateNextValue(const string& input, set<int>& numbers);
string FormatString(const string& input);

int main()
{
    // Initialize variables/constants; create a set of integers.
    const int NUM_INTS = 100;
    // Let's use a "set" to take advantage of the fact that it's a binary search tree and
    // has unique elements.
    set<int> numbers;
    int counter = 0;

    // Populate the set.
    while (counter < NUM_INTS)
        numbers.insert(++counter);
    // Prompt the user to think of a number.
    cout << "Hi. Please think of a number between 1 and 100." << endl;
    cout << "Don't tell me!  I'm going to guess your number!" << endl;
    cout << endl;
    // Let's begin a loop to find the guessed number. We use the Quit() function to decide whether to
    // exit/continue the loop.
    int index = NUM_INTS / 2;
    string answer = "";
    while (!Quit(answer))
    {
 		// Ask the user if we have the correct answer.
 		cout << "Is your number " << index << "? (Yes or No): ";
 		cin >> answer;
 		if ((ValidateInput(answer,index)) && (FormatString(answer) == "yes"))
        {
            Quit("quit");
            break;
        }
        // If we don't have the right number, take another guess.
 		cout << "Is your number less then " << index << "? (Yes or No; \"Quit\" to exit): ";
		cin >> answer;
		cout << endl;
		if (Quit(answer))
            break;
		while (!ValidateInput(answer,index))
            cin >> answer;
        index = CalculateNextValue(answer,numbers);
        // In case the user forgot the number...
        if (index == 0)
        {
            cout << "Interesting...No more numbers! I guess I need more practice.  Good-bye!!" << endl;
            break;
        }
    }
    return 0;
}


bool
ValidateInput(const string& input, const size_t& index)
{
    // Format the string so it's not case sensitive; make sure that the input is a 'yes' or 'no';
    string str = FormatString(input);
    if ((str != "yes") && (str != "no"))
    {
        cout << "Invalid response. Is your number equal or less then " << index << endl;
        cout << "Please answer \"Yes\" or \"No\"" << endl;
        return false;
    }
    return true;
}

int
CalculateNextValue(const string& input, set<int>& numbers)
{
    int index = numbers.size() / 2;
    int counter = 0;

    set<int>::iterator iter = numbers.begin();

    while (counter < index)
    {
        ++iter;
        ++counter;
    }
    if (FormatString(input) == "no")  // If yes, we get the bottom half of the set.
        numbers.erase(numbers.begin(),iter);
    else
        numbers.erase(iter,numbers.end());
    index = (numbers.size()) / 2;
    counter = 0;
    iter = numbers.begin();
    while (counter < index)
    {
        ++iter;
        ++counter;
    }
    return *iter;
}

bool
Quit(const string& input)
{
    string str = FormatString(input);
    if (str != "quit")
        return false;
    cout << "Thanks for playing! See you next time!" << endl;
    return true;
}

string
FormatString(const string& input)
{
    // Just want to format the string to not be case sensitive.  
    string str = input;
    for (size_t i = 0; i < input.length(); ++i)
    {
        str[i] = tolower(input[i]);
    }
    return str;
}

Last edited on
Hi Coder777 and Skinding

thank you for your help :)

I will be reviewing your suggestions.
Coder 777 I will be looking more at binary search :)

Skinding - I am looking at your code and its complex!!! :) bearing in mind that this was exercise from B. Stroustrup book chapter 4, i was expecting something more basic ;) like finding odd or even number.


I do really appreciated your help. Solution to this problem eluded me for almost a week ;)
(and still does)
Last edited on
Topic archived. No new replies allowed.