CPTR 124 Fundamentals of Programming


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


  1. Teams

    You are encouraged to work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.

  2. Part 1: Guessing Game

    In C++, you can generate a pseudorandom number by calling the rand function. To generate pseudorandom numbers, execute the following statement once in your program

    srand(static_cast<unsigned>(time(0)));
    	 
    Do not call the srand function more than one time during your program's execution.

    Afterward, each time you need a pseudorandom number, the statement

    x = rand();
    	 
    will assign a pseudorandom number to the integer variable x. You will need to add the preprocessor directives #include <cstdlib> and #include <ctime> to the top of your program.

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

       #include <iostream>
       #include <cstdlib>
       #include <ctime>
       
       using namespace std;
       
       int main() {
           srand(static_cast<unsigned>(time(0)));
           
           int i = 0;
           while (i < 10) {
               cout << rand() << endl;    
               i++;
           }
       }
    

    Each time the program is run 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 (%).

    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. The program should generate a number in this range at random. The program's number is the correct answer. The player's guess is compared 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 total number of guesses is printed. The player's goal is to guess the number with the fewest attempts.

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

  3. 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. No plus or minus should be attached 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.

    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) (A = 4.0, B = 3.0, C = 2.0, D = 1.0, 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
    

  4. Check out

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