-
September 23rd, 2016, 06:09 AM
#16
Re: How to make a program that guesses what number I'm thinking of?
Originally Posted by 2kaud
What compiler are you using?
I am using Code Blocks with MinGW (GCC) compiler.
-
September 23rd, 2016, 06:12 AM
#17
Re: How to make a program that guesses what number I'm thinking of?
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.
-
September 23rd, 2016, 06:13 AM
#18
Re: How to make a program that guesses what number I'm thinking of?
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.
-
October 3rd, 2016, 05:32 AM
#19
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;
}
Last edited by 2kaud; October 3rd, 2016 at 06:51 AM.
-
October 3rd, 2016, 06:59 AM
#20
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 3rd, 2016, 07:57 AM
#21
Re: How to make a program that guesses what number I'm thinking of?
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.
-
October 3rd, 2016, 08:07 AM
#22
Re: How to make a program that guesses what number I'm thinking of?
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.
-
October 3rd, 2016, 08:35 AM
#23
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!
Last edited by 2kaud; October 3rd, 2016 at 11:38 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|