Classwork 7: I am Thinking of a Number


To practice writing a program that uses if statements and a while loop.

The Assignment

Write a program to play the game "I'm thinking of a number." The program will play the role of the person who has the "secret" number. Your program should prompt the user to guess a number. If user's guess is incorrect, your program should say whether the guess is too high or too low. A sample run of your program might look like the following (with the user response in orange):

PT[321]% gcc -Wall thinking.c

PT[322]% ./a.out
I'm thinking of a number between 1 and 100.
Guess my number.
Your guess? 13
Too low!
Your guess? 20
Too low!
Your guess? 35
Too low!
Your guess? 99
Too high!
Your guess? 74
Too high!
Your guess? 45
Too low!
Your guess? 84
Too high!
Your guess? 60
Too low!
Your guess? 70
Too high!
Your guess? 65
Too high!
Your guess? 63
Too low!
Your guess? 64
You got it!


You must use a while loop in your program which terminates when the user guesses the correct number.

Use #define to define a constant secret that holds the number your program is "thinking" of. (See for example math.c which used #define to define a constant PI.) That way, if you change the secret number, you only have to change it in one place.

Name your program thinking.c. Use the script command to record yourself compiling the program using and testing your program in several sample runs. (Remember to exit from script when you are done. Also, do not run nano when you are in script.) Finally, submit your program and sample runs using:

submit cs104_chang cw07 thinking.c 
submit cs104_chang cw07 typescript

Embellishments (optional)

Include any number of the following embellishments, if you have time: