mercredi 31 octobre 2018

Take differences in two arrays and record their number in a third array

So I have been having an extremely hard time trying to figure out this issue. All of my code currently works except for my last module where I am trying to compare the two arrays for differences and record the question number into a new array. I'm not really sure how to set it up. Any help will be appreciated.

The main issue is with the array incorrect I am creating. I do not know how to set up a blank array that gets bigger as values are entered. Unless I am just really messing that concept up. I have the issues currently commented out while I was trying different things.

#include <iostream>
using namespace std;

const int COLS = 20;
void input_data(char [], int);
void compare_data(char [], int, char [], int);

int main()
{
    char letters[COLS];
    char answers[] = { 'A', 'D', 'B', 'B',
                       'C', 'B', 'A', 'B',
                       'C', 'D', 'A', 'C',
                       'D', 'B', 'D', 'C',
                       'C', 'A', 'D', 'B'};

    input_data(letters, COLS);
    compare_data(letters, COLS, answers, COLS);
}

void input_data(char letter[], int size)
{
    cout << "Please enter the student's answers for each of the questions. \n";
    cout << "Press Enter after typing each answer. \n";
    cout << "Please enter only an A, B, C, or D for each question. \n";
    for (int i = 1; i <= size; i++)
    {
        cout << "Question " << i << ": ";
        cin >> letter[i - 1];
        while (letter[i - 1] != 'A' && 
                letter[i - 1] != 'B' &&
                letter[i - 1] != 'C' &&
                letter[i - 1] != 'D')
        {
            cout << "Please enter only A, B, C, or D \n";
            cout << "Question " << i << ":";
            cin >> letter[i - 1];
        }
    }
}

void compare_data(char letter[], int size, char answer[], int cols)
{
    int ans_correct = 0;
    int ans_wrong = 0;
    //int incorrect[20];
    for (int i = 1; i <= size; i++)
    {
        if (letter[i] == answer[i])
            ans_correct += 1;
        else
        {
            ans_wrong += 1;
            //incorrect[i-1] = i;
        }
    }

    if (ans_correct >= 15)
        cout << "The student passed the exam. \n";
    else
        cout << "The student did not pass the exam. \n";

    cout << "Correct Answers: " << ans_correct << endl;
    cout << "Incorrect Answers: " << ans_wrong << endl << endl;

    cout << "Questions that were answered incorrectly: \n";
    for (int i = 1; i < size; i++)
    {
        //cout << incorrect[i-1] << endl;
    }
}

Aucun commentaire:

Enregistrer un commentaire