10. Driver’s License Exam The local Driver’s License Office has asked you to write 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 2. D 3. B 4. B 5. C 6. B 7. A 8. B 9. C 10. D 11. A 12. C 13. D 14. B 15. D 16. C 17. C 18. A 19. D 20. B Your program should store the correct answers shown above in an array. It should ask the user to enter the student’s answers for each of the 20 questions, and the answers should be stored in another array. After the student’s answers have been entered, the program should display a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It should then display 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 accept the letters A, B, C, or D as answers.

Respuesta :

Debel

Answer & Explanation:

Written in java

import java.util.Scanner;

public class DriverslicenceExam {

   private char[] correct;

   private char[] student;

   private int[] missed;

   private int numCorrect;

   private int numIncorrect;

   DriverslicenceExam(char[] s) {

       correct = new char[20];

       student = new char[20];

       correct = s;

       numCorrect = 0;

       numIncorrect = 0;

   }

   private void gradeExam() {

       for (int i = 0; i < 20; i++) {

           if (student[i] == correct[i])

               this.numCorrect++;

           else {

               this.numIncorrect++;

           }

       }

   }

   private void makeMissedArray() {

       int j = 0;

       this.missed = new int[this.numIncorrect];

       for (int i = 0; i < 20; i++) {

           if (student[i] != correct[i]) {

               missed[j] = i + 1;

               j++;

           }

       }

   }

   boolean passed() {

       return numCorrect >= 15;

   }

   int totalCorrect() {

       return this.numCorrect;

   }

   public int totalIncorrect() {

       return this.numIncorrect;

   }

   public int[] questionsMissed() {

       return this.missed;

   }

   public static void main(String[] args) {

       Scanner in;

       char[] x = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};

       DriverslicenceExam de = new DriverslicenceExam(x);

       System.out.println("Enter your answers to the exam questions. (Make sure Caps Lock is ON!)");

       for (int i = 0; i < 20; i++) {

           System.out.print("Question " + (i + 1) + ": ");

           char answer;

           do {

               in = new Scanner(System.in);

               answer = in.next().charAt(0);

               if (!(answer == 'A' || answer == 'B' || answer == 'C' || answer == 'D'))

                   System.out.println("ERROR: Valid answers are A, B, C or D.");

               else

                   de.student[i] = answer;

           } while (!(answer == 'A' || answer == 'B' || answer == 'C' || answer == 'D'));

       }

       de.gradeExam();

       de.makeMissedArray();

       System.out.println("Correct answers: " + de.numCorrect);

       System.out.println("Incorrect answers: " + de.numIncorrect);

       if (de.passed())

           System.out.println("You passed the exam.");

       else

           System.out.println("You could not pass the exam. ");

       if (de.missed.length > 0) {

           System.out.println("You missed the following questions: ");

           for (int i = 0; i < de.missed.length; i++)

               System.out.print(de.missed[i] + " ");

       }

   }

}

nooray

Answer: C++ Program for Driver's License Exam

#include <iostream>

//input/output library  

#include <cctype>

//The <cctype> header file declares a set of functions

//to classify (and transform) individual characters.  

//For example, isupper() checks whether a character

//is uppercase or not.

using namespace std;

//using namespace std means that we are going to

//use classes or functions (if any) from "std" namespace

void checkAnswers(char[], char[], int, int);

//Function declartion to find out that given answer is right or wrong

int main() {

   

   const int NUMOFQUESTIONS = 20;

   const int MIN_CORRECT = 15;

   //Declare an initiate constant variables

   

   char answers[NUMOFQUESTIONS] = {

   'B', 'D', 'A', 'A', 'C',

   'A', 'B', 'A', 'C', 'D',

   'B', 'C', 'D', 'A', 'D',

   'C', 'C', 'B', 'D', 'A'

   };

   //Created a character array to store right answers

   

   char stu_answers[NUMOFQUESTIONS];

    //Created a character array to store student's answers

   

   //Loop for student's answers

   for (int replies = 0; replies < NUMOFQUESTIONS; replies++) {

       cout<< "Please enter your answers: "<< (replies + 1) << ": ";

       cin >> stu_answers[replies];

       

       //Validation of users answers

       while (stu_answers[replies] != 'A' && stu_answers[replies] != 'B' && stu_answers[replies] != 'C' && stu_answers[replies] != 'D')

     {

       cout << "You must enter A, B, C, or D\n";

       

       cout<< "Please enter your answers: "

       << (replies + 1) << ": ";

       cin >> stu_answers[replies];

     } //end of while loop

  } //end of for loop

   checkAnswers(answers, stu_answers, NUMOFQUESTIONS, MIN_CORRECT);

   //function call of checkAnswers taking four arguments

   

   return 0;

} //end of main function

   

void checkAnswers(char answers1[], char stu_answers1[], int NUMOFQUESTIONS, int MIN_CORRECT) {

   //Function definition for checkAnswers taking 4 parameters

   

   cout << "Total number of questions: " << NUMOFQUESTIONS;

   int correctAnswers = 0;

   

   //Check the student's replies against the correct answers

   for (int i = 0; i < NUMOFQUESTIONS; i++) {

   if (answers1[i] == stu_answers1[i]) //matching the correct answer with student's answer one by one

   correctAnswers++; //adding the score of correct answers given by student

   }

   //Did they pass or fail?

   cout << "\nYou must have at least 15 correct answers to pass.";

   if (correctAnswers >= MIN_CORRECT) {

    cout << "\nStudent passed the exam\n\n";

   }

   else {

    cout <<"\nStudent failed the exam\n\n";

   }

   

   //Display a list of the questions that were incorrectly answered.

   cout << "The list below shows the questions incorrectly";

   cout << " answered.\n";

   for (int i = 0; i < NUMOFQUESTIONS; i++) {

   if (answers1[i] != stu_answers1[i])  

   cout << "Question # " << i << " is incorrect." << endl;

   }

   

   //Display the number of correct and incorrect answers provided by the student.

   cout << "\nCorrect Answers = " << correctAnswers << endl;

   cout << "Incorrect Answers = " << NUMOFQUESTIONS - correctAnswers << endl;

   

} //end of checkAnswers function definition

Explanation:

A c++ program is written for given problem.  Comments are placed after every step.  To prepare this solution, C++ Functions and If-else conditions are used.

C++ Functions:

Function is a block of reusable code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions,

If-else conditions :

C++ has the conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false

// is used for single line comment

/* */ is used for multi line comment

[ ] is used to represent an array

Note: You can see output result in attached image.

Happy coding :)

Ver imagen nooray