I don't know what to do now. Below is my code I have so far but I have no idea what to do next.
Here's the prompt for what the program should do:
Welcome to the Weather Balloon Altitude and Velocity Program.
Enter the Balloon's Starting Time: 1
Enter the Time Increment: 2.2
Enter the Balloon's Ending Time: 10
~> more balloon.dat
Weather Balloon Information
Time (hr) Altitude (m) Velocity (m/s)
1.00 3951.88 0.94
3.20 9829.43 0.56
5.40 13066.73 0.27
7.60 14298.57 0.05
9.80 14092.26 -0.10
Peak Altitude: 14298.57 (m)
Corresponding Time: 7.6 hr
Assume that the following polynomial representation represents the altitude or height in meters during the first 48 hours following the launch of a weather balloon:
h(t) = -0.12t4 + 12t3 – 380t2 + 4100t +220,
where the units of t are in hours. The corresponding polynomial model for the velocity in meters/hr of the weather balloon is:
v(t) = -0.48t3 + 36t2 -760t + 4100.
Write a program that will print a table of the time, altitude, and the velocity for this weather balloon using units of m and m/s. Let the user enter the start time, the increment in time between lines of the table, and the ending time, where all the time values must be less than 48 hours. After your table is printed out, also print the peak altitude and its corresponding time to a file called balloon.dat.
Specifications:
The format for your output table numbers should be %6.2f for the time, and %9.2f for both the altitude and velocity information. To format your table, use the “\t” (tab) character. For instance, you can use a statement similar to: printf(“%6.2f\t\t%9.2f\t%9.2f\n”,time,altitude,velocity);
Here's my code I typed up so far:
/* Directives */
#include <stdio.h>
#define FILENAME "balloon.dat"
int main(void)
{
float startingtime, timeincrement, endingtime, height, velocity;
FILE *balloon;
/* Open file. */
balloon = fopen(FILENAME,"b");
printf("Welcome to the Weather Balloon Altitude and Velocity Program.\n");
printf("\n");
printf("Enter the Balloon's Starting Time: ");
scanf("%f", &startingtime);
printf("Enter the Time Increment: ");
scanf("%f", &timeincrement);
printf("Enter the Balloon's Ending Time: ");
scanf("%f", &endingtime);
/* Close file. */
fclose(balloon);
/* End Program */
return(0);
}
6 answers
Notice that if the local maximum occurs between the printing intervals (and most likely to happen), then you will miss the maximum, by a little, probably.
Here's the pseudocode:
Declare additional variables;
set maxHeight to -1000;
for(time=startingtime;time<=endingtime;time+=timeincrement)
height=formula given for height
velocity=formula given for velocity
if(height>maxHeight)then (maxHeight=height and maxTime=time)
print out time, height, velocity
end for
fprintf(balloon,"formatting string", maxTime, maxHeight);
Post your code if you need additional information.
However, there is a problem with looping using floats in that the last cycle may not be executed because of rounding problems.
With for loops using integers, from 3 to 12 in steps of three will go through 3,6,9 and 12.
With for loops using floating point numbers using the same limits 3.0 to 12.0 in steps of 3.0 might give something like 3.0, 6.0000002, 9.0000004 and the 12 will not be executed because 12.0000006 exceeds 12.
While-loops have the same rounding problem unless we take care of the problem.
We can define a small tolerance value such as 0.000001 and call it epsilon, or simply eps. Also, I think it is preferable to use double instead of float, if permitted by your teacher.
Here is a test using while-loop:
double eps=0.0000001; // use 0.00001 for float
double first=3.1416, last=21.9912, step=3.1416;
x=first;
while (x<=last+eps)
{
printf("cycle = %lf\n",x);
x+=step;
}
The for-loop can be constructed similarly.
I'm looking at the code you posted and I have a question. What would go into "time"? Like when you say time=staringtime; time<=endingtime;
Declare additional variables;
set maxHeight to -1000;
for(time=startingtime;time<=endingtime;time+=timeincrement)
height=formula given for height
velocity=formula given for velocity
if(height>maxHeight)then (maxHeight=height and maxTime=time)
print out time, height, velocity
end for
fprintf(balloon,"formatting string", maxTime, maxHeight);
time is a dummy variable that takes up the value of startingTime initially, and incremented by step=timeIncrement until it reaches endingTime. It has the same function as variable i in a for-loop:
for(i=0;i<maxNumber;i++){....}
"Declare additional variables;
set maxHeight to -1000; "
Forgot to annotate this.
maxHeight is a variable that stores the maximum value of height the iterations encounter. It is set initially to a small or impossible value so that it gets updated every time a new height is calculated. When the new height is greater than the maxHeight, then maxHeight is replaced by the new (and greater) height. So at the end of the day, maxHeight represents the highest altitude the balloon achieved. Of course, the absolute maximum is not known, because it could occur between the steps.
Also, in case it puzzles you, in the last line,
"formatting string" would be something like:
"At time %lf the maximum height %lf was attained\n"