需要帮助计算数组值的AVERAGE(减去最低值)

时间:2015-01-12 02:18:58

标签: c++ arrays function average function-prototypes

所以我成功地混淆了自己这样做的地狱。我试图让它计算输入到数组中的权重的平均值减去数组中的最低权重。我正在使用函数和某个地方,我把自己与传递变量混淆了。如果有人能给我一个指针并告诉我我是否离开基地,我将不胜感激。另外,我如何比较输入到验证码的值?我有一句话评论说我正在摆弄,但从未开始工作。

   #include <iostream>

    using namespace std;

    int getWeight();
    int findLowest(int arrayWeight);
    double calcAverage(int weight);
    bool askToContinue();

    int main(){

        do{
            int weights = getWeight();
            double lowest = findLowest(weights);
            double average = calcAverage(weights);
        }
        while(askToContinue());
    }

    int getWeight() {

        //Variables
        int weights[5]; //array
        double enterWeight = 0;
        bool validAmount = false;


        //For loop to gather info and place amounts in an array
        cout << "Please enter the weights of the Tulbuks: " << endl;
        cout << endl;
        for (int counter = 0; counter < 5; counter++)
        {
            cin >> weights[counter];
            //validAmount = (weights[index] > 5) && (weights[index] <= 500);
        }

        //Test to redisplay the entered information
        cout << "Entered information: " << endl;
        for(int index = 0; index < 5; index++)
        {
            cout << "\nThe entered information for Tulbuk #" << (index+1) << " is: " << weights[index];
            cout << endl;
        }


        return -1;


        /*

        do
        {

            //Gather user input of amount of discs
            cout << "How many discs do you wish to purchase?" << endl;
            cout << "Please enter a number between 1 and 1,000,000" << endl;
            cin >> weights;
            cout << endl;
            validAmount = (weights > 5) && (weights <= 500);                              // Tests if the amount entered is valid
            if (!validAmount)                                                             // Prompts user amount entered was invalid
            {
                cout << "Invalid Amount. Please try again!" << endl;
            }
        }
        while(!validAmount);                                                              // Runs loop again if the amount entered was not valid



        return discs;

        */

    }

    int findLowest(int arrayWeight){

        int lowWeight = 999999;

        if(lowWeight > arrayWeight)
            {
                lowWeight = arrayWeight;
            }

        cout << arrayWeight;

        system("PAUSE");

        return arrayWeight;
    }

    double calcAverage(int weight){

        //Variables
        float avgWeight = 0;
        int sumWeight = 0;

        //Calls findLowest function to find lowest value
        int lowestWeight = findLowest(weight);

        //Calculates the average score


        return weight;
    }

    bool askToContinue()                                                                 // Asks the user if they want to continue. If yes, the loop restarts. If no, the program exits.
    {

        char userResponse = ' ';
        bool validInput = false;

        do
        {
            cout << endl;
            cout << "Do you wish to continue?" << endl;
            cout << "Enter y for 'yes' or n for 'no'" << endl;
            cin >> userResponse;
            validInput = (userResponse == 'y') || (userResponse == 'n');
            if (!validInput)
            {
                cout << "Invalid response. Please try again!" << endl;
            }
        } while (!validInput);
        return(userResponse == 'y');
    }

1 个答案:

答案 0 :(得分:0)

您遇到了许多问题,首先需要了解您正在使用的数据类型。您应该声明一次数组,然后传递指向该数组的指针。这些是更好的声明集,为方便起见,为权重数量设置了一个常量。

#include <iostream>

using namespace std;

const int numWeights = 5;

void getWeights(int weights[]);
int findLowest(int weights[]);
double calcAverage(int weights[]);
bool askToContinue();

int main() {
    do {
        int weights[numWeights];
        getWeights(weights);
        double average = calcAverage(weights);
        cout << "Average: " << average << endl;
    }
    while (askToContinue());
}

getWeights大部分没问题,但使用传入的数组。

void getWeights(int weights[]) {

    double enterWeight = 0;
    bool validAmount = false;


        //For loop to gather info and place amounts in an array
        cout << "Please enter the weights of the Tulbuks: " << endl;
        cout << endl;
        for (int counter = 0; counter < 5; counter++)
        {
            int weight;
            cin >> weight;
            while (weight < 5 || weight > 500) 
            {
                cout << "Invalid weight, should be between 5 and 500" << endl;
                cin >> weight;
            }
            weights[counter] = weight;
            //validAmount = (weights[index] > 5) && (weights[index] <= 500);
        }

        //Test to redisplay the entered information
        cout << "Entered information: " << endl;
        for(int index = 0; index < 5; index++)
        {
            cout << "\nThe entered information for Tulbuk #" << (index+1) << " is: " << weights[index];
            cout << endl;
        }
}

对于findLowest,您需要跟踪最低值和最低索引。通过记住最低值的索引,您将使平均值更容易。由于我们知道您的集合中始终存在最低值,因此不需要您的99999幻数。从索引0和第一个值开始。如果您发现较小的内容,请更新值和索引。当循环结束时,您将拥有最低值的第一个索引。请注意,循环从1开始(第二项)。

int findLowest(int weights[]) {
    int lowestVal = weights[0];
    int lowestIndex = 0;

    for (int i=1; i<numWeights; i++) {
        if (weights[i] < lowestVal) {
            lowestVal = weights[i];
            lowestIndex = i;
        }
    }
    return lowestIndex;
}

对于平均值找到最低指数,加上所有权重但跳过最低指数,转换为双倍,这样你就可以得到一个好的平均值并返回该值。

double calcAverage(int weights[]) {
    int lowestIndex = findLowest(weights);
    int total = 0;
    for (int i=0; i<numWeights; i++) {
        if (i != lowestIndex) {
            total += weights[i];
        }
    }
    return (double)total/(double)(numWeights-1);
}
相关问题