Guess That Number
Objectives:
- Introduction to random numbers
- For loops
- Break statements
Resources:
Presentation about control structures (powerpoint or pdf)
C++.com
Student Instructions:
Write a program that randomly picks a number between 0 and 100, inclusive. Then ask the user to guess what number the computer guessed within 10 guesses. The computer should tell the user whether their guess was to low or two high.
Pseudocode:
Select a random number between 0 and 100. * Give the user a chance to enter their guess. If the user's guess was less than the computers number output "Sorry your number is to small." If the user's guess was to larger than the computers number output "Sorry your number was to large." If the user guessed the number output "Good guess" and exit the program. Repeat the statements between the * and this line 9 more times. The use of a loop for this program is optional. Turn in the .cpp file when you are finished.
Hints:
- Check out the Control Structures (html, PDF, ppt) presentation if you forgot how to setup an if statement.
- Use the following example to help you get your random number generator to work.
#include //for the time( NULL ) function call
main() {
int i;
srand( time( NULL ) ); //Seed the random number generator.
i = rand(); //Get the random number.
}
- num = i % 101 will keep your the value between 0 and 100 inclusive.
- If return 0 is called in the main function it will cause the program to exit.
- Login to post comments