数组循环结构

Array Looping Structure

本文关键字:结构 循环 数组      更新时间:2023-10-16

我一直在尝试循环两个数组中匹配的一系列答案。代码每次都会运行整个循环,并输出前20个问题,但不会继续到下一组来检查这些答案。

answeredCorrect是计数器studentAnswers是一个由100个问题组成的数组,与数组correct匹配。答案大小为20。QUESTIONS是一个常数int,值为20,并与correctAnswers配对。

我试着放弃for循环结构,只使用while循环来检查studentAnwers中的位置,但这导致了崩溃。

有什么想法可以继续为其他每个学生计算,而不是坚持前20个答案吗?

这是我正在处理的代码部分。

编辑添加了函数createReport2()

编辑2添加了更新代码

string createReport2 (int questNum, char stuResponse, char correctResponse)
{
stringstream ss;  // the string stream object that will be used for the conversions
// Add the various elements to the stringstream object.
ss << questNum << "(" << stuResponse << "/" << correctResponse << ")";
// Return the final result as a C++ string class.
return ss.str();
} // end function string createReport (int questNum, char stuResponse, char correctResponse)
int main()
{
    const int QUESTIONS = 20; 
    const int STUDENT_QUESTIONS = 100;
    ifstream inputFile;
    inputFile.open("CorrectAnswers.txt");
    char correctAnswers[QUESTIONS];
    for (int i=0; i<20; i++)
    {
        inputFile >> correctAnswers[i]; 
    }
    ifstream inputFile2;
    inputFile2.open("StudentAnswers.txt");
    char studentAnswers[STUDENT_QUESTIONS];
    for (int t=0; t<STUDENT_QUESTIONS; t++)
    {
        inputFile2 >> studentAnswers[t];
    }
int answeredCorrectly = 0;
string name;
for(int c = 0; c < 5; c++)
    {
    string arr[100];
    for (int x=0; x < QUESTIONS; x++)
        {      
            if (studentAnswers[(5*c)+x] == correctAnswers[x])
                answeredCorrectly++;
            else
                arr[c*5+x] = createReport2(x,studentAnswers[c*5+x],correctAnswers[x]);
        }
                cout << "Report for Student " << c+1 << ":" << endl;
                cout << "---------------------" << endl;
                cout << "Missed " << 20 - answeredCorrectly << " out of 20 questions for " << (answeredCorrectly / 20.0) * 100 << "% correct." << endl;
                cout << "Answered Correctly:  " << answeredCorrectly << endl;
                cout << "Questions missed:";
                for (int i = 0; i < 20; i++)
                {
                    cout << arr[c*5+i];
                }
                cout << endl << endl;
                answeredCorrectly = 0;
    }
}

我认为一个快速的解决方案是将studentAnswers[(20*c)+x]放在任何有studentAnswers[x]的地方。

实际上,你应该把学生的答案放在二维数组中(studentAnswers[STUDENTS][QUESTIONS],其中STUDENTS应该是5)。

createReport2()中可能存在一个of-by-1,其中questNum可能更改为questNum+1

此外,在的main for循环中声明string arr[20]。OR将arr[x]更改为arr[c*20+x]以及将arr[i]更改为arr[c*20+i]

编辑:所以总结

for(int c = 0; c < 5; c++)
{
    for (int x=0; x < QUESTIONS; x++)
    {      
        if (studentAnswers[c*20+x] == correctAnswers[x])
            answeredCorrectly++;
        else
            arr[c*20+x] = createReport2(x,studentAnswers[c*20+x],correctAnswers[x]);
    }
    cout << "Report for Student " << c+1 << ":" << endl;
    cout << "---------------------" << endl;
    cout << "Missed " << 20 - answeredCorrectly 
         << " out of 20 questions for " << (answeredCorrectly / 20.0) * 100 
         << "% correct." << endl;
    cout << "Answered Correctly:  " << answeredCorrectly << endl;
    cout << "Questions missed:";
    for (int i = 0; i < 20; i++)
    {
        cout << arr[c*20+i];
    }
    cout << endl << endl;
    answeredCorrectly = 0;
}

我认为你需要一个单独的变量来处理不正确的答案,比如answeredIncorrectly。

for (int c = 0; c < 5; c++) {
    int answeredCorrectly = 0;
    int answeredIncorrectly = 0;
    for (int x = 0; x < QUESTIONS; x++) {
        if (studentAnswers[x] == correctAnswers[x])
            answeredCorrectly++;
        else {
            arr[answeredIncorrectly] = createReport2(x, studentAnswers[x],
                    correctAnswers[x]);
            answeredIncorrectly++;
        }
    }
    cout << "Report for Student " << c + 1 << ":" << endl;
    cout << "---------------------" << endl;
    cout << "Missed " << 20 - answeredCorrectly
            << " out of 20 questions for "
            << (answeredCorrectly / 20.0) * 100 << "% correct." << endl;
    cout << "Answered Correctly:  " << answeredCorrectly << endl;
    cout << "Questions missed:";
    for (int i = 0; i < answeredIncorrectly; i++) {
        cout << arr[i];
    }
    cout << endl << endl;
    answeredCorrectly = 0;
}