如何从while循环生成的一组输出中找到最大值



我需要使用用户输入的参数找到while循环生成的一组数字的最大值。我当前的代码只适用于第一个最大数字。例如,气球先上升,然后下降,再上升。我的程序只在第一次上升时找到最大高度,不考虑第二次。我如何才能找到这方面的真正最大值?

这是代码:

using namespace std;
int main()
{
    // output file var
    ofstream table;                                                             // output file named table
    table.open("spg5172-hw3-output.txt");                                       // output file created and opened
    // Var
    double startTime, endTime, incrementTime;                               // variables made for the start time, end time, and increment time. 
    //input
    cout << "CMPSC 201 - Homework3" << endl;                                                // class and assignment
    cout << "Create a table of altitude and velocity of a weather balloon" << endl;         // what the program will do
    cout << "Enter the start time (in hours): ";                                            // user asked to put in a start time
    cin >> startTime;                                                                       // user enteres start time
    if (startTime >= 48)                                                                    // if user enters a start time greater than or equal too 48 hours 
    {                                                                                       
        cout << "Please enter a start time less than 48 hours" << endl;                     // they will be told that a value under 48 is needed
        cout << "Enter the start time (in hours): ";                                        // intructed to put in a new start time
        cin >> startTime;                                                                   // user enters new start time
    }
    cout << "Enter the increment (in hours): ";                                             // user asked to enter increment time in
    cin >> incrementTime;                                                                   // user enters icnrement time
    if (incrementTime >= 48)
    {
        cout << "Please enter a increment time less than 48 hours" << endl;                 // if increment time is greater than or equal to 48 hours user is notified to insert another number less than 48 hours
        cout << "Enter the increment (in hours): ";
        cin >> incrementTime;
    }
    cout << "Enter the end time (in hours): ";                                              // user asked to enter a end time
    cin >> endTime;                                                                         // user enters end time
    if (endTime >= 48)
    {
        cout << "Please enter a end time less than 48 hours" << endl;                       // if user enters a time great than or equal to 48 hours, user is notified to insert another number less than 48 hours
        cout << "Enter the end time (in hours): ";
        cin >> incrementTime;
    }

    table.setf(ios::fixed);                             // shows 2 decimal placed to be shown
    table.setf(ios::showpoint);                         // shows 2 decimal placed to be shown
    table.precision(2);
    // table setup
    table << setw(12) << "Time" << setw(12) << "Height" << setw(12) << "Velocity" << endl;              // column for time height and velocity are created in output file
    table << setw(12) << "(hrs)" << setw(12) << "(meters)" << setw(12) << "(meters/s)" << endl;         // under each column is the appropriate unit  

    // output data points 
    for (double i = startTime; i <= endTime; i = i + incrementTime)                                     // a loop is created to find the cooridinates at ever given interveral. this loop will stop the the interval as research the end time
    {

        double altitude = (-.12 * pow(i, 4)) + (12 * pow(i, 3)) - (380 * pow(i, 2)) + (4100 * i) + 220;     // i is the time. i is pluged into this formula and a altitude is found
        double velocity = (-0.48 * pow(i, 3)) + (36 * pow(i, 2)) - (760 * i) + 4100;                        // the same i is then taken and plugged into this formulay to find its velocity at that specific time
        double secVelcoity = velocity / 3600;                                                               // the velocity is divided by 3600 to get the velocity in meters per sec
        table << setw(12) << i << setw(12) << altitude << setw(12) << secVelcoity << endl;                  // the time interval with its coorisponding altitude and velocity are writen in their columns and the loop is restarted untill i is equal to the end time
    }
    double k;                                                                                               // variable k is declared for the loop
    for (double j = startTime, k = startTime + incrementTime; j <= endTime; j = j + incrementTime, k = k + incrementTime)       // the max height need to found so the start altitudes are calculted again
    {
        double altitude = (-.12 * pow(j, 4)) + (12 * pow(j, 3)) - (380 * pow(j, 2)) + (4100 * j) + 220;                         // this is the formula for the altitude at j, the current time.
        double altitudeNext = (-.12 * pow(k, 4)) + (12 * pow(k, 3)) - (380 * pow(k, 2)) + (4100 * k) + 220;                     // this is the formula for the altitude at k, the time one increment higher then j

        if (j >= endTime)                                                                                                       // if j becomes greater then end time before the altitude at k becomes less then the altitude at j. the max altitude is at the end time
        {
            table << "Maximum balltoon height was " << altitude << " meters, and it occurs at " << j << " hours." << endl;      // this altotude is displayed along with the time it researched that altitude
        }
        if ( altitudeNext < altitude)                                                                                           // if the altitude  at j is greater then k we know that the balloon is on its way down again so at time j the altitude is the greatest
        {   
            table << "Maximum balltoon height was " << altitude << " meters, and it occurs at " << j << " hours." << endl;      // if this is true the altitude at j is displayed and time j is displayed and the loop ends
        }
    }

    table.close();          //output file closes

    return 0;
}

我真的建议制作子函数来帮助可读性:

double compute_altitude(double t)
{
    return (-.12 * pow(t, 4)) + (12 * pow(t, 3)) - (380 * pow(t, 2)) + (4100 * t) + 220;
}

要获得该函数的最大值(在离散点上):

double get_maximum_altitude(double startTime, double endTime, double incrementTime)
{
    double maxi = compute_altitude(startTime);
    for (double t = startTime + incrementTime; t <= endTime; t += incrementTime)
    {
        maxi = std::max(maxi, compute_altitude(t));
    }
    return maxi;
}

相关内容

最新更新