c++ - 如何将一维数组与二维数组进行比较(C++)

标签 c++ arrays vector

我正在编写一个程序,该程序随机生成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/

相关文章:

c++ - toUniquePtr 实现

javascript - 如何在angularjs中将数组插入另一个数组

html - 将矢量图标字体添加到网站标题名称?

arrays - 使用给定集合中的值计算获得 N 的所有可能性

java - 将一个数组调用到另一个数组中...?

c++ - 在另一个 vector 的末尾添加一个 vector

c++ - 为什么 vector 上的这段代码会出现运行时错误?

c++ - 在 Visual Studio 中使用 C++/C# 连接 MySQL

c++ - 如何在类中使用 pthread_mutex 及其函数?

C++ 模板 : can I call a function whose name is based on the input type name?