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;
}
|