Download the attached file that contains data for noteworthy tsunamis. Each reco
ID: 3821977 • Letter: D
Question
Download the attached file that contains data for noteworthy tsunamis. Each record has the following fields: month, day, year, fatalities, wave height in meters, location. Write a program that reads the data and prints out a report containing the following: the maximum wave height in feet the average wave height in feet all the locations of the tsunamis that were less than the average wave height. 09 02 1992 17 10 Nicaragua 12 02 1992 1000 26 Flores_Island 07 12 1993 239 31 Okushiri, Japan 06 02 1994 238 14 East_Java 11 14 1994 49 7 Mindoro_Island 10 9 1995 1 11 Jalisco, Mexico 01 01 1996 9 3.4 Sulawesi_Island 02 17 1996 161 7.7 Irian_Javy 02 21 1996 12 5 Peru 07 17 1998 2200 15 Papua, New_GuineaExplanation / Answer
#include<stdio.h>
typedef struct DATA { //since total size of ddat is not known, we need to use linked list approach and this is the node structure
int d,m,y,f;
float ht;
char name[20];
struct DATA* next;
} data;
void main () {
data *head = NULL;
FILE *fp = fopen("data.txt", "r"); //you may change the file name
data *temp = malloc(sizeof(data)); //create temp node
float total_ht = 0, avg, max_ht = -1;
int count = 0;
while (fscanf(fp, "%d %d %d %d %f %s", &(temp->d), &(temp->m), &(temp->y), &(temp->f), &(temp->ht), temp->name) > 0) { //scan data from file
temp -> next = head; //add temp node to start of list
head = temp;
total_ht += temp->ht; //update total ht and count
count ++;
if (temp->ht > max_ht)
max_ht = temp->ht; //update max_ht based on condition
temp = malloc(sizeof(data));
}
printf("Max ht: %f meters ", max_ht); //print max ht
avg = total_ht/count;
printf("Average ht: %f meters ", avg); //print avg
free(temp);
temp = head;
printf("Locations with height above avg: ");
while (temp != NULL) { // scan every node and check if ht is more than avg
if (temp->ht > avg)
printf("%s ", temp->name);
temp = temp->next;
}
}
I tried my best to keep the code simple and useful. I have also commented the critical sections of the codes to make thing easy at your end. If incase you face any other prob, please let me know. I shall be glad to help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.