In this lab we are going to bring two enhancements to the program that you wrote
ID: 3758093 • Letter: I
Question
In this lab we are going to bring two enhancements to the program that you wrote in lab 5. In lab 5 we had said that for each question there were four possible answers and that only one would be correct. We shall stick to four possible answers but we want to support questions in which several answers may be correct (you’ll probably have to modify the structure that stores a question for that). The structure should contain “something” to tell whether there is only one or multiple correct answers. The second enhancement is that we want to write a function that returns the score for one question. The function will take as parameter a pointer to one structure that holds a question, and the user’s answer as a string. For questions with multiple correct answers, we expect a comma separated list of integers, for instance “2,3” if the user thinks that the two correct choices are choices 2 and 3. The function will return a float that will be the score. The score will be computed as follows: If there is only one correct answer, 1 if the user answered correctly, 0 otherwise. If there are several possible answers, the score will be the ratio of correct answers, rounded to the nearest half-point. For instance: Correct answers are 1 and 4. The user answered “1,3” 1: correct 2: correct (correctly identified as false) 3: false 4: false The user has 2 correct answers out of 4, so the score should be 0.5.
This is what I have written for the code so far, and it is not working, can you please help me correct my code so that it runs, and meets the specifications given above?
#include <stdio.h>
#include <string.h>
#include <errno.h>
//Define a structure of a Quiz named Quiz1
//include a character that reserves space for Question
//include an integer for Answer
typedef struct Quiz {
char *Question;
int multiple;
int correct[4];
}Quiz1;
int main()
{
//declare an array of Quiz1
//include 5 questions, all with 4 multiple choice answers for each question
//include correct answer for each question.
Quiz1 Quiz[5] = {
{{ "Kansas is a state in? 1.North America 2.United States 3.South America 4.Australia " },{ 1 },{ 1, 1, 0, 0 }},
{{"Kansas State University is located in? 1.Manhattan 2.Salina 3.Oklahoma city 4.Denver " },{ 1 },{ 1, 1, 0, 0 }},
{{"Fiedler Hall is a building at? 1.KSU 2.OU 3.UT 4.OSU "},{ 0 },{ 1, 0, 0, 0 }},
{{"The buildings in the engineering complex are? 1.Fiedler 2.Nichols 3.Chalmers 4.Seaton "},{ 0 },{ 1, 0, 0, 0 }},
{{"The football stadium at KSU is called? 1.Bill Snyder Family Stadium 2.Kauffman 3.Arrowhead 4.DKR Memorial Stadium "},{ 0 },{ 1, 0, 0, 0 }}
};
//variables: score=0 (integer)
//answer (integer)
//variable i that includes values =0,1,2,3,4 (integer)
//r1,r2,r3,r4,r5 (integer) to make questions come up in a random order
int score = 0;
int answer;
int i;
int r[5] = { 0, 1, 2, 3, 4 };
int L;
int ans[2];
//these statements following are used to randomize the value assigned to one of the r value
//so that it never equals another one of the r values
for (int i = 4; i >= 0, i--;)
{
srand(time(NULL));
int j = rand() % (i + 1);
int choice = r[i];
r[i] = r[j];
r[j] = choice;
}
//we need to now assign a randomized r value with a question and answer from the earlier structure
//so that we can print them using if statements
//make sure that the r statement that prints is not the same as the previously printed r statements
//each one of the if statements also keeps track of the score so that it can be totalled at the end
for (i = 0; i < 5; i++)
{
print("%s", Quiz[r[i]].Question);
if (Quiz[r[i]].multiple == 1) {
printf(" This is a question that has multiple answers, please enter all correct answers separated by commas: ");
L = 0;
char p[256];
scanf("%s", &p);
char*token = strtok(p, ",");
while (token) {
ans[L] = atoi(token);
token = strtok(NULL, ",");
L++;
}
}
else {
printf(" This is a question that only has one answer(1,2,3,or 4): ");
}
scanf("%d", &answer);
}
{
printf("%s", Quiz[r[i]].Question);
printf(" enter your choice: ");
scanf("%d", &answer);
if (answer == Quiz[r[0]].correct[0])
{
score = score + 1;
}
}
if ((r[0] <= 5) || (r[1] >= 0) || (r[1] != r[0]))
{
printf("%s", Quiz[r[1]].Question);
printf(" enter your choice: ");
scanf("%d", &answer);
if (answer == Quiz[r[1]].correct[1])
{
score = score + 1;
}
}
if ((r[2] <= 5) || (r[2] >= 0) || (r[2] != r[1]) || (r[2] != r[0]))
{
printf("%s", Quiz[r[2]].Question);
printf(" enter your choice: ");
scanf("%d", &answer);
if (answer == Quiz[r[2]].correct[2])
{
score = score + 1;
}
}
if ((r[3] <= 5) || (r[3] >= 0) || (r[3] != r[2]) || (r[3] != r[1]) || (r[3] != r[0]))
{
printf("%s", Quiz[r[3]].Question);
printf(" enter your choice: ");
scanf("%d", &answer);
if (answer == Quiz[r[3]].correct[3])
{
score = score + 1;
}
}
if ((r[4] <= 5) || (r[4] >= 0) || (r[4] != r[3]) || (r[4] != r[2]) || (r[4] != r[1]) || (r[4] != r[0]))
{
printf("%s", Quiz[r[4]].Question);
printf(" enter your choice: ");
scanf("%d", &answer);
if (answer == Quiz[r[4]].correct[4])
{
score = score + 1;
}
}
//after someone has input all of the answers, the last thing to do is print the score that has
// been totalled after each question was answered
printf(" Your score:%d", score);
//to finish the program we need to have a system pause so that we can see the result
//exit (0);
getchar();
system("pause");
return 0;
}
Explanation / Answer
Solution :
There is a compilation info in the code :
stdout
Once we solve the compilation error, we can get the correct code for getting score of the multiple choice question.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.