Re: How to make a program that guesses what number I'm thinking of?
Quote:
Originally Posted by
2kaud
What compiler are you using?
I am using Code Blocks with MinGW (GCC) compiler.
Re: How to make a program that guesses what number I'm thinking of?
Quote:
Originally Posted by
2kaud
This works for VS2015
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello" << endl;
system("pause");
}
However, depending upon the compiler you may also need to include cstdlib
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
cout << "Hello" << endl;
system("pause");
}
The second example worked for me. So it needed that inclusion.
Re: How to make a program that guesses what number I'm thinking of?
Quote:
Originally Posted by
2kaud
Which loops have you covered? There are 3 - do, for and while.
I have covered while loops, and I have seen for loops in other examples with other languages. But I have not worked with it yet with C++ in this course.
Re: How to make a program that guesses what number I'm thinking of?
I almost have it now. But I keep going around the loop even after finding the correct number. How do I prevent this?
Code:
int main()
{
// Create an array of 100 elements
int arr[100];
// Set values to elements, ranging from 1 to 100
for (int i = 0; i < 100; i++)
{
arr[i] = i + 1;
}
cout << "Think of a number between 1 and 100. Press ENTER when ready." << endl;
cin.get();
int key;
int first = arr[0];
int last = (sizeof(arr)/sizeof(int)-1);
while (first <= last)
{
int mid = (first + last)/2;
cout << "Is it " << mid << "?" << endl;
cout << "Press 1 and ENTER if the number is correct." << endl;
cout << "Press 2 and ENTER if your number is lower." << endl;
cout << "Press 3 and ENTER if your number is higher." << endl;
cin >> key;
switch(key)
{
case 1:
cout << "Found it!" << endl;
case 2:
last = mid +1;
break;
case 3:
first = mid + 1;
break;
default:
return -1;
break;
}
}
return 0;
}
Re: How to make a program that guesses what number I'm thinking of?
When the response is 1 then you need to break out of the loop. So there must be some condition for the while loop to stop when the number has been found.
Incidentally, you don't need the array as first is 1 and last is 100 at the beginning.
Re: How to make a program that guesses what number I'm thinking of?
Quote:
Originally Posted by
2kaud
When the response is 1 then you need to break out of the loop. So there must be some condition for the while loop to stop when the number has been found.
Incidentally, you don't need the array as first is 1 and last is 100 at the beginning.
I need to set the first to be greater than last? That should stop it.
But I used a return instead. This is what I have now:
Code:
int main()
{
// Create an array of 100 elements
int arr[100];
// Set values to elements, ranging from 1 to 100
for (int i = 0; i < 100; i++)
{
arr[i] = i + 1;
}
cout << "Think of a number between 1 and 100. Press ENTER when ready." << endl;
cin.get();
int key;
int first = arr[0];
int last = (sizeof(arr)/sizeof(int)-1);
while (first <= last)
{
int mid = (first + last)/2;
cout << "Is it " << mid << "?" << endl;
cout << "Press 1 and ENTER if the number is correct." << endl;
cout << "Press 2 and ENTER if your number is lower." << endl;
cout << "Press 3 and ENTER if your number is higher." << endl;
cin >> key;
switch(key)
{
case 1:
cout << "Found it!" << endl;
return 0;
case 2:
last = mid - 1;
break;
case 3:
first = mid + 1;
break;
default:
return -1;
break;
}
}
return 0;
}
I have turned it in this way. I did add a counter as the last thing before turning it in. It failed to work properly anyway. I needed to print out the number of tries the computer had made once it found the number. I did something stupid with j++ and got 1 for 3 tries (where key was 37).
Note that I changed last = mid + 1 in case 2.
I'm curious though, how would you do this? Why do I not need an array? Feel free to post your own solution or change mine.
Re: How to make a program that guesses what number I'm thinking of?
Quote:
Originally Posted by
cozySam
I'm curious though, how would you do this? Why do I not need an array? Feel free to post your own solution or change mine.
You should ask yourself why you do need it. You know the upper and lower bounds are 0 and 100. Why not just set first and last directly. You don't use the array or any of the values other than the first one.
Re: How to make a program that guesses what number I'm thinking of?
Consider
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Think of a number between 1 and 100. Press ENTER when ready." << endl;
cin.get();
int key;
int first = 1;
int last = 100;
int cnt = 0;
do {
int mid = (first + last) / 2;
++cnt;
cout << "Is it " << mid << " ?" << endl;
cout << "Press 1 and ENTER if the number is correct." << endl;
cout << "Press 2 and ENTER if your number is lower." << endl;
cout << "Press 3 and ENTER if your number is higher." << endl;
cin >> key;
switch (key) {
case 1:
cout << "Found it in " << cnt << " tries" << endl;
break;
case 2:
last = mid - 1;
break;
case 3:
first = mid + 1;
break;
default:
cout << "Invalid entry." << endl;
--cnt;
break;
}
} while (key != 1);
return 0;
}
Note that this code doesn't check for incorrect responses - like always entering 2 etc. I'll leave that checking to you as an exercise! :d