CS 115 Program 4 Arithmetic Tutor Spring 2001

Due Date: Wednesday, April 11, 2001

You are going to write a program to help students practice their basic arithmetic skills, addition, subtraction, multiplication and division. You'll use a random number generator to create problems for solution, check the student's answers and give a performance report at the end.

The student identifier is simply an integer the student types in when the program starts. It is shown on the final report.

Phase I

Write a program that will create an addition problem, present it to the student, check their answer and give them feedback about whether they answered correctly or not. It should use numbers between 0 and 99 as parts of the problem. It should present these problems repeatedly until the student says they want to stop. Then it should report the student's performance, how many questions were attempted and how many were answered correctly.

The problems should use randomly generated numbers from 0 to 99. Write a function, getrand, that will return a random number from 0 to 99. Look at the code you used in the last program to generate a number in the desired range. Don't worry about calling srand() at this point; the seed will be given a default value if you don't call srand() at all. Use getrand to write a function, called add, that will perform the work described above. This function should return two numbers, which are the number of problems the student tried and the number they got right.

Also write a function called report that will take three numbers as parameters, the student id, the number of problems attempted and the number correct, and write the information to the screen.

Note the formatting of the addition problem: the numbers are lined up on the right. You can do this using the function call cout.width(constant); where constant is the number of columns you want the numbers to take up. This function call takes effect on only the next thing which is output, so it may have to be repeated a few times.

Example Run: (user input in bold)
Enter student id: 1234

Addition
	  23
	  45
       -----
	  68

Right!!
Again? y/n y

Addition
	  49
	   7
       -----
	  56

Right!!
Again? y/n n

Student Number 1234
	Number Attempted 2
	Number Correct 2
	Percent Correct 100.0%

Phase II


Write a function called menu with no parameters to display a menu of operations and return the student's choice as a character. Allow the student to type in upper or lower case letters at any of the inputs.

Use menu to get the student's choice in your main function, then use a switch statement to call four different functions, add, sub, mul, or div, depending on what the student's choice was. The student will only practice with one operation during one run of the program. That is, the menu will be shown only once when the program is run.

The four functions will be very similar but not all precisely the same. For subtraction, you want to make sure that the answer to the problem is a positive (or zero) number, not a negative number. You may have to get random numbers more than once to make sure this is true. For division, you, of course, want to make sure you don't ask the student to divide by zero. You also want the answer to be a whole number, not a fraction. Again, you will have to check your random numbers to make sure this is true.

Use an enumerated type to represent the four operations: add, subtract, multiply and divide. This will make things in your switch statement code clearer.

Example Run: (user input in bold)
Enter student id: 1234

Arithmeic Tutor

A = Addition 
S = Subtraction 
M = Multiplication 
D = Division 
Q = quit 

Enter choice - A S D M Q a

Addition 
	  92
	  54
	-----
	148
Wrong!!
The right answer is 146
Again? y/n y

Addition 
	  44
	  84
	-----
 	128
Right!!
Again? y/n n

Student Number 1234
	Number Attempted 2
	Number Correct 1
	Percent Correct 50.0%

Phase III

One factor that should be adjustable is the level of difficulty of the questions. The student will be able to specify a difficulty level of 1 to 5, which means the maximum number of digits in the numbers in the problems. So a difficulty level of 1 would give problems like 7 + 5 or 3 - 2, a difficulty level of 3 would give problems like 298 + 41 or 732 * 382 or 781 - 3. This can be accomplished by the expression below, which uses the difficulty level as a power of 10 and then uses this number to find the random number for the problem.

	number = rand() % (10 raised to the power of the difficulty)

You should call a mathematical function to find the expression in the parentheses. Rewrite the getrand function so that it has one parameter, the difficulty. It returns the next random number in the range 0 through 10 to the power of difficulty -1. If the difficulty is 1, the numbers will be from 0 to 9; if the difficulty is 2, the numbers will be from 0 to 99; if the difficulty is 3, the numbers will be from 0 to 999, and so on. The main function will have to ask the student for this value (check for a correct value), and you will need to adjust the four operator functions to accept this value from the main, so it can be passed to getrand.


Example Run: (user input in bold)
Enter student id: 1234
Enter a number for a seed: 834

Enter level of difficulty (1 is easiest, 5 is hardest) 3

A = Addition 
S = Subtraction 
M = Multiplication 
D = Division
Q = quit 

Enter choice - A S D M Q a

Addition 
	  902
	  534
	-----
1438
Wrong!!
The right answer is 1436
Again? y/n y

Addition 
	  434
	  874
	-----
 1308
Right!!
Again? y/n n

Student Number 1234
	Number Attempted 2
	Number Correct 1
	Percent Correct 50.0%
	At difficulty level 3

Testing:
Capture test runs at each phase, ones where the student missed some problems, missed them all, got them all right, etc. Test your program for a while without calling srand(). This will produce the same random numbers each time you run the program. For the final program, ask the user for a number at the beginning of the program for the seed, as you did in the last program.

Design
Write a pseudocode design for Phase II at detail Level 2. This will be discussed in a designated recitation, and you will be responsible for having it in readable form at that time. Please read the documentation standard. As you can see from looking at the grading sheet, we will be looking to see how you meet these standards. Every function needs a description of its purpose and its parameters. Local variables need documentation too.

As described in the documentation standard, turn in the following, neatly stapled, in this order: