Assignment 06

Due: Thursday, Mar. 1, 2012, at 12:07:45 p.m., 100 points

For this assignment you will submit (in the usual way) multiple files. These files will contain a C++ program that you have named appropriately, each file with an appropriate name and appropriate extension. Be sure that your program compiles using the g++ compiler before you submit it. To compile a program with multiple files, use the usual g++ command but instead of the single file you compiled before, compile *.cpp and the compiler will "grab" all the cpp files in that directory. So, you see, it's important that you put all the files for a particular program in their own directory! You don't have to list the header files; that will happen automatically. Use cssubmit in the usual way.

Background: Some nonsense stuff.

Specifications: Your program will be a "quiz-er" for the us-er. That is, your program will present the user with a quiz of arithmetic problems. Each "play" of the quiz will be 10 questions. The user will initially be presented with a short menu of options on difficulty level. It could look something like this:

DIFFICULTY LEVEL 1. Easy 2. Moderate 3. Advanced

The difficulty levels determine the level of difficulty. If that isn't clear, then read on. Easy means only single digit integers; moderate means double digit integers; and advanced means 4-digit floating point numbers (numbers with decimal parts - 2 digits to the right of the decimal point and 2 digits to the left). After the user picks the level they desire, your program presents problems that look like this:

45 + 9 = _ 34 - 88 = _ 24.53 - 1.23 = _ etc

But for each problem presented, the user is given a chance to answer. If the answer is correct, another problem is presented. If the answer is wrong, the user is to be given one more chance at that problem. Once your program has moved on to the next problem, the correctness/incorrectness of the preceeding problem is tallied. The number of correct and incorrect answers are to be presented at the termination of the quiz along with percentage correct.

So, how are the problems produced? Well, you will use the random number generator provided by the compiler to determine:

    1. the values to be added or subtracted

    2. whether the problem is addition or subtraction

Remember to put srand(time(NULL)); in your main function only and near the beginning and not in a loop. Be sure to #include <ctime> also. This call to the function srand() will seed the random number generator. A subsequent call to rand() will then return a random number.

You will use functions in this program. You are to decide on the structure of the main and what functions you are to use, with some required exceptions, these are explained below. You will be graded on how well you use functions and on your program layout.

Here are the required functions:

    • Include an overloading of the function named rand_num. One version is to have two parameters of type int and returns an int. The other is to have two parameters of type float and returns a float. Both of these functions will do the same thing, but be implemented a little differently. For each, the first parameter represents the lower end-point of an interval and the second parameter represents the upper end-point of the same interval. The function is to return a random value (int for one version, float for the other version) from that interval. For example, rand_num(4, 34); would return an integer chosen at random from the interval [4, 34]. The call rand_num(3.41, 56.78);would return a random number from the interval [3.41, 56.78].

      • As stated above, rand() returns a long integer from the interval [0,RAND_MAX], and RAND_MAX is ~2 billion. So, you might be wondering how to generate floating point random numbers. Let's consider an example: you want random floats from [23.45, 12.56]. So, find a random int from [2345, 1256] and then divide by 100. Smart, eh? So now this begs the question, "what if a float with more than 2 dec places is passed to the function?" There is a very simple fix for this potential problem. Can you figure it out?

    • Include a template function called the_problem that will be passed two values to represent the lower and upper end-points of an interval. It will immediately call the rand_num() function twice to choose (randomly) the operands for constructing the problem to present to the user. That function should start out like this:

      • template ??? the_problem(const T min_val, const T max_val) { T lhs = rand_num(min_val, max_val); T rhs = rand_num(min_val, max_val); . . . }

    • You decide what it does after that and what it should return (or not). Notice that if you pass floats to the_problem, it will call the float version of rand_num(), and if you pass it ints it will call the int version of rand_num().

    • Include also a template function to output the problem. This will allow your program to make integer output look nice, and float output to look nice. This last function is necessary because all float values for this program will be required to be only 2 decimal values. So, you will need to include the magic code we gave you in hw #3

      • cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);

    • and you don't want integers forced to have .00 stuck on the ends.

Here is a suggested listing of functions you can use in your program. You may include others if you see fit. You can opt not to use any of this; but you must use functions. It's up to you.

    • a displayMenu function that has no parameters and returns a char

    • a isCorrect function that returns a bool (correct or not) and is passed enough information to reconstruct the problem for that determination.The return value is determined in this function by whether or not the user's answer is correct for the given problem.

    • a displayMessage function that returns nothing but outputs "correct" or "incorrect" dependent on the value returned by the preceding function.

    • a displayFinalResults function that returns nothing and has two parameters: the number correct and the number wrong.

Once the user has finished the quiz, prompt them to see if they'd like to do it again. When you submit, play the quiz once and opt out of a second go round.

Optional: If this isn't enough for you, include a function that will choose at random from a list (your own)of 4 output messages for good answers and a list (your own) of 4 output messages for wrong answers. Of course, make the messages appropriate.

Another Optional: Make the number of tries at each problem a prompted for parameter for each run of the quiz at the beginning of the quiz.

Final Note: Don't underestimate the complexity and yaddi yaddi yaddi. Start to work on it NOW .

When you submit: Before submitting, change the constant that you set up for the number of questions from 10 to 3. Now,

    1. pick advanced

    2. answer one of the questions incorrectly twice, another question incorectly once, and the one questin correctly on your first try.

    3. quit (or, kwit)

As always, if you have questions, don't hesitate to ask your instructor or the lab help if you need help. Don't ask your barber or hairdresser.