**Introduction to C - Programming Assignment #3**

**Assigned: 9/22/06, Friday**

**Due: 10/7/06, Saturday at 3:00am WebCT Time**

**Objective**
1. To give students practice in using functions

**Problem: Educational Software**
Your little brother is having trouble with arithmetic. Your parents realize that after taking a few weeks of your C programming course, that you could potentially write a computer program that will allow him to practice his arithmetic skills.

In particular, your program will allow your brother to play two separate games:

1) A game where he has to complete several additions or multiplications.

2) A game where he has to determine a secret number after being told if his guesses are too high or too low.

Your program should prompt your brother with the following menu:

1) Play Arithmetic Game

2) Play Guessing Game

3) Print Score

4) Quit

If he chooses option 1, then you should prompt him with the following menu choices:

1) Addition

2) Multiplication

Your program should then prompt him for the maximum value of the numbers to be used in the problems and the total number of questions.

Then it should play the game by prompting him with the desired number of additions/multiplications. Your program should keep track of how much time he takes for these problems. In addition, 5 penalty seconds should be added for each incorrect response. The total time he takes (taking into account the penalty seconds) will be used to determine his score for the round.

If he chooses option 2, then you should prompt him for the maximum integer, n, for the guessing game. Then your program should generate a randominteger in between 1 and n, inclusive. After that, it should prompt your brother for his first guess. After each guess, your program should tell him whether to guess higher or lower. This continues until he gets the number exactly. Your program should also keep track of how much time the game takes to play. This value will be used to determine his score for the round.

For option 3, simply report your brother's total score, which is the sum of his scores from each round he plays.

*Scoring Details*
For the arithmetic game, the score your brother earns is equal to the total amount of time (in seconds) it took him to finish the problems (including penalty seconds) divided by the number of problems he solved.

For the guessing game, the score your brother earns is equal to the total amount of time (in seconds) it took him to guess the secret number divided by two times the number of digits in the maximum number allowed in the game. (For example if the maximum number was 1000, and he took 15 seconds to guess the correct number, then his score would be 15/(2*4) = 1.875.)

The score in seconds then must be converted to an integer number of points in between 0 and 10. In particular, the conversion works as shown in the chart below:

Time, t, (in seconds)

Corresponding Score

t < 1

10

1 ≤ t < 2

9

2 ≤ t < 3

8

3 ≤ t < 4

7

4 ≤ t < 5

6

5 ≤ t < 6

5

6 ≤ t < 7

4

7 ≤ t < 8

3

8 ≤ t < 9

2

9 ≤ t < 10

1

t ≥ 10

0

*Implementation Details*
You will be required to write four functions with the prototypes given below. (Note: you may write other functions as well, but these three are required.) Your functions should do what the comments for them below specify:

[FONT=Courier New, monospace]// This function gives the user quantity arithmetic[/FONT]

[FONT=Courier New, monospace]// questions, where each operand ranges from 1 to max,[/FONT]

[FONT=Courier New, monospace]// inclusive. The value of operator dictates whether[/FONT]

[FONT=Courier New, monospace]// the problems are addition or multiplication problems.[/FONT]

[FONT=Courier New, monospace]// Namely, if op is 1, they are addition problems,[/FONT]

[FONT=Courier New, monospace]// otherwise, they are multiplication problems.[/FONT]

[FONT=Courier New, monospace]// The function returns the number of seconds the user took[/FONT]

[FONT=Courier New, monospace]// to play the entire game, divided by the number of [/FONT]

[FONT=Courier New, monospace]// problems they solved. [/FONT]

[FONT=Courier New, monospace]

**double arithGame(int max, int quantity, int op);**[/FONT]

[FONT=Courier New, monospace]// This function allows the user to play the guessing game[/FONT]

[FONT=Courier New, monospace]// where the randomly generated number lies in between 1 [/FONT]

[FONT=Courier New, monospace]// and max, inclusive. The value returned is the number of[/FONT]

