[ Lab1 ] [ Lab2 ] [ Lab3 ] [ lab4 ] [ lab5 ] [ lab6 ] [ Lab7 ] [ Lab8 ] [ Lab9 ] [ Lab10 ]
CP107
LAB 4
LAB OBJECTIVES:
 |
Read a value from keyboard into a variable |
 |
Learn the meaning of true or false in C++ |
 |
Use relational operators and compound conditions such as AND,
OR , NOT |
 |
Recognizing Boolean expressions as true or false |
 |
Using a switch statement instead of if/else series |
 |
Use
functions |
 | Pass
parameters by reference and by value |
LAB ASSIGNMENTS :
-
Write and run a program that
reads a single char digit, 1 to 8 and then prints the number as a literal
string. For
example, if the input is 7, then the output should be the word
"seven".
If user enters a number greater than 8, then output should be “Out
of range”. Must use a switch statement.
Submit the first program as digit.cpp

-
Exercise Program 3-39 - Write a program that plays the game of
"guess the number" as follows:
Your program chooses the number to be guessed by selecting an integer in the
range of 1 to 1000. The program then displays:
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess
The player then types a first guess. The program responds with one of the
following :
1. Excellent! you have guessed the number!
Would you like to play again ( Y or n) ?
2. Too low. Try again.
3. Too high. Try again.
Count the guesses the player makes, If the player makes less than 10
guesses then display
" Either you know the secret, or you got lucky. If the player makes
more than 10 guesses, then display "You can do better"
use the following algorithm and create the following three functions (do not
change the name or functionality of the functions).
bool isCorrect( int, int );
void display( int );
int GetRandomNumber (int iMaximumRange);
do {
randomNumber = GetRandomNumber()
Prompt user "I hae a number between 1 and 1000, can you guess my
nummber"
Get a response from user.
if response = -1 then return. end of game.
bool Done = False
while loop (NOT Done )
{
Done = isCorrect(guess, randomNumber
count - increment how many times we have
propmpted the user.
if NOT Done (the response was
incorrect), then prompt
user to input an other number
}
user must have guessed the number.
Display a message Excellent! You guessed the number!
Call function Display (count) to display if user guessed within 10 tries or
not.
} (while user wants to continue to play)
Function void display( int num)
Takes one argument as an integer. This is the number of tries it took for the
user to guess the number.
If num is less than 10, then display a message
"Either you know the secret, or you got lucky".
Else, display the message "You can do better".
Function bool isCorrect( int usersGuess, int
RandomNumWeAreGuessing )
Check to see if usersGuess is eual to RandomNumWeAreGuessing. If it is, then
display the message that you that you have guessed then number. If the guess is
lower than Random number, then display, too low, guess Higher (or something like
that). If the guess is higher, then display, too high, guess lower.
If user guess the number, then the return value will be true, otherwise it will
be false.
|