Thursday, December 13, 2018

structures_waves2

#include <stdio.h>
#define FILENAME "waves2.txt"
/* Define structure to represent a tsunami. */
struct tsunami
{
int mo, da, yr;
char location[20];
double fatalities, max_height;

};
int main(void)
{
/* Declare variables. */
int k=0, npts;
double max=0, sum=0, ave;
struct tsunami t[100];
{
FILE *waves2;
/* Read and print information from the file. */
waves2 = fopen(FILENAME,"r");
if (waves2 == NULL)
printf("Error opening data file. /n");
else
{
while (fscanf(waves2,"%d %d %d %s %lf %lf ",&t[k].mo,&t[k].da,
&t[k].yr, t[k].location, &t[k].fatalities,&t[k].max_height,) == 6)
{
sum = sum + t[k].max_height;
if (t[k].max_height > max)
max = t[k].max_height;
k++;
}
npts = k;
//ave = sum/npts;
printf("Summary Information for Tsunamis /n");
printf("Maximum Wave Height (in feet): %.2f /n",max*3.28);
printf("Average Wave Height (in feet): %.2f /n",ave*3.28);
printf("Tsunamis with greater than average heights: /n");
for (k=0; k <=npts-1; k++)
printf("%s /n",t[k].location);
fclose(waves2);
}
return 0;
}
}

No comments:

Post a Comment