我无法在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;
}`
答案 0 :(得分:0)
尝试假设您的第一个学生的AVERAGE最低 然后在第二个学生的所有学生上运行你的循环
我修复了你的代码,看看:
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;
} `