CPTR 124 Fundamentals of Programming


This lab provides more practice writing programs involving loops.


  1. Teams

    You may 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. What to do

    Use your experience gained from the guessing game program to write a program that can be used by elementary school children to hone their arithmetic skills. Your program should generate random two number addition, subtraction, and multiplication problems. When the user supplies an answer, the program checks to see if the answer is correct. If the answer is correct, the program generates a new problem; otherwise, if the user's answer is not correct, the program keeps requesting the solution for the same problem. After five incorrect attempts, the program prints the correct answer and produces a new problem.

    The particulars:

    • The numbers must be integers in the range -500...500, inclusive.
    • The operations consist of addition, subtraction, and multiplication.
    • The numbers and the operation must be generated at random using the rand function.

    The following shows a sample run of the program:

    Solve 215 x 20: 4300
    Correct!  Please try another.
    Solve -468 + -259: -727
    Correct!  Please try another.
    Solve 216 x -8: 100
    100 is not correct, please try again.
    Solve 216 x -8: -100
    -100 is not correct, please try again.
    Solve 216 x -8: 0
    0 is not correct, please try again.
    Solve 216 x -8: 12
    12 is not correct, please try again.
    Solve 216 x -8: 10
    Sorry, the correct answer is -1728. Let's try another.
    Solve 338 + 245: 583
    Correct!  Please try another.
    Solve -152 x 351:
                       

    Generating the random numbers is pretty straightforward, but what about the operation? Simply find a pseudorandom number in the range 0...3, and, based on that number select a character to assign to a char variable ('+', '-', or 'x'), and pre-compute the result. Then you can print the problem for the user to see and compare the user's input to the correct answer.

  3. Check out

    Your finished program will be evaluated for correctness and compliance. When approved, you should submit your source file to eclass.