我正在编写一个程序,该程序随机生成10个学生对5个问题的测试的答案(存储在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;
}
关于c++ - 如何将一维数组与二维数组进行比较(C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64056029/