[FONT=Courier New, monospace]// seconds the user took to finish the game divided by the[/FONT]

[FONT=Courier New, monospace]// 2 times the number of digits in the number max.[/FONT]

[FONT=Courier New, monospace]

**double guessGame(int max);**[/FONT]

[FONT=Courier New, monospace]// Returns the number of digits in number.[/FONT]

[FONT=Courier New, monospace]

**int numDigits(int number);**[/FONT]

[FONT=Courier New, monospace]// Returns the number of points the user has earned based[/FONT]

[FONT=Courier New, monospace]// on time. In particular, if time is less than 1, 10 is [/FONT]

[FONT=Courier New, monospace]// returned. Otherwise, if it is less than 2, 9 is [/FONT]

[FONT=Courier New, monospace]// returned, etc. If time is greater than or equal to 10,[/FONT]

[FONT=Courier New, monospace]// then 0 is returned.[/FONT]

[FONT=Courier New, monospace]

**int numPoints(double timesec);**[/FONT]

*Other Useful Information*
Seed the random number generator at the beginning of your program. Do this exactly once. Here is the line of code:

[FONT=Courier New, monospace]srand(time(0));[/FONT]

In order to use this you need to include stdlib.h and time.h atthe top of the program.

Please use the following constants for ADD and MULT

[FONT=Courier New, monospace]#define ADD 1[/FONT]

[FONT=Courier New, monospace]#define MULT 2[/FONT]

In order to calculate how much time something takes, you can use the time function. In particular, the function call time(0) returns a double that represents the number of seconds after some time. In order to effectively use this, you must call the function twice: once right before you start what you want to time, and once right afterwards. Subtract these two values to obtain the amount of time a segment of code took. Here is a short example:

[FONT=Courier New, monospace]int start = time(0);[/FONT]

[FONT=Courier New, monospace]// Insert code you want to time here.[/FONT]

[FONT=Courier New, monospace]int end = time(0);[/FONT]

[FONT=Courier New, monospace]int timespent = end - start;[/FONT]

[FONT=Courier New, monospace]printf("Your code took %d seconds.\n", timespent);[/FONT]

In order to carry out the scoring function, it may be helpful to look at the following functions in the math library:

[FONT=Courier New, monospace]double ceil(double x);[/FONT]

[FONT=Courier New, monospace]double floor(double x);[/FONT]

Remember, if you want to convert a double to a corresponding integer, you can use a cast as in the example below, where we assume that value is an integer and seconds is a double:

[FONT=Courier New, monospace]value = (int)seconds;[/FONT]

**References**
Textbook: Chapters 3 and 4 Notes: Lectures on loops, functions, random

number generator functions

**Restrictions**
Name the file you create and turn in

*game.c*. Although you may use other compilers, your program must compile and run using Dev C++ or gcc. If you use your olympus account to work on this assignment, please follow the steps shown in class to create, compile, and test your program. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. You should also include comments throughout your code, when appropriate. If you have any questions about this, please see a TA.

**Deliverables**
A single source file named

*game.c* turned in through WebCT.

**Sample Output **
[FONT=Courier New, monospace]Please make a selection from the following:[/FONT]

[FONT=Courier New, monospace]1. Play Arithmetic Game.[/FONT]

[FONT=Courier New, monospace]2. Play Guessing Game.[/FONT]

[FONT=Courier New, monospace]3. Print Score.[/FONT]

[FONT=Courier New, monospace]4. Quit.[/FONT]

[FONT=Courier New, monospace]

**1**[/FONT]

[FONT=Courier New, monospace]Would you like, 1)Addition or 2)Multiplication?[/FONT]

[FONT=Courier New, monospace]

**1**[/FONT]

[FONT=Courier New, monospace]What is the maximum number you would like?[/FONT]

[FONT=Courier New, monospace]

**100**[/FONT]

[FONT=Courier New, monospace]How many problems do you want?[/FONT]

[FONT=Courier New, monospace]

**4**[/FONT]

[FONT=Courier New, monospace]What is 21+86?[/FONT]

