如何从while循环生成的一组输出中找到最大值
How to find the maximum from a set of outputs generated by a while loop
我需要使用用户输入的参数找到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;
}
相关文章:
- 在 c++ 中拥有一组结构的正确方法是什么?
- 将值从二维数组输出到文本文件
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 一组值的零开销下标运算符
- 使用一组结构,避免在一组结构中出现重复的结构
- CMake:我们可以为一组不形成可执行文件或库的特定文件指定包含目录吗?
- 合并一组模板专用化
- 如何更好地检查两个 char 变量是否在一组值中?
- C++有没有办法强制重写一组方法,如果其中一个方法在子类中具有重写?
- 给定一个枢轴点,按照它们与枢轴点构成的角度递增顺序对一组点进行排序
- 如何从一组变量中查找最低值
- 如何根据对的第二个元素对 STL c++ 中的一组对进行排序?
- 如何通过查找迭代器结果分配给一组对的元素
- 读取一组用户输入,按升序排序,然后打印结果
- C :如何将数据作为CSV输出到现有文件中的一组特定单元格
- 如何从while循环生成的一组输出中找到最大值
- 如何打印一组要输出的字符串?C++
- C++ 输出一组数字的总和和乘积
- c++ for循环使用一个数组和多个数组输出不同的结果