如何比较这两个数组
How do I compare these two arrays?
基本上,我有两个数组。一个具有测试的正确答案,另一个具有用户对测试的答案。如何比较两者?例如:
如何显示用户在测试中的得分为 15-20?
如何显示用户答错了多少个问题?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
const int NUM_ANSWERS = 20;
char answers[NUM_ANSWERS] = { 'A', 'D', 'B', 'B', 'C', 'B', 'A', 'B', 'C', 'D', 'A', 'C', 'D', 'B', 'D', 'C', 'C', 'A', 'D', 'B' };
char userAnswer[NUM_ANSWERS];
string textName;
int count = 0;
cout << "nEnter a text file name ";
cin >> textName;
ofstream textFileOut;
textFileOut.open(textName);
for (int i = 0; i < NUM_ANSWERS; i++)
{
cout << "nEnter the test answers ";
cin >> userAnswer[i];
textFileOut << userAnswer[i] << 'n';
}
textFileOut.close();
ifstream textFileIn;
textFileIn.open(textName);
while (count < NUM_ANSWERS && textFileIn >> userAnswer[count])
count++;
textFileIn.close();
cout << endl << endl;
system("pause");
return(0);
}
一个简单的循环应该为你解决这个问题。通过使用计数器,您可以获得正确答案的数量,如果所有答案的价值相同,则可以同时获得正确答案的数量和用户分数。
numCorrect = 0;
for (int i = 0; i < count; i++)
{
if (userAnswer[i] == answers[i])
numCorrect++;
}
相关文章:
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- 在c++中获取两个大int,并将它们存储在数组中
- 如何以优化的方式同时迭代两个间距不相等的数组
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 将字符串拆分为标记,并将标记拆分为两个单独的数组
- 可以将两个相同类型的连续数组视为一个数组吗?
- C++递归来决定数组中的两个值
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 按 Tab 单步执行两个控制组
- 如何检查两个元组的所有成员是否不同
- C 通过随机选择其元素从两个元组制成元组
- 如何将两个元组作为C 的参数发送
- 对于同一循环的这两个连续组,是否有更有效的解决方案
- 在 C++11 中加入两个元组
- 将两个双数组集组合为一个查找其并集和交集的最简单方法
- 如何创建一个在 C++11 (STL) 中压缩两个元组的函数
- 快速实现两个 8 位数组的协方差
- 如何绑定两个元组
- 将一个16位整数复制到一个两字节数组