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

// ASSIGNMENT : overhaul the skeleton code below to read 10 questions from the f

ID: 3836939 • Letter: #

Question

//ASSIGNMENT: overhaul the skeleton code below to read 10 questions from the file given using C.

//The file name: questionbank.txt //THIS IS THE FILE THAT HAS 50 QUESTINS. 10 SHOULD BE CHOSEN AT RANDOM. IT IS POSTED BELOW THE SKELETON.

//WHAT THE PROGRAM SHOULD DO:

1. THE PROGRAM SHOULD ASK THE USER IF HE WANTS TO BE IN TRAINING MODE OR QUIZ MODE

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

3. 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.

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

HINT:

The function question_1 would be redefined as

int Question_1 ( int mode )

{

printf ( "What is the capital of USA ? 1. Atlanta 2. Washington DC 3. New York 4. Chicago " ) ;

int correctAnswer = 2 ;

int userResponse ;

if ( mode == TRAINING )

{
give him three chances.

}   

else {

give him one chance

}

rewrite the rest of the functions

}

SKELETON CODE: //////////////////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;

}

// file name: questionbank.txt:

//these are the questions. 10 should be chosen at random.

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.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 ( ) ;
void printQuestion(Question_T Q)

{
    printf("%s%s%s%s%s", Q.question, Q.answer1, Q.answer2, Q.answer3, Q.answer4);
}

int 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
    int quiz = 1;
    printf("Which mode you want to play. (1) quiz (0) training Enter your choice: ");
    scanf("%d", &quiz);
    for (i = 0; i < 10; i++) {
        printQuestion(questions[i]);
        printf("Enter your answer option: ");
        scanf("%d", &questions[i].userTypedAnswer);

        if (questions[i].userTypedAnswer == questions[i].correctAnswer || quiz == 1) continue; // if user answered correctly in 1st chance continue
        printf("Incorrect Answer "); // if mode is traiing give user other 2 chances
   printf("2nd trail, Enter your choice: ");
        scanf("%d", &questions[i].userTypedAnswer);

        if (questions[i].userTypedAnswer == questions[i].correctAnswer) continue; // if user answered correctly in 2nd chance continue
        printf("Incorrect Answer ");
        printf("3rd trail, Enter your choice: ");
        scanf("%d", &questions[i].userTypedAnswer);

        if (questions[i].userTypedAnswer == questions[i].correctAnswer) continue; // if user answered correctly in 3rd chance continue
        printf("Incorrect Answer ");
   printf("correct Answer is : %d ", questions[i].correctAnswer); // 3rd chance failed show correct answer to user
   
   }

   int score = 0;
   for (i = 0; i < 10; i++) {
       if (questions[i].userTypedAnswer == questions[i].correctAnswer) score++; // if answer is matching increase score
   }

   printf("Your score: %d out of %d ", score, 10);

}

/*
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;

    int linesToSkip = (randomNumber - 1) * 6;
    int count = 0;
    char discard[1000];
    while (count < linesToSkip) {
        fgets(discard, 1000, fp);
   count++;
    }
    fgets(test.question, 1000, fp);
    fgets(test.answer1, 128, fp);
    fgets(test.answer2, 128, fp);
    fgets(test.answer3, 128, fp);
    fgets(test.answer4, 128, fp);
    fscanf(fp, "%d", &test.correctAnswer);

   // 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

   fclose ( fp);

   return test;

}

sanjiv@sanjiv-VirtualBox:~$ gcc questions.c -o questions
sanjiv@sanjiv-VirtualBox:~$ ./questions
Which mode you want to play.
(1) quiz
(0) training
Enter your choice: 1
North Carolina is
1. Next to Texas
2. Next to California
3. Next to South Carolina
4. Utah
Enter your answer option: 1
New York City is in
1. New York
2. Maryland
3. Delaware
4. New Jersey
Enter your answer option: 1
Nigeria is in
1. America
2. Africa
3. Asia
4. Europe
Enter your answer option: 1
Louisiana is in
1. Canada
2. Texas
3. USA
4. Mexico
Enter your answer option: 1
Atlanta is in
1. Atlantic City
2. Atlantic Ocean
3. Georgia Country
4. Georgia State in USA
Enter your answer option: 1
Chicago is in
1. America
2. Australia
3. Europe
4. Asia
Enter your answer option: 1
North Carolina is
1. Next to Texas
2. Next to California
3. Next to South Carolina
4. Utah
Enter your answer option: 1
Perth is in
1. America
2. Europe
3. Asia
4. Australia
Enter your answer option: 1
China is in
1. America
2. Africa
3. Asia
4. Europe
Enter your answer option: 1
London is in
1. America
2. Australia
3. Europe
4. Asia
Enter your answer option: 1
Your score: 2 out of 10
sanjiv@sanjiv-VirtualBox:~$
sanjiv@sanjiv-VirtualBox:~$
sanjiv@sanjiv-VirtualBox:~$ ./questions
Which mode you want to play.
(1) quiz
(0) training
Enter your choice: 0
Illinois is in
1. South
2. North
3. East
4. West
Enter your answer option: 1
Incorrect Answer
2nd trail, Enter your choice: 2
Chicago is in
1. America
2. Australia
3. Europe
4. Asia
Enter your answer option: 1
White house is in
1. Canada
2. California
3. Next to my house
4. Washington DC
Enter your answer option: 4
White house is in
1. Canada
2. California
3. Next to my house
4. Washington DC
Enter your answer option: 4
United Kingdom is in
1. America
2. Africa
3. Europe
4. Asia
Enter your answer option: 3
New York City is in
1. New York
2. Maryland
3. Delaware
4. New Jersey
Enter your answer option: 1
Where is Canada
1. Below USA
2. Next to Australia
3. Below Mexico
4. Above USA
Enter your answer option: 4
Where is Canada
1. Below USA
2. Next to Australia
3. Below Mexico
4. Above USA
Enter your answer option: 4
Egypt is in
1. America
2. Africa
3. Europe
4. Asia
Enter your answer option: 2
The neighbors of USA are
1. Canada and Mexico
2. United Kingdom, Russia, Mexico and North Carolina
3. Brazil, Mexico, Canada
4. No Neighbors
Enter your answer option: 1
Your score: 10 out of 10
sanjiv@sanjiv-VirtualBox:~$