Data Collection
Section 4.4 break and continue Statements 157
4.6 Problem Solving Applied: Weather Balloons
Weather balloons are used to gather temperature and pressure data at various altitudes in the atmosphere. The balloon rises because the density of the helium in the balloon is less than the density of the surrounding air outside the balloon. As the balloon rises, the surrounding air becomes less dense, and thus the balloon’s ascent slows until it reaches a point of equi-librium. During the day, sunlight warms the helium trapped inside the balloon, which causes the helium to expand and become less dense and the balloon to rise higher. During the night, however, the helium in the balloon cools and becomes more dense, causing the balloon to descend to a lower altitude. The next day, the sun heats the helium again and the balloon rises.
Over time, this process generates a set of altitude measurements that can be approximated with a polynomial equation.
Assume that the polynomial alt(t) = −0.12t4+ 12t3− 380t2+ 4100t + 220, where the units of t are hours, represents the altitude or height in meters during the first 48 hours follow-ing the launch of a weather balloon. The correspondfollow-ing polynomial model for the velocity in meters per hour of the weather balloon isv(t) = −0.48t3+ 36t2− 760t + 4100.
Print a table of the altitude and the velocity for this weather balloon using units of meters and meters per second. Let the user enter the start time, increment in time between lines of the table, and ending time, where all the time values must be less than 48 hours. In addition to printing the table, also print the peak altitude and its corresponding time.
1. PROBLEM STATEMENT
Using the polynomials that represent the altitude and velocity for a weather balloon, print a table using units of meters and meters per second. Also find the maximum altitude (or height) and its corresponding time.
2. INPUT/OUTPUT DESCRIPTION
The following I/O diagram shows the user input that represents the starting time, time increment, and ending time for the table. The output is the table of altitude and velocity values and the maximum altitude and its corresponding time. We can use the built-in data type double for our input and output objects.
164 Chapter 4 Control Structures: Repetition
Starting time Time increment
Table of velocities and acceleration Maximum height and its time Ending time
3. HAND EXAMPLE
Assume that the starting time is 0 hours, the time increment is 1 hour, and the ending time is 5 hours. To obtain the correct units, we need to divide the velocity value in meters per hour by 3600 in order to get meters per second. Using our calculator, we can then compute the following values:
Time Altitude (m) Velocity (m/s)
0 220.00 1.14
1 3,951.88 0.94
2 6,994.08 0.76
3 9,414.28 0.59
4 11,277.28 0.45
5 12,645.00 0.32
We can also determine the maximum altitude for this table, which is 12,645.00 meters; it occurred at 5 hours.
4. ALGORITHM DEVELOPMENT
We first develop the decomposition outline because it breaks the solution into a series of sequential steps.
Decomposition Outline
1. Get user input to specify times for the table.
2. Generate and print conversion table and find maximum height and corresponding time.
3. Print maximum height and corresponding time.
The second step in the decomposition outline represents a loop in which we generate the table and, at the same time, keep track of the maximum height. As we refine this outline, and particularly step 2 into more detail, we need to think carefully about finding the maxi-mum height. Look back at the hand example. Once the table has been printed, it is easy to look at it and select the maximum height. However, when the computer is computing and printing the table, it does not have all the data at one time; it only has the information for
Section 4.6 Problem Solving Applied: Weather Balloons 165
the current line in the table. Therefore, to keep track of the maximum, we need to specify a separate object to store the maximum value. Each time that we compute a new height, we will compare that value to the maximum value. If the new value is larger, we replace the maximum with this new value. We will also need to keep track of the corresponding time.
The following refinement in pseudocode outlines these new steps:
Refinement in Pseudocode
main: read initial, increment, final values from keyboard set max_height to zero
set max_time to zero if height > max_height
set max_height to height set max_time to time add increment to time
print max_time and max_height
The steps in the pseudocode are now detailed enough to convert into C++. Note that we convert the velocity from meters per hour to meters per second in the cout statement.
/*---*/
/* Program chapter4_8 */
/* */
/* This program prints a table of height and */
/* velocity values for a weather balloon. */
#include <iostream>//Required for cin, cout
#include <iomanip>//Required for setw()
#include <cmath>//Required for pow() using namespace std;
int main() {
// Declare and initialize objects.
double initial, increment, final, time, height, velocity, max_time(0), max_height(0);
int loops;
// Get user input.
cout << "Enter initial value for table (in hours) \n";
cin >> initial;
cout << "Enter increment between lines (in hours) \n";
cin >> increment;
cout << "Enter final value for table (in hours) \n";
cin >> final;
166 Chapter 4 Control Structures: Repetition
// Print report heading.
cout << "\n\nWeather Balloon Information \n";
cout << "Time Height Velocity \n";
cout << "(hrs) (meters) (meters/s) \n";
// Set formats.
cout.setf(ios::fixed);
cout.precision(2);
// Compute and print report information.
// Determine number of iterations required.
// Use integer index to avoid rounding error.
loops = (int)( (final - initial)/increment );
for (int count=0; count<=loops; ++count) {
time = initial + count*increment;
height = -0.12*pow(time,4) + 12*pow(time,3) - 380*time*time + 4100*time + 220;
velocity = -0.48*pow(time,3) + 36*time*time - 760*time + 4100;
cout << setw(6) << time << setw(10) << height
<< setw(10) << velocity/3600 << endl;
if (height > max_height) {
max_height = height;
max_time = time;
} }
// Print maximum height and corresponding time.
cout << "\nMaximum balloon height was " << setw(8)
<< max_height << " meters \n";
cout << "and it occurred at " << setw(6) << max_time
<< " hours \n";
If we use the data from the hand example, we have the following interaction with the program:
Enter initial value for table (in hours) 0
Enter increment between lines (in hours) 1
Enter final value for table (in hours) 5
Section 4.6 Problem Solving Applied: Weather Balloons 167
Weather Balloon Information
Time Height Velocity
(hrs) (meters) (meters/s)
0 220.00 1.14
1 3951.88 0.94
2 6994.08 0.76
3 9414.28 0.59
4 11277.28 0.45
5 12645.00 0.32 Maximum balloon height was
12645.00 meters, and it occurred at 5.00 hours.
Figure 4.9 contains a plot of the altitude and velocity of the balloon for a period of 48 hours. From the plots, we can see the periods during which the balloon rises or falls.
2
1.5
1
0.5
00 5 10 15 20 25 30 35 40 45 50
Time, hr Balloon Altitude
Altitude, meters
104 1.5
1 0.5 0 –0.5
–10 5 10 15 20 25 30 35 40 45 50
Time, hr Balloon Velocity
Velocity, meters/sec
Figure 4.9 Weather balloon altitude and velocity.
168 Chapter 4 Control Structures: Repetition
Modify!
These problems relate to the program developed in this section that prints a table of weather balloon information.
1. Use this program to generate a table showing the weather balloon information for every 10 minutes for 2 hours, starting at 4 hours after the balloon was launched.
2. Modify the program to include a check to be sure that the final time is greater than the initial. If it is not, ask the user to reenter the complete set of report information.
3. The equations given in this section were developed to be accurate only for time from 0 to 48 hours, so modify the program to print a message to the user that specifies an upper bound of 48 hours. Also, check the user input to be sure that it stays within the proper bounds. If there are any errors, ask the user to reenter the complete set of report information.
4. If there are two times with the same maximum height, this program will print the first time that the maximum height occurred. Modify the program so that it will print the last time that the maximum height occurred.