Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please do in C (not C++, C+, or C#) Hurricane data Download the attached file th

ID: 3692753 • Letter: P

Question

Please do in C (not C++, C+, or C#) Hurricane data Download the attached file that contains hurricane data: each line is a record containing name, year and category of a hurricane. The very first line, however, contains the number of records in the file. Read the data in the file. Use dynamic memory allocation to provide for the space of the data to be read. Then print out the youngest records of the storms in each of the categories 5, 4 and 3. Here is what was in the data.txt file:

12 Allen 1980 3 Gloria 1985 3 Audrey 1957 4 Opal 1995 3 Donna 1960 4 Carla 1961 4 Camille 1969 5 Hazel 1954 4 Celia 1970 3 Frederic 1979 3 Hugo 1989 4 Andrew 1992 5

Explanation / Answer

/** C code to find youngest records of the storms in each of the categories 5, 4 and 3. **/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
int n;
FILE *fptr;

if ((fptr = fopen("inputfile","r"))==NULL)
{
printf("Error! opening file");
return 0; /* Program exits if file pointer returns NULL. */
}

fscanf(fptr,"%d",&n);
char name[20];
int year,category;


int *ptr_year = (int*)malloc(n*sizeof(int)); //memory allocated using malloc
int *ptr_category = (int*)malloc(n*sizeof(int)); //memory allocated using malloc
char *ptr_name[12];

int i = 0;
while(fscanf(fptr, "%s %d %d", name, &year, &category) == 3)
{
ptr_name[i] = malloc(strlen(name));
strcpy(ptr_name[i], name);
ptr_year[i] = year;
ptr_category[i] = category;
i++;
}

int youngest_3 = 0;
int youngest_4 = 0;
int youngest_5 = 0;
for (i = 0; i < 12; ++i)
{
if(ptr_category[i] == 3)
{
if(ptr_year[youngest_3] < ptr_year[i]) youngest_3 = i;
}

else if(ptr_category[i] == 4)
{
if(ptr_year[youngest_4] <ptr_year[i]) youngest_4 = i;
}

else if(ptr_category[i] == 5)
{
if(ptr_category[youngest_5] <ptr_year[i]) youngest_5 = i;
}

}

printf("youngest records of the storms in category 3: %s %d ", ptr_name[youngest_3], ptr_year[youngest_3]);
printf("youngest records of the storms in category 4: %s %d ", ptr_name[youngest_4], ptr_year[youngest_4]);
printf("youngest records of the storms in category 5: %s %d ", ptr_name[youngest_5], ptr_year[youngest_5]);

return 0;

}