Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//prototypes
int arithGame(int max, int op);
int guessGame();
int max(int a, int b);
int min(int a, int b);
//mian function
int main() {
int choice, score, op, max;
//seed random number generator.
srand(time(0));
printf("Please make a selection from the following: \n");
printf("1. Play Arithmetic Game. \n");
printf("2. Play Guessing Game. \n");
printf("3. Print Score. \n");
printf("4. Quit. \n");
scanf("%d", &choice);
while (choice != 4) {
switch (choice) {
case 1: printf("Would you like, 1)Addition or 2)Multiplication? \n");
scanf("%d", &op);
printf("What is the maximum number you would like? \n");
scanf("%d", &max);
score = arithGame(max, op);
break;
case 2: score = guessGame();
break;
case 3: printf("Your score is %d. \n", score);
break;
default: printf("\n\n Thank you for playing!");
break;
}
printf("Please make a selection from the following: \n");
printf("1. Play Arithmetic Game. \n");
printf("2. Play Guessing Game. \n");
printf("3. Print Score. \n");
printf("4. Quit. \n");
scanf("%d", &choice);
}
return 0;
}
/*Pre-Conditions: This function takes in two integers, max and op.
This function gives the user 10 arithmetic questions, where
each operand ranges from 1 to max, inclusive. The value of
operator dictates whether the problems are addition or
multiplication problems.
Namely, if op is 1, they are addition problems, otherwise,
they are multiplication problems.
*Post-Conditions: The function returns the number problems solved
correctly, and prints out for the user after their answer
whether or not they got the question correct.
*/
int arithGame(int max, int op) {
int ans, score = 0, i, variable, variable2;
if (op==1) {
for(i=1;i<=10;i++) {
variable = 1+rand()%max;
variable2 = 1+rand()%max;
printf("What is %d + %d? \n", variable,variable2);
scanf("%d", &ans);
if(ans== (variable) + (variable2)){
printf("Correct! Great job! \n");
score++;
}
else
printf("Sorry, that's incorrect, the answer is %d \n", (variable) + (variable2));
}
printf("Your score for the round is %d. \n\n", score);
}
else {
for(i=1;i<10;i++) {
variable = 1+rand()%max;
variable2 = 1+rand()%max;
printf("What is %d * %d? \n", variable,variable2);
scanf("%d", &ans);
if(ans== (variable) * (variable2)){
printf("Correct! Great job! \n");
score++;
}
else
printf("Sorry, that's incorrect, the answer is %d \n", (variable) * (variable2));
}
printf("Your score for the round is %d. \n\n", score);
}
return score;
}
/*Pre-Conditions: This function does not take in any parameters.
This function allows the user to play the guessing game
where the randomly generated number lies in between 1
and 100, inclusive.
*Post-Conditions: The value returned is the score of the user in the game.
This score is 17 minus the number of guesses unless this
value is lower than 0 or greater than 10.
In these cases, 0 and 10 are returned, respectively.
*/
int guessGame() {
int secnum, guess, num_guesses = 0, score;
secnum = rand()%100+1;
while (guess != secnum) {
printf("Enter the guess! \n");
scanf("%d", &guess);
num_guesses++;
if (max(guess, secnum) == guess)
printf("Your guess is too high, try again. \n");
else if (min(guess, secnum) == guess)
printf("Your guess is too low, try again. \n");
else;
}
printf("Great, you guessed the correct number %d in %d guesses. \n", secnum, num_guesses);
score = 17 - num_guesses;
if (score < 0)
score = 0;
else if (score > 10)
score = 10;
else ;
printf("Your score for the round is %d. \n\n", score);
}
/*Pre-Conditions: This function takes in two integers, a and b.
*Post-Conditions: This function returns the larger of a and b.
*/
int max(int a, int b) {
if (a > b)
return a;
else
return b;
}
/*Pre-Conditions: This function takes in two integers, a and b.
*Post-Conditions: This function returns the smaller of a and b.
*/
int min(int a, int b) {
if (a < b)
return a;
else
return b;
}