Thread: I need help comparing arrays

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    I need help comparing arrays

    Why isn't my code running.
    Can someone please help me?

    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:
    
    
    Code:
    #include <iostream>using namespace std;
    
    
    void get_answers(char[], int);
    int calculate_grade(char[], char[],int, int);
    void output_grade(int, int);
    
    
    int main()
    {
        const int number_of_answers = 20;
        int answered_correctly = 0;
        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;
    
    
        get_answers(enter_answers, number_of_answers);
        calculate_grade(right_answers, enter_answers,number_of_answers, answered_correctly);
        output_grade(number_of_answers,answered_correctly);
    
    
        return 0;
    }
    
    
    void get_answers(char enter_answers[], int number_of_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;
        }
    }
    Last edited by Guy_from_SoCal; 11-09-2012 at 10:01 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you know the difference between pass by value and pass by reference?

    How is calculate_grade() going to pass an updated answered_correctly back to main?

    > for(int x=0; x <= number_of_answers; x++)
    Try < instead of <=
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing two arrays
    By Gonz in forum C Programming
    Replies: 2
    Last Post: 03-23-2011, 12:38 AM
  2. Comparing Arrays
    By cjohnman in forum C Programming
    Replies: 6
    Last Post: 04-24-2008, 03:24 PM
  3. Comparing Arrays
    By Raison in forum C++ Programming
    Replies: 10
    Last Post: 04-20-2004, 02:21 PM
  4. comparing arrays
    By dustyrain84 in forum C Programming
    Replies: 1
    Last Post: 01-21-2004, 05:52 PM
  5. Comparing Arrays
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-23-2001, 08:07 PM

Tags for this Thread