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\\";
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 << "\\You must have at least 15 correct answers to pass.";
if (correctAnswers >= MIN_CORRECT) {
cout << "\\Student passed the exam\\\\";
}
else {
cout <<"\\Student failed the exam\\\\";
}
//Display a list of the questions that were incorrectly answered.
cout << "The list below shows the questions incorrectly";
cout << " answered.\\";
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 << "\\Correct Answers = " << correctAnswers << endl;
cout << "Incorrect Answers = " << NUMOFQUESTIONS - correctAnswers << endl;
} //end of checkAnswers function definition
Step-by-step 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 :)