我在 2D 阵列中找不到最低的 AVG!(最高平均作品)

I cant find the lowest AVG in a 2D array! (Highest AVG Works)

本文关键字:高平 阵列 2D 找不到 我在 AVG      更新时间:2023-10-16

我在 2D 数组中找到最低的 AVG 时遇到了问题,找到最高的 AVG 有效,但在找到最低的时我得到了垃圾数据。这是我的函数:在我找到最高后,我尝试使最低=最高,但这并没有解决解决方案。我知道问题可能出在最低的初始化为 [0][[0],因此找不到小于 [0][0] 的值。 有什么帮助吗?

void averaging(double gpa [][NUM_QTRS], double gpa2[][QTR_NUM])
 {
double highest, lowest;
double totalAvg = 0;
highest = gpa2[0][0];
lowest = gpa2[0][0];
for (int stu = 0; stu < NUM_STUDENTS; stu++)
{
    double total = 0;
    double avg = 0;
    double temp;

    // sum up all gpas for 1 student
    for(int i = 0; i < NUM_QTRS; i++)
    {
        total = total + gpa[stu][i];
    }
    // calculate average gpa for 1 student
    avg = total / NUM_QTRS;

    //fill second row with GPA of each student
    gpa2[0][stu] = stu + 1;
    gpa2[1][stu] = avg;
    if(gpa2 [1][stu] > highest)
        {
            highest = gpa2[1][stu];
        }
    if(gpa2 [1][stu] < lowest)
        {
            lowest = gpa2[1][stu];
        }

    /*for (int z = 0; z < NUM_STUDENTS; z++)
    {
    }*/

    totalAvg = totalAvg + avg;
    //temp = (static_cast<int>( (avg + 0.005) * 100 ) ) / 100.0;

    cout << fixed << showpoint << setprecision(2);
    cout << setw(3) << stu+1 << setw(13) << avg << endl;
}

totalAvg = totalAvg / NUM_STUDENTS;
cout << "Avg of all students: " << totalAvg << endl;
cout << "Highest GPA: " << highest << endl;
cout << "Lowest GPA: " << lowest << endl;

} '

假设您的第一个学生的 AVG 最低然后从第二个开始对所有学生运行循环

我修复了你的代码,看看:

void averaging(double gpa [][NUM_QTRS], double gpa2[][QTR_NUM])
 {
double highest, lowest;
double totalAvg = 0;
highest = gpa2[0][0];
for(int i = 0; i < NUM_QTRS; i++)
    {
        total = total + gpa[0][i];
    }
    // calculate average gpa for 1 student
    avg = total / NUM_QTRS;

    //fill second row with GPA of each student
    gpa2[0][0] = 1;
    gpa2[1][0] = avg;
lowest = avg;
for (int stu = 1; stu < NUM_STUDENTS; stu++)
{
    double total = 0;
    double avg = 0;
    double temp;

    // sum up all gpas for 1 student
    for(int i = 0; i < NUM_QTRS; i++)
    {
        total = total + gpa[stu][i];
    }
    // calculate average gpa for 1 student
    avg = total / NUM_QTRS;

    //fill second row with GPA of each student
    gpa2[0][stu] = stu + 1;
    gpa2[1][stu] = avg;
    if(gpa2 [1][stu] > highest)
        {
            highest = gpa2[1][stu];
        }
    if(gpa2 [1][stu] < lowest)
        {
            lowest = gpa2[1][stu];
        }

    /*for (int z = 0; z < NUM_STUDENTS; z++)
    {
    }*/

    totalAvg = totalAvg + avg;
    //temp = (static_cast<int>( (avg + 0.005) * 100 ) ) / 100.0;

    cout << fixed << showpoint << setprecision(2);
    cout << setw(3) << stu+1 << setw(13) << avg << endl;
}

totalAvg = totalAvg / NUM_STUDENTS;
cout << "Avg of all students: " << totalAvg << endl;
cout << "Highest GPA: " << highest << endl;
cout << "Lowest GPA: " << lowest << endl;
} `