# Comparing Arrays question

• 11-09-2012
Guy_from_SoCal
Comparing Arrays question
For any input i type in I keep getting the same thing. It keeps saying that all of the answers are incorrect. I'm also getting these lines of output every time.
You got 20 correct.
You missed -20.
You've passed!

Here's the description of the problem. My code is below it.

The local Driver's License Office has asked you to write aprogram that grades the written portion of driver's license exam.The exam has 20 multiple choice questions. Here are the correctanswers:
1. B 6.A 11.B 16. C
2. D 7.B 12.C 17. C
3. A 8.A 13.D 18. B
4. A 9.C 14.A 19. D
5. C 10.D 15.D 20. A
Your program should store the correct answers shown above inan array. It should ask the user to enter the student's answers foreach of the 20 questions, and the answers should be stored inanother array. After the student's answers have been entered, theprogram should display a message indicating whether the studentpassed or failed the exam. (A student must correctly answer 15 ofthe 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 questions numbers of the incorrectly answered questions.
Input Validations: Only accept the letters A, B, C, or D as answers.

Code:

``` //Write the program described on page 446 -- question 9.  The question is titled "Driver's License Exam". #include <iostream> using namespace std; //function prototypes void get_answers(char[], int); int calculate_grade(char[], char[],int, int); void output_grade(int, int); int main() {     //constent variable to store the number of answers/questions     const int number_of_answers = 20;     int answered_correctly = 0;     //variable to store right answers     char right_answers[number_of_answers] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D',                                             'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};     char enter_answers[number_of_answers];     cout << "Driver's License Exam Grader" << endl;     //function to get answers from the user     get_answers(enter_answers, number_of_answers);     //function to calculate grade     calculate_grade(right_answers, enter_answers,number_of_answers, answered_correctly);     //function to output if the user passed or failed     output_grade(number_of_answers,answered_correctly);     return 0; } void get_answers(char enter_answers[], int number_of_answers) {     //ask the user to enter the 20 answers     for (int counter=0; counter < number_of_answers; counter++)     {         cout << "Enter your answer for question " << (counter + 1) << endl;         cin >> enter_answers[counter];         while (enter_answers[counter] != 'A' && enter_answers[counter] != 'B' &&                 enter_answers[counter] != 'C' && enter_answers[counter] != 'D' )         {             cout << "ERROR: Only A,B,C, and D are valid.\n";             cout << "Question " << (counter + 1) << ": ";             cin >> enter_answers[counter];         }     } }     int calculate_grade(char right_answers[], char enter_answers[],int number_of_answers, int answered_correctly)     {         for(int x=0; x <= number_of_answers; x++)         {             if (right_answers[x] == enter_answers[x])             {                 answered_correctly ++;             }             return answered_correctly;         }     } void output_grade(char right_answers, char enter_answers, int answered_correctly, int number_of_answers) {     int missed = number_of_answers - answered_correctly;     for (int x=0; x< number_of_answers; x++)         {             if (right_answers[x]!= enter_answers[x])             {                 for (x = 0; x<number_of_answers; x++)                 {                     cout << "Answer " << (x+1)<< " is incorrect" << endl;                 }             }         }     }     cout << "Results (minimum 15 correct to pass): " << endl;     cout << "You got " << answered_correctly << " correct." << endl;     cout << "You missed " << missed << endl;     if (answered_correctly >=15)     {         cout<< "You've passed!" << endl;     }     else     {         cout << "You'ved failed." << endl;     } }```
• 11-09-2012
std10093
Code:

``` void calculate_grade(char right_answers[], char enter_answers[],int number_of_answers, int answered_correctly)     {         for(int x=0; x <= number_of_answers; x++)         {             if (right_answers[x] == enter_answers[x])             answered_correctly +=1;         }       ...```
Ok you compute the number of correct answers.What will happen when the program ends?You do not pass the variable by reference ,either return it, so the change of value will be lost when the function terminates.