Tuesday, February 7, 2017

Programming Challenge 7.11 - Exam Grader

Example Files: CorrectAnswers.txt
                         StudentAnswers.txt
                         StudentAnswersFail.txt

/* Exam Grader - One of your professors has asked to write a program to
   grade her final exams which consist of only 20 multiple-choice questions.
   Each question has one of four possible answers: A, B, C, or D. The file
   "CorrectAnswers.txt" contains the correct answers for all the questions,
   with each answer written on a separate line.
  
   The first line contains the answer to the first question, the second line
   contains the answer to the second question, and so forth.
  
   This program reads the contents of the "CorrectAnswers.txt" file into
   a char array, and then reads the contents of another file, containing
   the student's answers, into a second char array. The program determines
   the number of questions that the student missed and then displays the
   following:

      * A list of the questions missed by the student, showing the correct
        answer and the incorrect answer provided by the student for each
        missed question
      * The total number of questions missed
      * The percentage of questions answered correctly. This can be calculated
        as:

         * Correctly answered questions / Total number of questions

      * If the percentage of correctly answered questions is 70% or greater,
        the program indicates that the student passed the exam. Otherwise, it
        indicates that the student failed the exam. */

#include "Utility.h"

/* Prototype: Process files */
void processFiles();
void compareFiles(const char[], const char[]);

int main()
{
   /* Call: processFiles */
   processFiles();

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: processFiles

   This function contains three char arrays:

      * correctAnswers[]
      * studentAnswers[]

   The correct answers are read in from "CorrectAnswers.txt",
   and then compared to the student's answers, which are read
   in from "StudentAnswers.txt".
   ********************************************************** */

void processFiles()
{
   /* Constant: Array size */
   const int ARR_SIZE = 20;

   /* Array Variables: Correct answers, Student answers */
   char correctAnswers[ARR_SIZE] = { ' ' };
   char studentAnswers[ARR_SIZE] = { ' ' };

   /* Variables: Read in (loop counter) */
   int readIn = 0;

   /* File stream objects: Correct answers, Student answers */
   ifstream corrAnswers;
   ifstream studAnswers;

   /* Open file: "CorrectAnswers.txt", "StudentAnswers.txt" */
   corrAnswers.open("CorrectAnswers.txt");
   studAnswers.open("StudentAnswers.txt");

   /* Display a message in case the files could not be opened or
      processed, else the files are processed and their content is
      stored in the two arrays */
   if (!corrAnswers || !studAnswers)
   {
      cout << "File Processing Error: The file could not be opened, read\n"
           << "in or processed successfully. Please make sure that the file\n"
           << "exists, is placed in the correct folder, and is not damaged\n"
           << "or has otherwise been tampered with. Please close this window\n"
           << "now, make sure that everything is as it should be, then restart\n"
           << "the program.\n";
   }
   else
   {
      while (readIn < ARR_SIZE && corrAnswers >> correctAnswers[readIn] &&
                                  studAnswers >> studentAnswers[readIn])
      {
         readIn++;
      } 

      /* Call: compareFiles */
      compareFiles(correctAnswers, studentAnswers);
   }

   /* Close files: "CorrectAnswers.txt", "StudentAnswers.txt" */
   corrAnswers.close();
   studAnswers.close();
}

/* **********************************************************
   Definition: processFiles

   This function accepts the following arrays:

      * correctAnswers[]
      * studentAnswers[]

   The correct answers stored in correctAnswers[], and the
   student's answers stored in studentAnswers[], are compared
   and evaluated in this function. Once done, the percentage
   is calculated and the result is displayed.
   ********************************************************** */

void compareFiles(const char correctAnswers[], const char studentAnswers[])
{
   /* Constant: Array size, Passing percent */
      const int ARR_SIZE = 20,
                PASSING_PERCENT = 70;

   /* Variables: Calculate percent, Correct answers, Incorrect answers
                 (accumulators), Count (loop counter) */
   int calcPercent = 0,
       correct = 0,
       incorrect = 0,
       count = 0;

   /* Accumulate the correct and incorrect answers */
   for (count = 0; count < ARR_SIZE; count++)
   {
      studentAnswers[count] != correctAnswers[count] ? incorrect += 1 :
                                                       correct += 1;
   }

   /* Calculate the percent of correct answers */
   calcPercent = (correct * 100) / 20;

   /* Display: The missed answers, the number of correctly and
               incorrectly answered questions, the percentage
               of correctly answered questions, and whether
               the student has passed or failed the test */
   cout << "\t\tKANAZAWA JIN-JU JUNIOR HIGH - AUTOMATIC EXAM GRADER\n\n"
        << "\t\t\t\tSUBJECT - BIOLOGY\n\n"
        << "You answered these questions incorrectly:\n\n";

   for (int count = 0; count < ARR_SIZE; count++)
   {
      if (studentAnswers[count] != correctAnswers[count])
      {
         cout << "Question: " << (count + 1) << "\n\n"
            << "You answered:\t\t" << studentAnswers[count] << "\n"
            << "The correct answer is:  " << correctAnswers[count] << "\n\n";
      }
   }

   cout << "You answered " << incorrect << " questions incorrectly.\n"
        << "You answered " << correct << " questions correctly.\n";

   /* This ternary determines whether the percentage was high enough,
      and displays whether it is a pass or a fail */
   calcPercent >= PASSING_PERCENT ?
      cout << "\nYou had % " << calcPercent << " correct. PASS!\n" :
      cout << "\nYou had % " << calcPercent << " correct. FAIL!\n";
}

Example Output: 






No comments:

Post a Comment