user14337188
user14337188

Reputation:

How to compare 1D arrays to 2D arrays (C++)

I'm writing a program that randomly generates 10 students' answers to a 5 question test (stored in a 2D array). The test answers are then compared to an answer key (1D array) and scored. I can't figure out how to compare the 1D to the 2D array and score the tests. Can anyone help out? (I coded as much as I could)

//Test grading program
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() 
{

//random answers to 10 text questions
srand(time(NULL));

const int KEY[5] = {1,1,3,2,4};
int studentAnswers[10][5];
int x = 0;
int scores[5];
int gradeAssignment (int KEY, int studentAnswers);
int correctAns = 0;

while ( x < 10)
{
  for (int i = 0; i < 10; i++)
  {
      for (int j = 0; j < 5; j++)
      {
        studentAnswers[i][j] = rand() % 5+1;
        cout<< studentAnswers[i][j]<< " ";
        x++;
      }
      cout << endl;
  }
}
//comparing scores
//PROBLEM AREA

Upvotes: 0

Views: 168

Answers (1)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

This should get you the scores of each student. To compare each studentanswer to the key we use the j index value to compare the values individually and then store it into the corresponding i index for the different student.

int scores[10]={0};

for (int i = 0; i < 10; i++)
{
      for (int j = 0; j < 5; j++)
      {
        studentAnswers[i][j] = rand() % 5+1;
        cout<< studentAnswers[i][j]<< " ";
        if(studentAnswers[i][j] == KEY[j])
        {
            scores[i]+=1;
        } 
      }
      cout <<endl;
}
for (int i = 0; i < 10; i++)
{
    cout<<scores[i]<<endl;
}

Upvotes: 1

Related Questions