如何比较1D阵列和2D阵列(C++)

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

本文关键字:阵列 2D C++ 何比较 1D 比较      更新时间:2024-04-28

我正在编写一个程序,该程序随机生成10名学生对5道题测试的答案(存储在2D数组中(。然后将测试答案与答案关键字(1D数组(进行比较并打分。我不知道如何将1D阵列与2D阵列进行比较并为测试打分。有人能帮忙吗?(我尽可能多地编码(

//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

这应该会得到每个学生的分数。为了将每个学生的答案与密钥进行比较,我们使用j索引值来单独比较这些值,然后将其存储到不同学生的相应i索引中。

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;
}