Monday, February 6, 2017

Programming Challenge 7.10 - Driver's License Exam

/* Driver's License Exam - The local Driver's License Office has asked
   for a program that grades the written portion of the driver's license
   exam. The exam has 20 multiple choice questions. Here are the correct
   answers:
 
      * 1. A         6. B         11. A         16. C
      * 2. D         7. A         12. C         17. C
      * 3. B         8. B         13. D         18. A
      * 4. B         9. C         14. B         19. D
      * 5. C        10. D         15. D         20. B

   The program stores the correct answers in an array. It asks the user
   to enter the student's answers for each of the 20 questions, and the
   answers are stored in another array.

   After the student's answers have been entered, the program displays
   a message indicating whether the student has passed or failed the
   exam. (A student must correctly answer 15 of the 20 questions to pass
   the exam.)

   It then displays the total number of correctly answered questions, the
   total number of incorrectly answered questions, and a list showing the
   question numbers of the incorrectly answered questions.

   Input validation: Only letters A, B, C, or D are accepted as answers. */

#include "Utility.h"

/* Prototypes: Display intro, Get student answers, Display questions,
               Evaluate answers */void displayIntro();
void getStudentAnswers();
void displayQuestions(string[]);
void evaluateAnswers(string[], char[]);

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

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: displayIntro

   This function displays an intro to the program.
   ********************************************************** */

void displayIntro()
{
   cout << "\n\t\tSIDNEY DRIVING SCHOOL - DRIVERS LICENSE TEST\n\n"
        << "Welcome to the driver's license writing test!\n"
        << "This portion of the test consists of 20 (in words twenty)\n"
        << "multiple-choice questions. You must answer 15 of these\n"
        << "correctly to receive your driver's license.\n\n"
        << "Please answer the questions with:\n\n"
        << " * 'A' or 'a'\n"
        << " * 'B' or 'b'\n"
        << " * 'C' or 'c'\n"
        << " * 'D' or 'd'\n\n"
        << "We wish you the best of luck!\n\n";

   /* Call: getStudentAnswers */
   getStudentAnswers();
}

/* **********************************************************
   Definition: getStudentAnswers

   This function asks the student a number of questions and
   stores his or her answers in an array.
   ********************************************************** */

void getStudentAnswers()
{
   /* Constant: Number of questions */
   const int NUM_QUESTIONS = 20;

   /* Array variables: Questions, Student answers */
   string questions[NUM_QUESTIONS] = { " " };
   char studentAnswers[NUM_QUESTIONS] = { ' ' };

   /* Variables: Question counter (loop counter) */
   int qCounter = 0;

   /* Call: displayQuestions */
   displayQuestions(questions);

   /* This loop gets the student's answers, and stores these in
      questions[] */
   while (qCounter < NUM_QUESTIONS)
   {
      cout << questions[qCounter];
      cout << "Your answer: ";
      cin >> studentAnswers[qCounter];

      /* If characters are entered in lower-case, they are converted
         to upper-case characters */
      if (islower(studentAnswers[qCounter]))
      {
         studentAnswers[qCounter] = toupper(studentAnswers[qCounter]);
      }

      /* Validate: Input */
      while (studentAnswers[qCounter] != 'a' && studentAnswers[qCounter] != 'A' &&
             studentAnswers[qCounter] != 'b' && studentAnswers[qCounter] != 'B' &&
             studentAnswers[qCounter] != 'c' && studentAnswers[qCounter] != 'C' &&
             studentAnswers[qCounter] != 'd' && studentAnswers[qCounter] != 'D')
      {
         cout << "\nYour choice is invalid. Valid choices are:\n\n"
              << "'A', 'a', 'B', 'b', 'C', 'c', 'D', d'\n"
              << "Your answer: ";
         cin >> studentAnswers[qCounter];
      }

      qCounter++;
      cout << endl;
   }

   /* Call: Evaluate result */
   evaluateAnswers(questions, studentAnswers);
}

/* **********************************************************
   Definition: evaluateAnswers

   This function takes the following array as its argument:

      * getStudentAnswers

   It evaluates the answers and displays the result.
   ********************************************************** */

