2

私は、5 つのスコアを取り、最低のスコアを落とし、最高の 4 つのスコアの平均を取るプログラムを行うように割り当てられました。変数「lowest」を使用できません。私は少し混乱していて、助けが必要です。

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

} 
4

5 に答える 5

2

最低を返しますが、割り当てません...findLowestパラメーターを参照に変更するか、パラメーターを削除して次のように呼び出します。

lowest = findLowest(score1, score2, score3, score4, score5);
于 2012-11-11T20:55:24.027 に答える
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;
}
于 2012-11-11T21:01:13.803 に答える
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");
}
于 2012-11-11T21:34:59.800 に答える
0

使用するブロックで low を宣言する必要があります (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;
}
于 2012-11-11T21:01:49.197 に答える