我被分配做了一个得分为5分的程序,得分最低,然后取得最高4分的平均分。它不会让我使用变量'最低'。我有点困惑,可以使用一些帮助。
#include <iostream>
#include <iomanip>
using namespace std;
void getValues(float &score1, float &score2, float &score3, float &score4, float &score5);
float findLowest(float score1, float score2, float score3, float score4, float score5, float lowest);
void calcAverage(float score1, float score2, float score3, float score4, float score5, float &lowest);
void displayVales();
int main(void)
{
float score1, score2, score3, score4, score5, lowest;
getValues(score1, score2, score3, score4, score5);
findLowest(score1, score2, score3, score4, score5, lowest);
calcAverage(score1, score2, score3, score4, score5, lowest);
}
void getValues(float &score1, float &score2, float &score3, float &score4, float &score5)
{
cout << "Welcome to the test averaging program!" << endl;
cout << "This program will drop the lowest of five scores." << endl;
cout << "Please enter score #1 --> ";
cin >> score1;
cout << "Please enter score #2 --> ";
cin >> score2;
cout << "Please enter score #3 --> ";
cin >> score3;
cout << "Please enter score #4 --> ";
cin >> score4;
cout << "Please enter score #5 --> ";
cin >> score5;
}
float findLowest(float score1, float score2, float score3, float score4, float score5, float lowest)
{
lowest = score1;
{
if (score2 < lowest) lowest = score2;
if (score3 < lowest) lowest = score3;
if (score4 < lowest) lowest = score4;
if (score5 < lowest) lowest = score5;
cout << "The lowest test score is " << lowest << endl;
}
return lowest;
}
void calcAverage (float score1, float score2, float score3, float score4, float score5, float &lowest)
{ double average;
cout << setw(4);
cout.precision(2);
cout.setf(ios::fixed);
average = (( score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;
cout << "The average of the 4 highest test scores is: " << average << endl;
}
答案 0 :(得分:2)
您返回最低但不分配...要么将findLowest
中的参数更改为引用,要么删除参数并将其调用为:
lowest = findLowest(score1, score2, score3, score4, score5);
答案 1 :(得分:2)
在findLowest函数中,您需要通过引用传递(使用&符号)。
float findLowest(float score1, float score2, float score3, float score4, float score5, float &lowest)
{
if (score2 < lowest) lowest = score2;
if (score3 < lowest) lowest = score3;
if (score4 < lowest) lowest = score4;
if (score5 < lowest) lowest = score5;
cout << "The lowest test score is " << lowest << endl;
}
答案 2 :(得分:1)
请注意,您可以简单地将所有值相加,然后关闭最低值。有点像这样:
#include <iostream>
using namespace std;
int main() {
const int num_of_values = 5;
float values[num_of_values];
int lowest = 0;
float total = 0;
for (int i = 0; i < num_of_values; i++) {
printf("Enter value %i: ", i);
cin >> values[i];
total += values[i];
if (values[i] < values[lowest])
lowest = i;
}
total -= values[lowest];
printf("Result: %f \n", total/(num_of_values-1));
system("pause");
}
答案 3 :(得分:0)
你需要在它使用的块中声明最低(不是在main中,而是在findLowest中):
float findLowest(float score1, float score2, float score3, float score4, float score5, float lowest)
{
float lowest = score1;
if (score2 < lowest) lowest = score2;
if (score3 < lowest) lowest = score3;
if (score4 < lowest) lowest = score4;
if (score5 < lowest) lowest = score5;
cout << "The lowest test score is " << lowest << endl;
return lowest;
}
答案 4 :(得分:0)
我也做过这个节目。我使用Visual Studio 2017在我的程序上得了100分。
#include <iostream>
using namespace std;
void getScore(double&, double&, double&, double&, double&);
int findLowest(double&, double&, double&, double&, double&);
void calcAverage(double&, double&, double&, double&, double&, int&, int&);
int main()
{
double ts1, ts2, ts3, ts4, ts5; //declaring a variable for each requested test score
int ta; //declaring test average into an int because of the program requirements
getScore(ts1, ts2, ts3, ts4, ts5); //pulling the getScore function into the main which then defined
int low = findLowest(ts1, ts2, ts3, ts4, ts5); //pulling the lowest variable into the main for further use
calcAverage(ts1, ts2, ts3, ts4, ts5, ta, low); //calculating the average
cout << "The average of all the scores is " << ta << endl;
system("PAUSE");
return 0;
}
void getScore(double& ts1, double& ts2, double& ts3, double& ts4, double& ts5)
{
for (int c = 0; c < 5; c++)
{
double t = 0; // current test score
cout << "Input the test score for test " << c + 1 << endl;
cin >> t;
if (t < 0 || t > 100) //input validation
{ cout << "Input the test score for test " << c + 1<< " that is more than 0 and less than 100" << endl;
cin >> t; }
switch (c)//to input each test score into a variable for later use
{
case 0: if (c == 0)
{ ts1 = t;
break; }
case 1: if (c == 1)
{ ts2 = t;
break; }
case 2: if (c == 2)
{ ts3 = t;
break; }
case 3: if (c == 3)
{ ts4 = t;
break; }
case 4: if (c == 4)
{ ts5 = t;
break; }
}
}
}
int findLowest(double& ts1, double& ts2, double& ts3, double& ts4, double& ts5)
{
int lowest = 100;
if (lowest > ts1)
{
lowest = ts1;
}
if (lowest > ts2)
{
lowest = ts2;
}
if (lowest > ts3)
{
lowest = ts3;
}
if (lowest > ts4)
{
lowest = ts4;
}
if (lowest > ts5)
{
lowest = ts5;
}
return lowest; //the lowest will be returned to main
}
void calcAverage(double& ts1, double& ts2, double& ts3, double& ts4, double& ts5, int& ta, int& low)
{
ta = ts1 + ts2 + ts3 + ts4 + ts5;
ta = ta - low;
ta = ta / 4; //calculating the average
}