Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Math Quiz Your task is to write a C program that randomly generates, conducts, a

ID: 3536339 • Letter: M

Question

Math Quiz



Your task is to write a C program that randomly generates, conducts, and scores a simple arithmetic quiz based on certain user parameters. The arithmetic quiz will ask questions dealing with simple addition, subtraction, multiplication, and division. The user will be asked to enter the number of questions for the quiz, between 1 and 20 (be sure to verify and re-prompt if necessary) and a difficulty level between 1 and 4 (also verify).

The difficulty will determine the range of numbers that are randomly generated for the questions:



So a quiz with a difficulty of three might have a problem like "75+4" but would not have a problem such as "-75+4", since negative numbers are only included at difficulty level four.

Once these parameters are set, the quiz will begin, providing the user with simple arithmetic statements, where the operations (+, -, *, /) and operands are randomly generated using rand(). In the case of division, do not allow the second operand to be zero. Make sure the quiz is different every time!

For each question the user is prompted to enter their answer and will be told if they are correct or incorrect. There must be three correct and incorrect responses, and the one used will be randomly selected. If you take a look at the example below, you will see that "Nice!", "Good job!" and "You're right!" are the three randomly selected responses for correct answers, and there are three more for incorrect answers. If the user answered incorrectly, the program also prints the correct answer.

After all the questions are given and answered, the score is provided by stating the number of questions correct / number of questions asked.

For the most part how you write the program solution is up to you; however it must include at least these functions (feel free to add more):

genQuestion() %u2013 Used to randomly generate and output a random question based upon a difficulty which is passed as a parameter. Returns the answer of the question.

answerQuestion() %u2013 Prompts user for an answer, checks if it is correct based on the correct answer, which is passed to the function as a parameter. Returns an indicator of whether or not the answer was correct.

response() %u2013 Outputs a response to the user%u2019s answer based upon a passed parameter which indicates whether the answer was correct or incorrect. The function randomly selects one of the three responses, either from the correct responses or the incorrect responses.

Example Runs:


Run 1:
How many questions for this test (1 - 20)? 5
Select difficulty (1 - 4): 2
Question 1: 23 / 29 =
Enter Answer: 0
Nice!
Question 2: 47 * 8 =
Enter Answer: 376
Good Job!
Question 3: 50 - 5 =
Enter Answer: 42
Sorry!
The correct answer was 45
Question 4: 7 + 24 =
Enter Answer: 31
Good Job!
Question 5: 32 / 43 =
Enter Answer: 0
Nice!
Your score was 4/5




Run 2:
How many questions for this test (1 - 20)? 25
How many questions for this test (1 - 20)? 4
Select difficulty (1 - 4): -6
Select difficulty (1 - 4): 1
Question 1: 2 - 7 =
Enter Answer: -5
Nice!
Question 2: 4 + 4 =
Enter Answer: 9
Sorry!
The correct answer was 8
Question 3: 10 / 9 =
Enter Answer: 1
You're right!
Question 4: 7 / 1 =
Enter Answer: 7
Good Job!
Your score was 3/4

Explanation / Answer

#include <stdio.h>
#include <stdlib.h> /* required for randomize() and random() */

int main(){
    int noqs,difficulty,cAnswer,i;
    printf("How many questions for this test(1-20)?");
    scanf("%d",&noqs);
    printf("Select difficulty(1-4):");
    scanf("%d",&difficulty);
    for(i=0;i<noqs;i++){
        cAnswer=genQuestion(difficulty);
        if(answerQuestion(cAnswer)){
            printf("%s",*response(1));
            }
        else{
            printf("%s",*response(0));
            }
       
        }
    }
   
int genQuestion(int difficluty){
    int rand1,rand2,answer,operation;;
    switch(difficulty){
        case 1 : rand1 = random(10); rand2 = random(10);break;
        case 2 : rand1 = random(50); rand2 = random(50);break;
        case 3 : rand1 = random(100); rand2 = random(100);break;
        case 4 : rand1 = random(100); rand2 = -(random(100));break;
    }
    operation = random(4);
    switch(operation){
        case 1 : answer=rand1+rand2;
        case 2 : answer=rand1-rand2;
        case 3 : answer=rand1*rand2;
        case 4 : while(rand2==0){if(difficulty == 1)rand2 = random(10);if(difficulty == 2)rand2 = random(50);if(difficulty == 3)rand2 = random(100);if(difficulty == 4)rand2 = -random(100);}answer=rand1/rand2;
       
    }
    return answer;

    }
   
int answerQuestion(int cA){
    int uA;
    printf("Enter Answer : ");
    scanf("%d",uA);
    if(cA == uA)
        return 1;
    else
        return 0;
       
    }
   
char *response(int rsp){
    int userRsp = random(3);
    char actResp[300];
    switch(userRsp){
        case 1 : if(rsp)actResp = "Nice!"; else actResp = "Sorry!";
        case 1 : if(rsp)actResp = "Your Right!"; else actResp = "Sorry no!";
        case 1 : if(rsp)actResp = "Good Job!"; else actResp = "Wrong Answer!";
        }
    return actResp;
   
    }