Respuesta :
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] + " ");
}
}
}
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 :)