[FONT=Courier New, monospace]

**107**[/FONT]

[FONT=Courier New, monospace]Correct, great job![/FONT]

[FONT=Courier New, monospace]What is 87+96?[/FONT]

[FONT=Courier New, monospace]

**173**[/FONT]

[FONT=Courier New, monospace]Sorry, that's incorrect, the answer is 183.[/FONT]

[FONT=Courier New, monospace]What is 86+70?[/FONT]

[FONT=Courier New, monospace]

**156**[/FONT]

[FONT=Courier New, monospace]Correct, great job![/FONT]

[FONT=Courier New, monospace]What is 55+4?[/FONT]

[FONT=Courier New, monospace]

**59**[/FONT]

[FONT=Courier New, monospace]Correct, great job![/FONT]

[FONT=Courier New, monospace]You took an average of 6.000000 seconds per question.[/FONT]

[FONT=Courier New, monospace]Your score for the round is 4.[/FONT]

[FONT=Courier New, monospace]Please make a selection from the following:[/FONT]

[FONT=Courier New, monospace]1. Play Arithmetic Game.[/FONT]

[FONT=Courier New, monospace]2. Play Guessing Game.[/FONT]

[FONT=Courier New, monospace]3. Print Score.[/FONT]

[FONT=Courier New, monospace]4. Quit.[/FONT]

[FONT=Courier New, monospace]

**2**[/FONT]

[FONT=Courier New, monospace]Enter the maximum number for the game.[/FONT]

[FONT=Courier New, monospace]

**100**[/FONT]

[FONT=Courier New, monospace]Enter the guess![/FONT]

[FONT=Courier New, monospace]

**50**[/FONT]

[FONT=Courier New, monospace]Your guess is too high, try again.[/FONT]

[FONT=Courier New, monospace]Enter your guess![/FONT]

[FONT=Courier New, monospace]

**30**[/FONT]

[FONT=Courier New, monospace]Your guess is too high, try again.[/FONT]

[FONT=Courier New, monospace]Enter your guess![/FONT]

[FONT=Courier New, monospace]

**10**[/FONT]

[FONT=Courier New, monospace]Your guess is too high, try again.[/FONT]

[FONT=Courier New, monospace]Enter your guess![/FONT]

[FONT=Courier New, monospace]

**5**[/FONT]

[FONT=Courier New, monospace]Your guess is too low, try again.[/FONT]

[FONT=Courier New, monospace]Enter your guess![/FONT]

[FONT=Courier New, monospace]

**7**[/FONT]

[FONT=Courier New, monospace]Your guess is too high, try again.[/FONT]

[FONT=Courier New, monospace]Enter your guess![/FONT]

[FONT=Courier New, monospace]

**6**[/FONT]

[FONT=Courier New, monospace]Great, you guessed the correct number 6 in 6 guesses in 10 seconds.[/FONT]

[FONT=Courier New, monospace]Your score for the round is 9.[/FONT]

[FONT=Courier New, monospace]Please make a selection from the following:[/FONT]

[FONT=Courier New, monospace]1. Play Arithmetic Game.[/FONT]

[FONT=Courier New, monospace]2. Play Guessing Game.[/FONT]

[FONT=Courier New, monospace]3. Print Score.[/FONT]

[FONT=Courier New, monospace]4. Quit.[/FONT]

[FONT=Courier New, monospace]

**3**[/FONT]

[FONT=Courier New, monospace]Your score is 13.[/FONT]

[FONT=Courier New, monospace]Please make a selection from the following:[/FONT]

[FONT=Courier New, monospace]1. Play Arithmetic Game.[/FONT]

[FONT=Courier New, monospace]2. Play Guessing Game.[/FONT]

[FONT=Courier New, monospace]3. Print Score.[/FONT]

[FONT=Courier New, monospace]4. Quit.[/FONT]

[FONT=Courier New, monospace]

**4**[/FONT]

[FONT=Courier New, monospace]Thank you for playing![/FONT]

Click to expand...