我打电话给两个函数,分别找到最高和最低的成绩。它们返回“ highestGrade”和“ lowestGrade”,但是我很困惑为什么在编译时会出现错误。这是一个实验室作业,大多数代码是预先编写的,我的任务是填写缺少的代码。该错误发生在第55和63行附近,我要引用的功能在代码的末尾。
我对使用数组不熟悉,因此我假设在函数“ findHighest”和“ findLowest”中可能包含一些错误代码。例如,“ findHighest”中的程序将假定它遇到的第一个数组是最高等级,并将与其余数组进行比较,直到找到更高的数组为止。如果是,它将为该数组分配“ highestGrade”。
float findAverage(const GradeType, int);
int findHighest(const GradeType, int);
int findLowest(const GradeType, int);
int main()
{
GradeType grades;
int numberOfGrades;
int pos;
float avgOfGrades;
int highestGrade;
int lowestGrade;
// Read in the values into the array
pos = 0;
cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
cin >> grades[pos];
int i = 1;
while (grades[pos] != -99)
{
// read in more grades
pos = i;
cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
cin >> grades[pos];
}
numberOfGrades = pos; // Fill blank with appropriate identifier
// call to the function to find average
findAverage(grades, numberOfGrades);
avgOfGrades = findAverage(grades, numberOfGrades);
cout << endl << "The average of all the grades is " << avgOfGrades << endl;
// Fill in the call to the function that calculates highest grade
findHighest(grades, highestGrade);
highestGrade = findHighest(grades, highestGrade);
cout << endl << "The highest grade is " << highestGrade << endl;
// Fill in the call to the function that calculates lowest grade
findLowest(grades, lowestGrade);
// Fill in code to write the lowest to the screen
lowestGrade = findLowest(grades, lowestGrade);
cout << endl << "The lowest grade is " << lowestGrade << endl;
return 0;
}
float findAverage(const GradeType array, int size)
{
float sum = 0; // holds the sum of all the numbers
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size); //returns the average
}
int findHighest(const GradeType array, int size)
{
// Fill in the code for this function
float highestGrade = array[0];
for (int i = 0; i < size; i++)
{
if (array[i] > highestGrade)
highestGrade = array[i];
}
return highestGrade;
}
int findLowest(const GradeType array, int size)
{
// Fill in the code for this function
float lowestGrade = array[0];
for (int i = 1; i < size; i++)
{
if (array[i] < lowestGrade)
lowestGrade = array[i];
}
return lowestGrade;
}
由于该错误,程序无法输出最高和最低成绩。
答案 0 :(得分:1)
import * as Config from "../utils/Config";
jest.mock("../utils/Config", () => ({
getConfig: () => ({ ApiUrl: 'yourMockApiUrl' })
}));
在初始化之前,您正在使用findLowest(grades, lowestGrade);
。
lowestGrade
应该是
int lowestGrade;
当然,作为更好的C ++,请在需要它之前声明它,而不是在函数顶部。
其他变量也是如此。
如果逻辑正确,所有这些当然都可以了。为什么在函数中传递最低/最高等级作为大小参数?