void evaluateAnswers(string questions[], char studentAnswers[])
{
   /* Constant: Number of questions */
   const int NUM_QUESTIONS = 20;

   /* Array variable: Correct answers */
   char correctAnswers[NUM_QUESTIONS] = { 'A', 'D', 'B', 'B', 'C',
                                          'B', 'A', 'B', 'C', 'D',
                                          'A', 'C', 'D', 'B', 'D',
                                          'C', 'C', 'A', 'D', 'B' };

   /* Variables: Number of wrong answers, Number of correct answers,
                 Count (loop counter), Review wrong answers */
   int numWrong = 0,
       numCorrect = 0,
       count = 0;

   char review = ' ';

   /* The ternary operator in this loop accumulates the correclty and
      incorrectly answered questions in numWrong and numCorrect */
   for (count = 0; count < NUM_QUESTIONS; count++)
   {
      studentAnswers[count] != correctAnswers[count] ? numWrong += 1 :
                                                       numCorrect += 1;
   }

   /* Evaluate and display the result */
   numCorrect >= 15 ? cout << "CONGRATULATIONS! You Passed!\n\n" :
      cout << "Sorry ... You Failed ...\n";

   cout << "\nYou answered " << numCorrect << " questions correctly!\n";
   cout << "You answered " << numWrong << " questions incorrectly.\n\n";

   /* If the user wishes, he or she can review the incorrectly answered
      questions */
   cout << "Do you wish to review the wrong answers? ";
   cin >> review;

   if (review == 'Y' || review == 'y')
   {
      for (int count = 0; count < NUM_QUESTIONS; count++)
      {
         if (studentAnswers[count] != correctAnswers[count])
         {
            cout << "\n" << questions[count] << "\n"
               << "You answered " << studentAnswers[count] << "\n"
               << "The correct answer is: " << correctAnswers[count] << "\n\n";
         }
      }

      /* Determine and display the correct exit message */
      numCorrect > 15 ? cout << "Now exiting ... Congratulations! Good bye!" :
                        cout << "Now exiting ... Better luck next time!\n";
   }

   else if (review == 'N' || review == 'n')
   {
      numCorrect > 15 ? cout << "\nNow exiting ... Congratulations! Good bye!" :
                        cout << "\nNow exiting ... Better luck next time!\n";
   }

}

/* **********************************************************
   Definition: displayQuestions

   This function contains a set of 20 questions the student
   will have to answer.
   ********************************************************** */

