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

(Write a program, and show the output too ) C programming You will be writing a

ID: 3832032 • Letter: #

Question

(Write a program, and show the output too) C programming

You will be writing a simple Bee application. The application will display question 1, wait for the user to type a response, it checks if it is correct or not. if correct, it will increment the score by one. otherwise, it will go ahead with the next question. You would read any 10 questions from below. Then, store them in an array of structures: store one question in each cell . Then, store them in an array of structures: store one question in each cell

You have both Training Mode and Quiz mode.

//You would overhaul the version 2 to read 10 questions from the file.
//



In above there are 50 questions.
But you would select only 10 random questions

There are two modes: Training and Quiz mode
The requirements are same as in version 2 for these modes


//////////////////SKELETON IS GIVEN TO YOU ////////////////

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
  

typedef struct _question {
char question [ 1024 ] ;
char answer1 [128 ] ;
char answer2 [128 ] ;
char answer3 [128 ] ;
char answer4 [128 ] ;
int correctAnswer ;
int userTypedAnswer ;
//struct _question *next ;
} Question_T ;

Question_T getARandomQuestion ( ) ;

main ( )

{

Question_T questions [ 10 ] ;
srand ( time ( NULL ) );
int i = 0;

while ( i < 10 ) {
// read the question from file into the array
questions [ i ] = getARandomQuestion ( ) ;
i++ ;
}

// NOW YOU HAVE STORED 10 RANDOM QUESTIONS into the array of
// structures.

// ADMINISTER THE QUIZ AS TRAINING MODE OR QUIZ MODE

}

/*
THIS FUNCTION SHOULD OPEN THE FILE, READ A RANDOM QUESTION
FROM THE FILE. THE QUESTION AND THE CHOICES ARE READ
INTO THE STRUCTURE Question_T and is returned

EACH QUESTION IS STORED IN 6 LINES IN THE FILE.

*/

Question_T getARandomQuestion ( )

{
Question_T test ;

FILE *fp ;

fp = fopen ( "questionbank.txt", "r");

if ( fp == NULL ) {
perror ("Something is terribly wrong ");
exit ( 1 );
}

int randomNumber = rand ( ) % 50 + 1;

// FILE IS OPENED. NOW READ A QUESTION FROM THE FILE
// HOW ?
// say randomNumber = 11
// you skip 10 questions, // ie 10 times 6 lines = 60 lines
// HOW DO I SKIP. READ THE 60 LINES USING fgets , but don't do anything

// then read the next 6 lines into as the 11st question
// into the structure test and return it

fclose ( fp);

return test;

}

1. Extend the code

2. Ask the user, if he wants to practice or Take the real quiz. There are two modes of operation: Practice mode and QUIZ MODE.

3A. In the practice mode, the user is given three chances to provide the correct answer. Score is incremented only he answers correctly in those three chances. Otherwise, show him the correct answer and proceed.

3B. In the Quiz mode, there is no additional chances given. Score is incremented only if he answers correctly the first time.

4. The order of questions is random. use any method. The order of questions should be different each time you run.

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define MAX_LINE_SIZE 1024

typedef struct _question {
   char question[1024];
   char answer1[128];
   char answer2[128];
   char answer3[128];
   char answer4[128];
   int correctAnswer;
   int userTypedAnswer;
   //struct _question *next ;
} Question_T;

typedef enum _mode {
   PRACTICE = 1,
   QUIZ
} Mode_T;

Question_T getARandomQuestion();
int ValidateMode(Mode_T mode);
void DisplayQuestion(const Question_T *pQuestion);

int main()
{
   int j;
   int iScore;
   Mode_T mode;
   int iAnswer;
   int iNoOfAttempt;
   int iCorrectAnswer;
   Question_T questions[10];
   srand(time(NULL));
   int i = 0;
   while (i < 10) {
       // read the question from file into the array
       questions[i] = getARandomQuestion();
       i++;
   }

   // NOW YOU HAVE STORED 10 RANDOM QUESTIONS into the array of
   // structures.
   // ADMINISTER THE QUIZ AS TRAINING MODE OR QUIZ MODE
   printf(" 1.PRACTICE mode:-");
   printf(" 2.QUIZ mode:-");
   printf(" Enter mode:-");
   scanf("%d", &mode);

   if (0 == ValidateMode(mode))
   {
       printf("Invalid mode");
       return 0;
   }

   iScore = 0;
   iNoOfAttempt = 1;
   if (mode == PRACTICE)
       iNoOfAttempt = 3;

   for (i = 0; i < 10; i++)
   {
       iAnswer = 0;
       iCorrectAnswer = 0;
       DisplayQuestion(&questions[i]);
       for (j = 0; j < iNoOfAttempt; j++)
       {
           printf(" Enter your answer:-");
           scanf("%d", &iAnswer);
           if (iAnswer == questions[i].correctAnswer)
           {
               iScore++;
               iCorrectAnswer = 1;
               break;
           }
       }

       if (0 == iCorrectAnswer && mode == PRACTICE)
       {
           printf(" Correct anwer is: %d ", questions[i].correctAnswer);
       }
   }

   printf("Your score is %d", iScore);
}

