CLASSWORK:
Hurricane Data:
#include <stdio.h>
#define FILENAME "storms2.txt"
/* Define structure to represent a hurricane. */
struct hurricane
{
char name[10];
int year, category;
};
int main(void)
{
/* Declare variables and function prototype. */
int max_category=0, k=0, npts;
struct hurricane h[100];
FILE *storms;
void print_hurricane(struct hurricane h);
/* Read and print information from the file. */
storms = fopen(FILENAME,"r");
if (storms == NULL)
printf("Error opening data file. \n");
else
{
printf("Hurricanes with Maximum Category \n");
while (fscanf(storms, "%s %d %d",h[k].name,&h[k].year,
&h[k].category) == 3)
{
if (h[k].category > max_category)
max_category = h[k].category;
k++;
}
npts = k;
for (k=0; k<=npts-1; k++)
if (h[k].category == max_category)
print_hurricane(h[k]);
fclose(storms);
}
/* Exit program */
return 0;
}
/*?????????????????????????????????????????????????????????????*/
/* This function prints the hurricane information. */
void print_hurricane(struct hurricane h)
{
printf("Hurricane: %s \n",h.name);
printf("Year: %d, Category: %d \n",h.year,h.category);
return;
}
HOMEWORK:
Tsunami Data:
#include <stdio.h>
#define FILENAME "waves2.txt"
/* Define structure to represent a tsunami. */
struct tsunami
{
int mo, da, yr, fatalities;
double max_height;
char location[20];
};
int main(void)
{
/* Declare variables. */
int k=0, npts;
double max=0, sum=0, ave;
struct tsunami t[100];
FILE *waves;
/* Read and print information from the file. */
waves = fopen(FILENAME,"r");
if (waves == NULL)
printf("Error opening data file. \n");
else
{
while (fscanf(waves,"%d %d %d %lf %d %s",&t[k].mo,&t[k].da,
&t[k].yr,&t[k].max_height,&t[k].fatalities,t[k].location[20]) == 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++)
if (t[k].max_height > ave)
printf("%s \n",t[k].location);
fclose(waves);
}
return 0;
}
No comments:
Post a Comment