C++ program proj1.cpp Solution.CPP

C++ program proj1.cpp Solution

 
Objective:

·         To review C++ basic programming knowledge: data types, writing loops, writing functions, using C++ I/O, using standard library,…

·         To gain experience with program development life cycle, especially specification, design, coding and testing.

In this warm up project, you are asked to write a C++ program proj1.cpp that lets the user or the computer play a guessing game. The computer randomly picks a number in between 1 and 100, and then for each guess the user or computer makes, the computer informs the user whether the guess was too high or too low. The program should start a new guessing game when the correct number is selected to end this run (see the sample run below).

Check this example and see how to use functions srand(), time() and rand() to generate the random number by computer.

Example of generating and using random numbers:



Read text page 33 to 38 for the binary search algorithm-that is the way you search a person's phone number in a phone book.

Here is a sample run of the program:

Would you like to (p)lay or watch the (c)omputer play?

p

Enter your guess in between 1 and 100.

45

Sorry, your guess is too low, try again.

Enter your guess in between 1 and 100.

58

Sorry, your guess is too low, try again.

Enter your guess in between 1 and 100.

78

Sorry, your guess is too high, try again.

Enter your guess in between 1 and 100.

67

Sorry, your guess is too low, try again.

Enter your guess in between 1 and 100.

70

Sorry, your guess is too high, try again.

Enter your guess in between 1 and 100.

68

Sorry, your guess is too low, try again.

Enter your guess in between 1 and 100.

69

Congrats, you guessed the correct number, 69.

 

Would you like to (p)lay or watch the (c)omputer play or (q)uit?

c

The computer's guess is 50.

Sorry, your guess is too high, try again.

The computer's guess is 25.

Sorry, your guess is too high, try again.

The computer's guess is 13.

Sorry, your guess is too low, try again.

The computer's guess is 19.

Sorry, your guess is too high, try again.

The computer's guess is 16.

Sorry, your guess is too low, try again.

The computer's guess is 17.

Congrats, you guessed the correct number, 17.

Would you like to (p)lay or watch the (c)omputer play or (q)uit?

q

Press any key to continue

Grading Policy

(30 points) Grading of this programming assignments will reflect three factors, weighted as shown:

·         Correctness -- Does the program run correctly and follow the requirements? (15 points)

·         Style -- Does the code follow the Documentation and Submission Guidelines? Is the code well structured, readable, indented properly? Do you use meaningful literals? Do you paste your own sample output as comments at the end of your source file? (10 points )

·         Design -- Did you use the most efficient algorithm? (5 points )

 
Powered by