我在 2D 阵列中找不到最低的 AVG!(最高平均作品)
I cant find the lowest AVG in a 2D array! (Highest AVG Works)
我在 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;
} `
相关文章:
- OpenMP阵列性能较差
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 当我的阵列太大时出现分段错误
- 位阵列上的快速AND运算
- 阵列必须使用大括号封闭的初始器进行初始化
- 没有从阵列<float>到阵列<int>的可行转换
- C++动态安全 2D 交错阵列
- 将平面阵列重塑为复杂的特征类型
- 如何使用英特尔 PIN 捕获阵列的所有负载?
- 为什么我能够为阵列分配比计算机实际拥有的内存更多的内存
- 库特<<恩德尔;不适用于打印 2D 阵列
- 数组类 阵列的打印输出
- 从较小的阵列到较大的阵列的元素级转换
- 仅在大型阵列上出现合并排序分段错误
- 从阵列C++中删除重复项
- 阵列的大小在C++中是否灵活?
- 反转阵列中的正序列
- 具有可变尺寸的 C++ 2D 阵列
- 二维阵列的动态分配
- 重新增长阵列时出错:写入时缓冲区溢出