IN C LANGUAGE This assignment administers a math quiz to a student. The math que
ID: 3757097 • Letter: I
Question
IN C LANGUAGE
This assignment administers a math quiz to a student. The math questions are in the file
/gaia/class/student/srivatss/csc60_18/shell/data.input
There are 10 questions in the file , each question of the format :
operator operand1 operand2
Step 1. Declare an array int quizData [ 30 ] and fill it up using the data that is available in the file.
Do not write a program to read from the file. We will do that later.
Step 2. Could you shuffle the questions. I show you in the class how to shuffle the questions without using any additional array.
Step 3. Use a for loop to iterate through the array quizData to display each question and ask the user for a correct answer. If the answer is correct, display an encouraging text and update the score by one.
For instance, the first question and the user response (in red) might be
what is 6 plus 2 ?
8
Good Job. Your current score is : 1
The second question and the user response could be
what is 9 minus 2
7
Good Job. Your current score is 2
The third question should be
what is 3 times 4
12
Good Job. Your current score is 3
and so on.
Step 4. Some of the functions you need are : add, sub, mult, and div and they are listed here. You should use these functions for comparing the answer keyed in by the user. NOTE: Each function should be in a separate file.
// add.c
int add ( int a, int b)
{
return a+b;
}
// sub.c
int sub ( int a, int b)
{
return a-b;
}
// mult.c
int mult ( int a, int b)
{
return a*b;
}
// div.c
int div ( int a, int b)
{
if ( b != 0 )
return a/b;
else
return 0;
}
Step 5. Use Makefiles to compile all your programs.
Here is the outline of the program you would write
int score= 0;
int quizData [30] ; // fill it up from data in the file.
write code to randomize the data in the array quizData
for ( i = 0 ; i < 30 ; i++ ) {
// use the switch case to prompt the questions to the user.
// use scanf function to get answer from the user
// compare the value the user entered with the expected answer.
if they are same, increment the score
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int i;
int response;
int correctAnswers = 0;
int incorrectAnswers = 0;
printf(" Math Quiz ");
printf("Please enter # of problems you would wish to try:");
scanf("%d", &response);
if(response == 0)
{
printf(" Thanks for playing! ");
return 0;
}
for(i=0; i<response; i++)
{
int answer = 0;
int a = rand() % 12;
int b = rand() % 12;
printf(" %d * %d = ",a ,b);
scanf("%d", &answer);
if((a * b) == answer){
printf(" Congratulations You are correct! ");
correctAnswers++;
}
else{
printf("Sorry you were incorrect! ");
incorrectAnswers++;
}
}
printf(" Your Results: ");
printf("Number Incorrect: %d ", incorrectAnswers);
printf("Number Correct: %d ", correctAnswers);
if(correctAnswers > incorrectAnswers){
printf("You Passed! Good work! ");
}
else{
printf("You did not pass! You need more work! ");
}
return 0;
}