2012-11-11 122 views
2

我被分配到一個程序,需要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

你是什麼意思「它不會讓我」。你得到一個編譯錯誤? –

+0

它是一個調試錯誤,說明如何使用「最低」,但未聲明。 – user1810514

+0

我用g ++ 4.6.3編譯時沒有遇到任何錯誤。你使用什麼編譯器? –

回答

2

將返回最低的,但不分配它...要麼改變帕拉姆在findLowest做個參考或刪除參數,並調用它像這樣:

lowest = findLowest(score1, score2, score3, score4, score5); 
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; 
} 
+0

謝謝你解決了它 – user1810514

+0

不客氣。如果這解決了您的問題,請將答案標記爲正確。 – Boundless

0

您需要聲明塊中的最低它使用(不以主,但在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; 
} 
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"); 
} 
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 
}