多项选择测试如何将正确的计数变量发送到数组分数[8] ... 例如。该密钥用于扫描数组答案并得到7第一行,但现在我如何更新7到数组得分[0]。该程序正在运行,我只需要知道如何正确更新阵列得分,以便我能找到平均感谢
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int NUMBER_OF_STUDENTS = 8;
const int NUMBER_OF_QUESTIONS = 10;
int score[8];
string studentnames[8];
double avg;
// Students' answers to the questions
char answers[NUMBER_OF_STUDENTS][NUMBER_OF_QUESTIONS] =
{ { 'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D' },
{ 'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D' },
{ 'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D' },
{ 'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' } };
// Key to the questions
char keys[] = { 'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D' };
//Loop for student
for(int i = 0; i < 8; i++)
{
cout << "Enter Student #" << i + 1 << " Name: ";
cin >> studentnames[i];
}
for(int i = 0; i < NUMBER_OF_STUDENTS; i++)
{
// Grade one student
int correctCount = 0;
for(int j = 0; j < NUMBER_OF_QUESTIONS; j++)
{
if(answers[i][j] == keys[j])
correctCount++;
}
//output Student name and test Score
cout << "Student Name is : " << studentnames[i] << endl;
cout << "Score of the test is " << correctCount << endl;
}
return 0;
}
答案 0 :(得分:0)
您可以将score
声明为double
,或进行浮动计算并将得分乘以100
for(int i = 0; i < NUMBER_OF_STUDENTS; i++)
{
// Grade one student
int correctCount = 0;
for(int j = 0; j < NUMBER_OF_QUESTIONS; j++)
{
if(answers[i][j] == keys[j])
correctCount++;
}
score[i] = 100 * ((double)correctCount / (double)NUMBER_OF_QUESTIONS);
cout
<< "Name: " << studentnames[i]
<< ", correctCount: " << correctCount
<< ", average: " << score[i] << "\n";
}
与您的问题无关,但您也可以通过声明学生的姓名来使代码更容易调试,而不是每次都输入。您可以稍后注释掉用户输入部分。
//for(int i = 0; i < 8; i++)
//{
// cout << "Enter Student #" << i + 1 << " Name: ";
// cin >> studentnames[i];
//}
std::string studentnames[NUMBER_OF_STUDENTS] =
{ "n1","n2","n3","n4","n5","n6","n7","n8" };