I am currently working on an assignment following a lab previously completed. I
ID: 3757942 • Letter: I
Question
I am currently working on an assignment following a lab previously completed. I am wondering how to fix my program in C so that I can enter multiple answers separated by commas.
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 pointerto 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: correct2: correct (correctly identified as false)3: false4: falseThe user has 2 correct answers out of 4, so the score should be 0.5.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>
//defining structure of questions
typedef struct test {
//define question pointer as range of characters
char *question;
//multiple (1) or single (0) correct answers
int multiple;
//correct answer (integer)
int correct[4];
};
int main()
{
//structure test (t) as an array of 5 questions with integer correct answer
struct test t[5] = {
//print question followed by carriage return and each answer followed by carriage return followed by integer value of correct answer
{ { "Santiago is the capital of: 1. Argentina 2. Chile 3. Bolivia 4. Paraguay " },{ 0 }, { 0, 1, 0, 0 } },
{ { " Athens is a city in: 1. Ohio 2. Greece 3. Bulgaria 4. Macedonia " },{ 1 }, { 1, 1, 0, 0 } },
{ { " London is a city in: 1. Kentucky 2. Ireland 3. Ontario 4. United Kingdom " },{ 1 }, { 1, 0, 1, 1 } },
{ { " Helsinki is the capital of: 1. Finland 2. Russia 3. Sweden 4. Norway " },{ 0 }, { 1 , 0, 0, 0} },
{ { " Vientiane is the capital of: 1. Cambodia 2. Laos 3. Vietnam 4. Thailand" },{ 0 }, { 0, 1, 0, 0} }
};
int score = 0;
int *answer;
int i;
int r[5] = { 0, 1, 2, 3, 4 };
for (int i = 4; i >= 0, i--;) {
srand(time(NULL));
int j = rand() % (i + 1);
int option = r[i];
r[i] = r[j];
r[j] = option;
}
for (i = 0; i < 5; i++)
{
printf("%s", t[r[i]].question);
if (t[r[i]].multiple == 1) {
printf(" This is a multiple answer question enter all correct answer choices separated by commas: ");
}
else {
printf(" This is a single answer question enter answer choice (1, 2, 3, or 4): ");
}
scanf("%s", &answer);
}
}
if (answer == t[r[i]].correct)
{
score = score + 1;
printf(" you are correct ");
}
else
{
score = score + 0;
printf(" you are incorrect ");
}
}
float percent = ((float)score / 5) * 100.0f;
printf("your final score is %i out of 5 ", score);
printf("your grade is %f percent ", percent);
getchar();
system("pause");
return 0;
};
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>
//defining structure of questions
typedef struct test {
//define question pointer as range of characters
char *question;
//multiple (1) or single (0) correct answers
int multiple;
//correct answer (integer)
int correct[4];
};
int main()
{
//structure test (t) as an array of 5 questions with integer correct answer
struct test t[5] = {
//print question followed by carriage return and each answer followed by carriage return followed by integer value of correct answer
{ { "Santiago is the capital of: 1. Argentina 2. Chile 3. Bolivia 4. Paraguay " },{ 0 }, { 0, 1, 0, 0 } },
{ { " Athens is a city in: 1. Ohio 2. Greece 3. Bulgaria 4. Macedonia " },{ 1 }, { 1, 1, 0, 0 } },
{ { " London is a city in: 1. Kentucky 2. Ireland 3. Ontario 4. United Kingdom " },{ 1 }, { 1, 0, 1, 1 } },
{ { " Helsinki is the capital of: 1. Finland 2. Russia 3. Sweden 4. Norway " },{ 0 }, { 1 , 0, 0, 0} },
{ { " Vientiane is the capital of: 1. Cambodia 2. Laos 3. Vietnam 4. Thailand" },{ 0 }, { 0, 1, 0, 0} }
};
float score = 0, currentScore;
char answer[10];
int i;
int ans;
int ansM[4];
int r[5] = { 0, 1, 2, 3, 4 };
for (int i = 4; i >= 0; i--) {
srand(time(NULL));
int j = rand() % (i + 1);
int option = r[i];
r[i] = r[j];
r[j] = option;
}
for (i = 0; i < 5; i++)
{
printf("%s", t[r[i]].question);
if (t[r[i]].multiple == 1) {
printf(" This is a multiple answer question enter all correct answer choices separated by commas: ");
}
else {
printf(" This is a single answer question enter answer choice (1, 2, 3, or 4): ");
}
scanf("%s", answer);
if(t[r[i]].multiple == 0)
{
ans = answer[0] - '0';
if (t[r[i]].correct[ans-1] == 1)
{
score = score + 1;
printf(" you are correct ");
}
else
printf(" you are incorrect ");
}
else
{
ansM[0] = ansM[1] = ansM[2] = ansM[3] = 0;
int j = 0;
while(answer[j] != '')
{
if(answer[j] == ',')
{
j++;
continue;
}
ansM[(answer[j] - '0') - 1] = 1;
j++;
}
currentScore = 0;
for(j = 0; j < 4; j++)
{
if(ansM[j] == t[r[i]].correct[j])
currentScore += 0.25;
}
if(currentScore == 1)
printf(" you are correct ");
else
printf(" you are incorrect ");
score += currentScore;
}
}
float percent = ((float)score / 5) * 100.0f;
printf("your final score is %f out of 5 ", score);
printf("your grade is %f percent ", percent);
getchar();
//system("pause");
return 0;
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.