In C language Write a program that stores answers to ten questions in a characte
ID: 3740394 • Letter: I
Question
In C language
Write a program that stores answers to ten questions in a character array. Each question has one correct answer and four possible answers. (A,B,C,D).
Prompt the user for ten guesses and store the user’s answers in another array.
After the user is done entering the answers, show them the correct answer, their answer, and whether they got the question correct or not.
Also show them how many they got right, and how many they got wrong.
char guesses[10]; //this will store the user answers
Sample Execution:
Enter your guess for Question 1 (A,B,C,D): A
Enter your guess for Question 2 (A,B,C,D): B
Enter your guess for Question 3 (A,B,C,D): F
ERROR!! Guess must be A,B,C,D
Enter your guess for Question 3 (A,B,C,D): D
…
Enter your guess for Question 10 (A,B,C,D): D
Here are the results of your quiz:
Question 1: Correct Answer: A Your Answer: A Correct.
Question 2: Correct Answer: B Your Answer: B Correct.
Question 3: Correct Answer: C Your Answer: D Incorrect.
…
Question 10: Correct Answer: A Your Answer: D Incorrect.
You got 4 questions right.
You got 6 questions wrong.
You got 40%.
Explanation / Answer
#include <stdio.h>
#include<stdlib.h>
int main(){
char answers[]={'A','B','C','D','B','A','B','C','D','A'};//the correct answers
char guesses[10]; //array for storing the guesses
int i;
int numberOfCorrect=0;
int percentage=1;
for(i=0;i<10;i++){ //taking input for guesses
printf("Enter your guess for Question %d(A,B,C,D):",(i+1));
char ans;
scanf(" %c",&ans);
while(ans!='A' && ans!='B' && ans!='C' && ans!='D')
{ //if guess is something other than A,B,C,D
printf("ERROR!! Guess must be A,B,C,D ");
printf("Enter your guess for Question %d(A,B,C,D):",(i+1));
scanf(" %c",&ans);
}
guesses[i]=ans;
}
//printing the result of the quiz
printf("Here are the results of your quiz: ");
for(i=0;i<10;i++){
//printing correct answer and user's answer
printf("Question %d: Correct Answer: %c Your Answer: %c ",(i+1),answers[i],guesses[i]);
if(answers[i]==guesses[i])
{
printf(" Correct. ");
numberOfCorrect++;
}
else{
printf("Incorrect. ");
}
}
//printing correct answers
printf("You got %d answers right. ",numberOfCorrect);
//printing incorrect answers
printf("You got %d answers wrong. ",(10-numberOfCorrect));
percentage = numberOfCorrect*10;
printf("You got %d %",percentage);
}
Sample input output::
Enter your guess for Question 1(A,B,C,D):A
Enter your guess for Question 2(A,B,C,D):B
Enter your guess for Question 3(A,B,C,D):C
Enter your guess for Question 4(A,B,C,D):D
Enter your guess for Question 5(A,B,C,D):D
Enter your guess for Question 6(A,B,C,D):C
Enter your guess for Question 7(A,B,C,D):A
Enter your guess for Question 8(A,B,C,D):B
Enter your guess for Question 9(A,B,C,D):C
Enter your guess for Question 10(A,B,C,D):C
Here are the results of your quiz:
Question 1: Correct Answer: A Your Answer: A Correct.
Question 2: Correct Answer: B Your Answer: B Correct.
Question 3: Correct Answer: C Your Answer: C Correct.
Question 4: Correct Answer: D Your Answer: D Correct.
Question 5: Correct Answer: B Your Answer: D Incorrect.
Question 6: Correct Answer: A Your Answer: C Incorrect.
Question 7: Correct Answer: B Your Answer: A Incorrect.
Question 8: Correct Answer: C Your Answer: B Incorrect.
Question 9: Correct Answer: D Your Answer: C Incorrect.
Question 10: Correct Answer: A Your Answer: C Incorrect.
You got 4 answers right.
You got 6 answers wrong.
You got 40%