void displayQuestions(string questions[])
{
 
   questions[0] =
   {
      "1. Under good weather conditions, when driving behind a vehicle\n"
      "   you should:\n\n"
      "A. Stay at least three seconds behind the vehicle in front of you\n"
      "B. Drive up as closely to the other vehicle as possible\n"
      "C. Stay one second behind the vehicle in front of you\n"
      "D. None of the above.\n\n"
   };

   questions[1] =
   {
      "2. As you drive into an intersection, the lights turn yellow\n"
      "   you should:\n\n"
      "A. Accelerate as hard as you can to pass before the light turns red\n"
      "B. Break immediately to stop\n"
      "C. Maintain your speed and pretend you haven't noticed\n"
      "D. None of the above.\n\n"
   };

   questions[2] =
   {
      "3. You are approaching an intersection and notice a pedestrian\n"
      "   crossing the street, you should:\n\n"
      "A. Sound your horn to warn the person\n"
      "B. Give way to the pedestrian\n"
      "C. Try to drive around the pedestrian to avoid hitting him or her\n"
      "D. None of the above.\n\n"
   };

   questions[3] =
   {
      "4. When reversing your car, you should:\n\n"
      "A. Take care and never reverse for a greater distance\n"
      "   and time than necessary\n"
      "B. Unbuckle your seat belt so you can reverse as quickly as possible\n"
      "C. Sound your horn to signal other drivers your intention\n"
      "D. None of the above\n\n"
   };

   questions[4] =
   {
      "5. You intend to leave your car. Do you have any responsibilities:\n\n"
      "A. No, there are no regulations for that\n"
      "B. No, the other drivers have to watch out and stop\n"
      "C. Yes, you must not open a door if it is likely that you are causing\n"
      "   danger to other road users\n"
      "D. None of the above\n\n"
   };

   questions[5] =
   {
      "6. You are involved in an accident, what details must you give to\n"
      "   other drivers if asked:\n\n"
      "A. No details at all, until you have contacted your insurance company\n"
      "B. You give them a name and an address, more details only\n"
      "   if an officer asks\n"
      "C. You must let them see your license, take details and give\n"
      "   your name and address.\n\n"
   };

   questions[6] =
   {
      "7. You hear the siren of an ambulance approaching you from behind, "
      "   you should:\n\n"
      "A. Move into the right lane\n"
      "B. Increase your speed and let the ambulance follow you\n"
      "C. They should drive slowly, it is a 30 mp/h zone\n"
      "D. None of the above\n\n"
   };

   questions[7] =
   {
      "8. You drive and you get sleepy, you should:\n\n"
      "A. Turn on the radio as loud as possible to keep you awake\n"
      "B. Stop and rest\n"
      "C. Turn on the air condition and open the car windows\n"
      "D. None of the above\n\n"
   };

   questions[8] =
   {
      "9. When you are driving on the freeway, which lane\n"
      "   should you choose:\n\n"
      "A. Whichever lane has the least traffic\n"
      "B. The fast driving lane to avoid slow-moving vehicles\n"
      "C. The right lane unless you are overtaking\n"
      "D. None of the above\n\n"
   };

   questions[9] =
   {
      "10. When you see the headlights flashing on a schoolbus,\n"
      "    what should you do:\n\n"
      "A. Stop and wait for the lights to stop flashing\n"
      "B. Drive past the bus only while the lights are flashing\n"
      "C. Be careful, there may be children about\n"
      "D. None of the above\n\n"
   };

   questions[10] =
   {
      "11. You must make a phone call while driving your car,\n"
      "    are you allowed to do that:\n\n"
      "A. No, it is strictly prohibited\n"
      "B. Yes, you are, but only at intersections\n"
      "C. Yes, but you must hold the steering wheel at least with one hand\n"
      "D. None of the above\n\n"
   };

   questions[11] =
   {
      "12. You are about to make a right-hand turn at an intersection.\n\n"
      "    You have the green light. You hear a siren and a fire truck is\n"
      "    approaching fast, what should you do:\n"
      "A. Continue and make the turn because you have the right\n"
      "B. Speed up to beat the fire truck\n"
      "C. Stop and let the fire truck pass by\n"
      "D. None of the above\n\n"
   };

   questions[12] =
   {
      "13. You want to drive over a bridge with just enough room for two\n\n"
      "    cars. As you approach you should:\n"
      "A. Maintain your current speed and hope for the best\n"
      "B. Sound your horn to warn the other drivers\n"
      "C. Drive in the middle of the street so no one even tries to cross it\n"
      "D. None of the above\n\n"
   };

   questions[13] =
   {
      "14. You borrow your friends car and find that the position of\n"
      "    the driver's seat puts you sitting a long ways away from the\n"
      "    steering wheel, what should you do:\n\n"
      "A. Put up with it. You should never adjust another person's seat\n"
      "B. Adjust the seat so it is right for you\n"
      "C. Ask your friend to put some pillows in your back\n"
      "D. None of the above\n\n"
   };

   questions[14] =
   {
      "15. Before driving a long distance at fast speed, you should:\n\n"
      "A. Have a large meal and several cups of coffee\n"
      "B. Make sure your navigation device is up-to-date\n"
      "C. That your car is clean\n"
      "D. None of the above\n\n"
   };

   questions[15] =
   {
      "16. Temporary traffic lights at road construction works:\n\n"
      "A. Apply to construction work vehicles only\n"
      "B. Are installed to warn drivers that work is in progress\n"
      "C. They must be obeyed\n"
      "D. None of the above\n\n"
   };

   questions[16] =
   {
      "17. If you are driving on the highway and road construction is\n"
      "    in progress, you must:\n\n"
      "A. Drive through the road construction zone as fast as possible\n"
      "B. Drive at the same speed all the other vehicles do\n"
      "C. Slow down to obey the speed limit\n"
      "D. None of the above\n\n"
   };

   questions[17] =
   {
      "18. When driving in poor light or weather conditions, and there\n"
      "    are pedestrians around, you should:\n\n"
      "A. Look carefuly and drive slowly because they are hard to see\n"
      "B. Keep your headlights on high beam all the time\n"
      "C. Always drive in the right hand lane so you are at a safe\n"
      "   distance from the pedestrians"
      "D. None of the above\n\n"
   };

   questions[18] =
   {
      "19. When entering or leaving a driveway, you must:\n\n"
      "A. Stop and give way only to other cars\n"
      "B. Sound your horn to make yourself being noticed\n"
      "C. Ignore others, they have to make way for you\n"
      "D. None of the above\n\n"
   };

   questions[19] =
   {
      "20. If you are driving and it starts to rain heavily, you should:\n\n"
      "A. Put your hazard warning lights on and increase your speed\n"
      "   to avoid the rain\n"
      "B. Slow down, using your breaks gently, since rain and oil may\n"
      "   create unsafe and dangerous driving conditions\n"
      "C. Put your lights on high beam so you can see better\n"
      "D. None of the above\n\n"
   };
}

Example Output:

 






No comments:

Post a Comment