Friday, November 3, 2017

Day 11 - File IO

CLASSWORK:
Wave Data Code
/* This program determines the maximum height of a wave that is the sum of two specified waves. */
#include <stdio.h>
#include <math.h>
#define PI 3.141593
#define FILENAME "waves1.txt"
int main(void)
{
/* Declare variables. */
int k;
double A1, A2, freq1, freq2, height1, height2, length1, length2;
double T1, T2, w1, w2, sum, new_period, new_height, time_incr, t;
double maxwave=0;
FILE *waves;
/* Open output file. */
waves = fopen(FILENAME,"w");
/* Get user input from the keyboard. */
printf("Enter integer wave period (s) and wave height (ft) \n");
printf("for wave 1: \n");
scanf("%lf %lf",&T1,&height1);
printf("Enter integer wave period (s) and wave height (ft) \n");
printf("for wave 2: \n");
scanf("%lf %lf",&T2,&height2);
/* Determine and print wavelengths. */
length1 = 5.13*T1*T1;
length2 = 5.13*T2*T2;
printf("Wavelengths (in ft) are: %.2f %.2f \n",length1,length2);
new_period = T1*T2;/* Determine period of combined waves. */
/* Compute 200 points of the combined waves over the */
/* period specified, and find the maximum height. */
time_incr = new_period/200;
A1 = height1/2;
A2 = height2/2;
freq1 = 1/T1;
freq2 = 1/T2;
for (k=0; k<=199; k++)
{
t = k*time_incr;
w1 = A1*sin(2*PI*freq1*t);
w2 = A2*sin(2*PI*freq2*t);
sum = w1 + w2;
fprintf(waves,"%.4f %.4f %.4f %.4f \n",t,w1,w2,sum);
if (sum >maxwave)
maxwave = sum;
}
new_height = maxwave*2;
/* Print new wave maximum. */
printf("Maximum combined wave height is %.2f ft \n",new_height);
fclose(waves);/* Close file and exit program. */
return 0;

}

HOMEWORK:
Balloon Data Code:
/* This program calculates the amplitude and velocity of a weather balloon and stores
the data values in a file called balloon1.txt */
#include <stdio.h>
#include <math.h>
#define FILENAME "balloon1.txt"

int main(void){
//Declare and initialize variables
double start_time_hrs, time_incr_min, end_time_hrs, upper_time_bound, t;
double altitude_m, velocity_m_hr, velocity_m_s;
double peak_altitude=0, max=0, t_peak=0;
double num_data_pts=0;

FILE *balloon;

//Open up balloon data file
balloon = fopen(FILENAME, "w");

printf("Welcome to my Program!\n");
printf("\n");

//Ask user to input information from keyboard
printf("Input an integer upper time bound(in hrs): \n");
scanf("%lf", &upper_time_bound);

//If upper time bound is a positive nonzero int...
if (upper_time_bound > 0) {
{
//Ask user to input start and end times...
printf("Input an integer start time(hrs) & an integer end time(hrs):\n");
scanf("%lf %lf", &start_time_hrs, &end_time_hrs);
}
}
//If the time bound is negative or zero...
else {
{
printf("Error: Only positive nonzero integers allowed. \n");
printf("Please reenter report information correctly. \n");
return 0;
}
}

//If the end time is greater than the start time...
//and both start time and end time are positive...
if ((end_time_hrs > start_time_hrs) && (start_time_hrs >= 0) && (end_time_hrs > 0)) {
{
//Ask user to input time increment
printf("Input an integer time increment(in min): \n");
scanf("%lf", &time_incr_min);

//If the time increment is positive...
if (time_incr_min > 0)
{
//Calculate number of data points
num_data_pts = ((end_time_hrs - start_time_hrs)/(time_incr_min/60)) + 1;

//Print the number of data points
printf("\nNumber of data points: %f \n", num_data_pts);
printf("You're good!\n");
//The program now runs the for loop below...
}
//If the time increment is negative or zero...
else
{
printf("Error: Only positive nonzero integers allowed. \n");
printf("Please reenter report information correctly. \n");
return 0;
}
}
}

//Else, check if start time or end time are negative values...
else if ((start_time_hrs < 0) || (end_time_hrs <= 0)) {
{
printf("Error: Please enter positive values from 0 to %f. \n", upper_time_bound);
return 0;
}
}

//If they're both still positive, then print this out...
else {
{
printf("Error: Start times and end times reversed. \n");
printf("Please reenter report information correctly. \n");
return 0;
}
}

//Create table for altitude and velocity values
for (double k=0; k<=num_data_pts-1; ++k)
{
t = start_time_hrs + k*(time_incr_min/60);

//Calculate altitude and velocity in m and m/hr
altitude_m = (-.12*t*t*t*t) + (12*t*t*t) - (380*t*t) + (4100*t) + 220;
velocity_m_hr = (-.48*t*t*t) + (36*t*t) - (760*t) + 4100;

//Convert velocity to SI units
velocity_m_s = velocity_m_hr/3600;

//Print out altitude and velocity in m and m/s to balloon file
fprintf(balloon, "%.4f %.4f %.4f \n", t, altitude_m, velocity_m_s);

//Let the peak altitude be equal to the current altitude
peak_altitude = altitude_m;
//If the current altitude is larger than the previous max value...
if (peak_altitude > max)
{
//Let the new max value be equal to
//the current altitude at the current time interval
max = peak_altitude;
t_peak = t;
}
}
printf("\nFinished calculating values \n");
printf("Maximum altitude reached: %.4f m at %.4f hrs", peak_altitude, t_peak);
//Close file and exit program.
fclose(balloon);
return 0;

}

No comments:

Post a Comment