void DisplayQuestion(const Question_T *pQuestion)
{
   printf("%s", pQuestion->question);
   printf("%s", pQuestion->answer1);
   printf("%s", pQuestion->answer2);
   printf("%s", pQuestion->answer3);
   printf("%s", pQuestion->answer4);
}

/*
THIS FUNCTION SHOULD OPEN THE FILE, READ A RANDOM QUESTION
FROM THE FILE. THE QUESTION AND THE CHOICES ARE READ
INTO THE STRUCTURE Question_T and is returned

EACH QUESTION IS STORED IN 6 LINES IN THE FILE.

*/
Question_T getARandomQuestion()
{
   FILE *fp;
   int iLine;
   Question_T test;
   int iSkippLineCount;
   char szLine[MAX_LINE_SIZE];

   fp = fopen("questionbank.txt", "r");
   if (fp == NULL) {
       perror("Something is terribly wrong ");
       exit(1);
   }

   int randomNumber = rand() % 50 + 1;
   // FILE IS OPENED. NOW READ A QUESTION FROM THE FILE
   // HOW ?
   // say randomNumber = 11
   // you skip 10 questions, // ie 10 times 6 lines = 60 lines
   // HOW DO I SKIP. READ THE 60 LINES USING fgets , but don't do anything
   iSkippLineCount = randomNumber * 6;
   for (iLine = 0; iLine < iSkippLineCount; iLine++)
   {
       fgets(szLine, MAX_LINE_SIZE, fp);
   }

   // then read the next 6 lines into as the 11st question
   // into the structure test and return it
   fgets(test.question, sizeof(test.question), fp);
   fgets(test.answer1, sizeof(test.answer1), fp);
   fgets(test.answer2, sizeof(test.answer2), fp);
   fgets(test.answer3, sizeof(test.answer3), fp);
   fgets(test.answer4, sizeof(test.answer4), fp);
   fgets(szLine, sizeof(szLine), fp);
   test.correctAnswer = atoi(szLine);

   fclose(fp);

   return test;
}

int ValidateMode(Mode_T mode)
{
   if (mode == PRACTICE || mode == QUIZ)
       return 1;
   else
       return 0;
}

Output:-


1.PRACTICE mode:-
2.QUIZ mode:-
Enter mode:-2
Where is California
1. Next to Nevada
2. Next to Texas
3. Next to Washington DC
4. Next to Washington State

Enter your answer:-1
Atlanta is in
1. Atlantic City
2. Atlantic Ocean
3. Georgia Country
4. Georgia State in USA

Enter your answer:-2
South Korea is in
1. America
2. Australia
3. Europe
4. Asia

Enter your answer:-3
Mississippi divides
1. America
2. Canada
3. Mexico
4. Russia

Enter your answer:-1
Blue Mountain Ridge is in
1. East of Mississippi
2. West of Mississippi
3. in Canada
4. Mexico

Enter your answer:-2
Iraq is in
1. America
2. Africa
3. Europe
4. Middle East

Enter your answer:-1
Los Angeles is in
1. Texas
2. Utah
3. California
4. Nevada

Enter your answer:-4
Where is California
1. Next to Nevada
2. Next to Texas
3. Next to Washington DC
4. Next to Washington State

Enter your answer:-4
Los Angeles is in
1. Texas
2. Utah
3. California
4. Nevada

Enter your answer:-6
UK stands for
1. Unknown
2. United Kentucky
3. United Kings
4. United Kingdom

Enter your answer:-1
Your score is 2

I have extended the code and shown the output in Quiz mode. It is also working in practice mode.