CPTR 124 Fundamentals of Programming


In this lab you will write two programs that use loops.


  1. Part 1: Guessing Game In C++, you can generate a pseudorandom integer by calling the rand function. In order for the compiler to recognize the rand function you must place the following preprocessor directives at the top of your program in additional to your iostream inclusion:
    #include <cstdlib> #include <ctime>
    Not only does this give your program access to the rand function, it also provides the function srand and a system-specific constant RAND_MAX. For Visual C++ RAND_MAX is 32,767. The ctime header provides access to the system clock.

    In preparation for generating pseudorandom numbers, execute the following statement once in your program

    srand(static_cast<unsigned>(time(0)));
    The sets the random number seed value from a time provided by the operating system. This ensures the first generated value is "random enough" for our purposes. Note: Do not call the srand function more than one time during your program's execution.

    After setting things up with srand, the following statement:

    int x = rand();
    will assign a pseudorandom integer in the range 0...RAND_MAX, inclusive, to the integer variable x.

    For example, the following complete program prints 10 pseudorandom numbers:

    #include <iostream> #include <cstdlib> #include <ctime> int main() { // Randomly set pseudorandom number seed srand(static_cast<unsigned>(time(0))); int i = 0; while (i < 10) { std::cout << rand() << '\n'; i++; } }

    Each time you run the program it prints a different sequence of numbers.

    In Visual C++, the expression rand() evaluates to a pseudorandom number in the range 0...32,767. This range is too large for our immediate need, so you will have to adapt the result with some arithmetic that includes the modulus operator (%); for example, if x is any nonnegative integer, the expression x % 25 evaluates to an integer in the range 0...24, inclusive.

    See Chapter 8 in the textbook for more information about random numbers.

    Write a C++ program that plays a guessing game with the user. The program's user is the player. The player is supposed to guess an integer in the range 1–100, inclusive. The program should generate a pseudorandom number in this range at random. The program's generated number is the correct answer, or secret answer. The program compares the player's guess to the correct number:

    • If the player's number is less than the correct number, then the program should print "Too low" and allow the player to guess again.
    • If the player's number is greater than the correct number, then the program should print "Too high" and allow the player to guess again.
    • If the player's number is the same as the correct number, the game is over.

    Within a loop, the game continues until the player guesses the correct answer. When the game ends, the program prints the total number of guesses, counting the correct guess as one of the guesses. The player's goal is to guess the number with the fewest attempts.

    The following shows a sample run of the program:

    Guess a value from 1 to 100: 50 Too low, please try again Guess a value from 1 to 100: 75 Too high, please try again Guess a value from 1 to 100: 64 Too high, please try again Guess a value from 1 to 100: 55 Too high, please try again Guess a value from 1 to 100: 52 Too high, please try again Guess a value from 1 to 100: 51 51 is the correct answer It took you 6 tries to get the correct answer

    Hint: While you are developing your program you can print the answer (secret number to guess) each time the program requests a new guess. That way you can better tell if your program is working properly; otherwise, it is difficult to determine if the program is giving the player the correct feedback. Be sure to disable this "feature" in your final version, or it will not be much of a game!

  2. Part 2: Grade Calculator

    Write a C++ program that reads in letter grades for a class. Acceptable grades are A, B, C, D, and F. The user may enter as many grades as desired and enters a Z to indicate the end of the grades. The terminating letter Z does not count as a grade. The user should not attach a plus or minus to a grade. The program should ignore any other characters entered by the user. Your program should treat a lower-case letter like its upper-case equivalent; for example, process a b as if it were a B. If letter is a char variable, the statement

    letter = static_cast<char>(toupper(letter)); // Capitalize the letter, if possible

    will capitalize the character if is an alphabetic letter.

    The program should keep track of the number of students that passed (D or above) and the number that failed (F). After the user finishes entering the letter grades, the program should calculate and print the percentage of students passing and failing as well as the class grade-point average (GPA). Use the following numeric values for computing the class GPA: A = 4.0, B = 3.0, C = 2.0, D = 1.0, and F = 0.0.

    Any letters that the user enters that are not A, B, C, D, or F do not participate in the calculation of the pass/fail percentage and do not contribute to the GPA calculation. If the user enters a Z before entering any valid letter grades, the program should terminate without printing anything.

    Sample run:

    Enter grades (Z terminates the list): A B B C F D Z Students passing: 5 (83.3333%) Students failing: 1 (16.6667%) Class GPA: 2.16667

    You do not need to round the GPA to a certain number of places after the decimal point.

  3. Check out

    Your finished programs will be evaluated for correctness and compliance. When approved, you should submit your two C++ source files (the .cpp files) to http://eclass.e.southern.edu.