Thursday, December 13, 2018

Weather Ballon

#include <stdio.h>
#include <math.h>
#define FILENAME "balloon1.txt"
int main(void)
{
/*  Declare variables.  */
double start_time_hour, start_time_minute, end_time_hour, end_time_minute, time_passed_hour, time_passed_minute;
double incre_min, total_time, time_incre, alt, vel, max_alt, max_time_of_alt;
double Ptime, Palt, Pvel, datapoints;
FILE *balloon;

/*  Open output file.  */
balloon = fopen(FILENAME,"w");

/*  Get user input from the keyboard.  */
do
{
printf("Enter start time of weather balloon release in hrs then min. \n");
printf("start time: \n");
scanf("%lf %lf", &start_time_hour, &start_time_minute);
printf("\nEnter final time of weather balloon traveling in hrs then min. \n");
printf("final time: \n");
scanf("%lf %lf",&end_time_hour, &end_time_minute);

/*  Determine length of time passed based on user inputted times  */
time_passed_hour = end_time_hour - start_time_hour;
time_passed_minute = end_time_minute - start_time_minute;
total_time = time_passed_hour + (time_passed_minute / 60);
/* Checks data entered to see if valid */
if (end_time_hour < start_time_hour)
{
printf("\nStart time is greater than final time. Please reenter time values again.\n\n");
}
    if (end_time_hour > 48)
{
printf("\nFinal time value cannot be greater than 48 hours. Please reenter time values again.\n\n");
}
} while (end_time_hour < start_time_hour || (end_time_hour > 48));

printf("\nEnter time increment in minutes.\n");
printf("time increment: \n");
scanf("%lf",&incre_min);

double unceil_step = (total_time * 60) / incre_min;
double steps = ceil((total_time * 60) / incre_min);

/*  Determine altitudes and velocites of balloon  */
//fprintf(balloon,"(time(hrs)) (alitude(m)) (velocity(m/s))\n");
fprintf(balloon,"%lf\n", steps + 1 );
for (double k = 0; k <= steps; k++)
{
double T = (k*(total_time/unceil_step)) + start_time_hour;
alt = -0.12*pow(T,4) + 12*pow(T,3) - 380*pow(T,2) + 4100*T + 220;
vel = -0.48*pow(T,3) + 36*pow(T,2) - 760*T + 4100;
if(alt > max_alt)
{
max_alt = alt;
max_time_of_alt = T;
};
fprintf(balloon,"%.6lf %.6lf %.6lf\n", T, alt, vel/3600);
}
fprintf(balloon, "\nmax alitude: %.3lf m\n", max_alt);
fprintf(balloon, "\ntime of max alitude: %.3lf hrs\n", max_time_of_alt);
fclose(balloon);
/*   Close file and exit .   */

balloon = fopen("balloon1.txt","r");
fscanf(balloon," %lf", &datapoints);
printf("\nTIME ALT VEL\n");
for (double w = 1; w <= datapoints; w++ )
{
fscanf(balloon,"%lf %lf %lf",&Ptime,&Palt,&Pvel);
printf("%1.6lf %.6lf %.6lf\n",Ptime,Palt,Pvel);
}
printf("\nmax alitude: %.3lf m\n", max_alt);
printf("\ntime of max alitude: %.3lf hrs\n", max_time_of_alt);

return 0;
}

No comments:

Post a Comment