下降最低分数计划

Dropping lowest score program

本文关键字:计划 最低分      更新时间:2023-10-16

i被分配为一个程序,该程序获得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;
} 

您返回最低,但不要分配...要么更改 findLowest中的参数为参考,要么删除参数,然后以此为单位:

lowest = findLowest(score1, score2, score3, score4, score5);

在您的findlowest函数中,您需要通过参考(使用ampersand)传递。

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;
}

请注意,您可以简单地添加所有值,然后取下最低值。有点这样:

#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");
}

您需要在块中声明最低(不是主要的,而是在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;
}

我也做了这个程序。我使用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
}