Wednesday, December 12, 2018

Day 22 11/8/18

These problems relate to the program developed in this section to analyze tsunami data. 1. Modify the program to find and print the number of tsunamis in the file during the same year as the maximum wave height. 2. Modify the program to find and print the date of the tsunami with the largest number of fatalities. 3. Modify the program to find and print the locations for all tsunamis with over 100 fatalities. 4. Modify the program so that it counts the number of tsunamis that occurred during the month of July. 5. Modify the program so that it prints the number of tsunamis in Peru. Assume that the location might also include the city in Peru, so you will need to search the character string for the substring "Peru".


#include <stdio.h>
#include <stdlib.h>
#include <math.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, maxheight[10];
char location1[10];
struct tsunami tk[100];
FILE *waves;
// Read and print information from the file. /
waves = fopen(FILENAME,"r");
if (waves == NULL)
printf("Error opening data file. \n");
else
{
for(int a=0;a<10;a++){
fscanf(waves,"%d %d %d %s %lf %d",&tk[a].mo,&tk[a].da,&tk[a].yr,&tk[a].location,&tk[a].max_height,&tk[a].fatalities);
sum += tk[a].max_height;
if (tk[a].max_height > max){
max = tk[a].max_height;
}
maxheight[a]=tk[a].max_height*3.28;
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; k++){
if(maxheight[k]>ave*3.28){
printf("%s \n",tk[k].location);
}}
fclose(waves);
}
return 0;

}

No comments:

Post a Comment