CSC133 Introduction to CS Using C++
Lab 6

Part 1

Write a program that reads in 10 numbers and prints the largest of the numbers. Your program should have two functions:

Part 2

Write a program that implements a guessing game. The program will generate a random number between 1 and 100 inclusive. Ask the user for a number in the range of 1 through 100 inclusive. If their guess is larger than the number your program generated, print a message saying that their guess is too high. If their guess is smaller than the number your program generated, print a message saying that their guess is too low. Keep prompting the user for guesses until they get the correct number. When the guess is correct, congratulate the user and tell them how many guesses they needed to get the answer.

Your program should let the user play the guessing game as many times as he or she desires. After the user guesses the number, and the program prints the congratulations message, the program should ask the user whether he or she would like to play again. If so, the program should generate another random number and start the game again.

To write this program you will create the following functions:

To generate random numbers between 1 and 100, use the randomize() and random functions. These functions require #include statements for stdlib.h and time.h. The randomize() function initializes the random number generator. The function has no parms and returns nothing. To call it, use the statement:

     randomize();

randomize() must be called once at the beginning of the program, before any random numbers are generated. To generate a random number, use the random function. The random function has one parm, which is an int. It returns an int, which is a random number between 1 and (the value of the parm - 1). So, for example:

     num = random(50);

will set num to a random number between 1 and 49.

Sample Execution

I'm thinking of a number between 1 and 100.
Can you guess what it is?
Enter a guess: 45
Too high...guess again: 20
Too low...guess again: 35
Too high...guess again: 26
Too high...guess again: 23
Congratulations! You guessed it in 5 tries!

Would you like to play again [y or n]? y

I'm thinking of a number between 1 and 100.
Can you guess what it is?
Enter a guess: 62
Too high...guess again: 40
Too high...guess again: 24
Too high...guess again: 12
Too low...guess again: 18
Too high...guess again: 14
Too low...guess again: 16
Congratulations! You guessed it in 7 tries!

Would you like to play again [y or n]? y

I'm thinking of a number between 1 and 100.
Can you guess what it is?
Enter a guess: 33
Too low...guess again: 77
Too high...guess again: 61
Congratulations! You guessed it in 3 tries!

Would you like to play again [y or n]? n

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002