CS 010 - Introduction to Computer Science I
Assignment 2: Guessing Game

DUE: Tuesday, April 20 before 11:00pm


Collaboration Policy

Collaboration is strictly FORBIDDEN. Programs must represent YOUR OWN original work. Sharing code or team-coding are not allowed for this assignment. Copying code from ANY source (any book, current or past students, past solutions, the web, etc.) is not allowed. Cooperation to the extent of helping to debug, or discussing the general approach to solving the problem is encouraged, but should never involve communicating code or even pseudo-code or explicit algorithms. Your code must be unique -- if it is not, we will find out, and will treat it as a case of flagrant academic dishonesty.


You must turn in a C++ source file (and only a C++ source file). The name for your source file should be as2.cpp.

Turn in online to the appropriate folder for your lab section (e.g., as2_20 for students enrolled in lab section 20). If you turn your assignment in to the wrong folder, your assignment may not be graded. If it is graded, you will lose 2 pts (out of 10).

Remember to include the header as it is in the template provided on the class website.


Problem Definition:

Write a program that allows the user to play a guessing game. The program should pick a random integer (code for this given below) between 0 and 100 inclusive. The user then has 5 attempts to guess the number. Your program should help the user out by telling them whether an incorrect guess is too high or too low. It should also tell them how many picks are left after each guess.

If the user guesses incorrectly 5 times, the program should output the correct number and tell them they did not win.

If the user guesses correctly at any time, the program should output a congratulary message. For 1 point bonus, have a different message depending on how many guesses it took the user to guess the correct number.


The following are 2 different sample outputs to help you design your program. The numbers in bold indicate user input.

Sample 1

     This is a guessing game.
     You have 5 chances to correctly guess an integer between 0 and 100 inclusive.

     Enter your first guess: 50
     Sorry, your guess was too high.
     Enter your second guess: 25
     Sorry, your guess was too low.
     Enter your third guess: 37
     Sorry, your guess was too low.
     Enter your fourth guess: 44
     Sorry, your guess was too low.
     Enter your fifth guess: 47
     Correct. Wow! You just made it.

Sample 2

     This is a guessing game.
     You have 5 chances to correctly guess an integer between 0 and 100 inclusive.

     Enter your first guess: 33
     Sorry, your guess was too low.
     Enter your second guess: 83
     Sorry, your guess was too high.
     Enter your third guess: 37
     Sorry, your guess was too low.
     Enter your fourth guess: 57
     Sorry, your guess was too low.
     Enter your fifth guess: 63
     Oops!! You are out of guesses.
     The correct number was 60.


To generate a random number, you may use the following code to begin your project. The variable rand_num will hold the randomly generated number. Just copy-and-paste this code to your .cpp file and then put your code in place of the comment "// your code here".

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
   srand( time(0) );  
   int rand_num = rand() % 101;

   // your code here

   return 0;
}  

Look back here after 3pm on Tuesday for assignment rubric.

Rubric: (10 pts total + 1 bonus pt)

3 pts: branches
2 pts: correct output
       - (1 pt) Correct # of guesses is output for each guess
       - (1 pt) Too high, too low, or correct is output for each guess
1 pt:  Header info (use report template header on syllabus)
2 pts: Compiles without errors
2 pts: Style
       - (.5 pts) Good variable names
       - (.5 pts) Proper indentation/spacing
       - (.5 pts) Good comments
       - (.5 pts) No line wraps

1 pt BONUS: Different congratulatory message for each number of guesses.