0
我無法在二維數組中找到最低平均值,找到最高的AVG作品,但在查找最低值時我收到了垃圾數據。 這裏是我的功能: 我發現儘可能最低=最高,但我沒有修復解決方案。我知道問題可能在最低處初始化爲[0] [[0],所以你找不到一個小於[0] [0]的值。任何幫助?我無法找到二維數組中最低的AVG! (最高AVG工作)
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;
}`
你究竟在做什麼'gpa2 [0] [stu] = stu + 1;''''和'gpa2 [1] [stu] = avg;'?我假設'NUM_QTR'與'QTR_NUM'相同?如果是這樣,是不是季度'gpa2'中的第二維而不是學生ID'stu'? – Harry
for gpa2 [0] [stu] im將學生號存儲在2X12的單獨二維數組的第一行。並在gpa2 [1] [stu]中將所有學生的平均值存儲到二維數組的第二行。 否NUM_QTR有5個索引,而QTR_NUM有13個索引。 – SalehK
嘗試使用高值(例如10.0)初始化最低值,使用低值(例如-10.0)嘗試初始化最低值 – agbinfo