Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #define MAX_SPECIES_NAME_LENGTH 4096 // a struct to represent the date // a whale pod sighting was made struct date { int year;

image text in transcribed

#include  #include  #include  #define MAX_SPECIES_NAME_LENGTH 4096 // a struct to represent the date // a whale pod sighting was made struct date { int year; int month; int day; }; // a struct to represent a sighting // of a pod (group) of whales struct pod { struct pod *next; struct date *when; int howMany; char *species; }; struct pod *readSightingsFile(char filename[]); struct pod *readSighting(FILE *f); struct date *readDate(FILE *f); struct date *stringToDate(char *s); void betweenDaysWhales(struct pod *firstPod, struct date *startDay, struct date *finishDay); // PUT YOUR FUNCTION PROTOTYPES HERE int main(int argc, char *argv[]) { if (argc != 4) { fprintf(stderr, "Usage: %s    ", argv[0]); return EXIT_FAILURE; } struct pod *firstPod = readSightingsFile(argv[1]); struct date *startDay = stringToDate(argv[2]); struct date *finishDay = stringToDate(argv[3]); betweenDaysWhales(firstPod, startDay, finishDay); return EXIT_SUCCESS; } // Print the whale sightings between start_day and finish_day inclusive. // One line containing number of whales and species is printed for each sighting. void betweenDaysWhales(struct pod *firstPod, struct date *startDay, struct date *finishDay) { // PUT YOUR CODE HERE } // PUT YOUR FUNCTIONS HERE // // DO NOT CHANGE THE FUNCTIONS BELOW HERE // // return linked list of sightings read from filename // exit called if there is an error struct pod *readSightingsFile(char filename[]) { FILE *f = fopen(filename, "r"); if (f == NULL) { fprintf(stderr,"error: file '%s' can not open ", filename); exit(EXIT_FAILURE); } struct pod *firstSighting = NULL; struct pod *lastSighting = NULL; struct pod *sighting = readSighting(f); while (sighting != NULL) { if (firstSighting == NULL) { firstSighting = sighting; firstSighting->next = NULL; } else { lastSighting->next = sighting; } lastSighting = sighting; sighting = readSighting(f); } return firstSighting; } // read a whale sighting (date, number of whales, whale species) // return a pointer to a malloced struct containing these details // return NULL if a sighting can not be read struct pod *readSighting(FILE *f) { struct pod *p = malloc(sizeof (struct pod)); if (p == NULL) { fprintf(stderr, "out of memory "); exit(1); } p->next = NULL; p->when = readDate(f); if (p->when == NULL) { free(p); return NULL; } int nScanned = fscanf(f, "%d", &(p->howMany)); if (nScanned != 1) { free(p); return NULL; } fgetc(f); char speciesBuffer[MAX_SPECIES_NAME_LENGTH]; if (fgets(speciesBuffer, MAX_SPECIES_NAME_LENGTH, f) == NULL) { free(p); return NULL; } // finish string at ' ' if there is one char *newlinePtr = strchr(speciesBuffer, ' '); if (newlinePtr != NULL) { *newlinePtr = '\0'; } // also finish string at ' ' if there is one - files from Windows will newlinePtr = strchr(speciesBuffer, ' '); if (newlinePtr != NULL) { *newlinePtr = '\0'; } // malloc a char array long enough to hold species name // and copy species to it p->species = malloc(strlen(speciesBuffer) + 1); if (p->species == NULL) { fprintf(stderr, "out of memory "); exit(EXIT_FAILURE); } strcpy(p->species, speciesBuffer); return p; } // read a date in year/month/day format from stream f // return a pointer to a malloced date struct containing them // return NULL if a date can not be read struct date *readDate(FILE *f) { struct date *d = malloc(sizeof (struct date)); if (d == NULL) { fprintf(stderr, "out of memory "); exit(1); } int n_scanned = fscanf(f, "%d/%d/%d", &(d->day), &(d->month), &(d->year)); if (n_scanned != 3) { free(d); return NULL; } return d; } // given a string containing a date in day/month/year format // return a pointer to a malloced date struct containing them // return NULL if a date can not be read struct date *stringToDate(char *s) { struct date *d = malloc(sizeof (struct date)); if (d == NULL) { fprintf(stderr, "out of memory "); exit(EXIT_FAILURE); } int nScanned = sscanf(s, "%d/%d/%d", &(d->day), &(d->month), &(d->year)); if (nScanned != 3) { free(d); return NULL; } return d; }
The main function in between_days_whale_list.c has this code: struct pod *firstPod-readsightingsFile (argv1]) struct date *startDaystringToDate (argv[2]) struct date *finishDay-stringToDate (argvl 3]); betweenDaysWhales (firstPod, startDay, finishDay) Your task in this exercise is to complete this function: // Print the whale sightings between start_day and finish_day inclusive. // One line containing number of whales and species is printed for each sighting void betweenDaysWhales (struct pod *firstPod, struct date *startDay, struct date *finishDay) Do not change any other function day_whale should print for the specified every whale sighting between the two dates, including the dates themselves. The sightings should be printed in the order they occur in the linked list of structs You should add at least two new functions You are not permitted to use arrays in this exercise When you have completed the function dayWhale this is how between_days_whale list.c should behave: ./between_days_whale_list whales.txt 29/12/17 03/01/18 30/12/17 2 Coastal bottlenose dolphin 29/12/17 24 Dwarf sperm whale 03/01/18 21 Fin whale 29/12/17 34 Pygmy right whale 02/01/18 2 Short-finned pilot whale 29/12/17 12 Striped dolphirn 30/12/17 2 Orca 03/01/18 41 Indo-Pacific humpbacked dolphin 03/01/18 1 Dwarf minke whale 30/12/17 29 Striped dolphin 03/01/18 6 Coastal bottlenose dolphin 30/12/17 25 Pygmy right whale 02/01/18 5 Spinner dolphirn 02/01/18 33 Short-finned pilot whale 03/01/18 27 Coastal bottlenose dolphin 01/01/18 35 Indo-Pacific humpbacked dolphin 03/01/18 25 Dwarf sperm whale Hint: use the printf format "%02d/%02d/%92d" to print dates and ..%2d" to print how many whales

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

2nd Edition

0471180742, 978-0471180746

More Books

Students also viewed these Databases questions