Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Counting number in struct function https://gyazo.com/9f8028aeb0730687db541d24ffae7be3 // Starting code for COMP1511 lab exercises #include #include #define MAX_SPECIES_NAME_LENGTH 128 #define MAX_SIGHTINGS 10000 // a struct to

Counting number in struct function

https://gyazo.com/9f8028aeb0730687db541d24ffae7be3

 // Starting code for COMP1511 lab exercises #include  #include  #define MAX_SPECIES_NAME_LENGTH 128 #define MAX_SIGHTINGS 10000 // 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 date when; int how_many; char species[MAX_SPECIES_NAME_LENGTH]; }; int read_sightings_file(char filename[], int len, struct pod sightings[len]); int read_sighting(FILE *f, struct pod *w); int read_date(FILE *f, struct date *d); int count_orca_sightings(int n_sightings, struct pod sightings[n_sightings]); int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s  ", argv[0]); return 1; } struct pod whale_sightings[MAX_SIGHTINGS]; int n_sightings = read_sightings_file(argv[1], MAX_SIGHTINGS, whale_sightings); if (n_sightings > 0) { int n_orca_pods = count_orca_sightings(n_sightings, whale_sightings); printf("%d Orca sightings in %s ", n_orca_pods, argv[1]); } return 0; } // return the number of sightings of Orca int count_orca_sightings(int n_sightings, struct pod sightings[n_sightings]) { // REPLACE THIS COMMENT WITH YOUR CODE // THIS FUNCTION SHOULD NOT CALL SCANF OR PRINTF // IT SHOULD JUST RETURN A VALUE return 42; // CHANGE ME } // // DO NOT CHANGE THE FUNCTIONS BELOW HERE // // return number of sightings read from filename // -1 is returned if there is an error int read_sightings_file(char filename[], int len, struct pod sightings[len]) { FILE *f = fopen(filename, "r"); if (f == NULL) { fprintf(stderr,"error: file '%s' can not open ", filename); return -1; } int n_sightings = 0; while (read_sighting(f, &sightings[n_sightings]) == 1 && n_sightings < len) { n_sightings = n_sightings + 1; } fclose(f); return n_sightings; } // return 1 if a sighting can be read, 0 otherwise int read_sighting(FILE *f, struct pod *s) { if (read_date(f, &(s->when)) != 1) { return 0; } if (fscanf(f, "%d", &(s->how_many)) != 1) { return 0; } fgetc(f); if (fgets(s->species, MAX_SPECIES_NAME_LENGTH, f) == NULL) { return 0; } // finish string at ' ' if there is one char *newline_ptr = strchr(s->species, ' '); if (newline_ptr != NULL) { *newline_ptr = '\0'; } // also finish string at ' ' if there is one - files from Windows will newline_ptr = strchr(s->species, ' '); if (newline_ptr != NULL) { *newline_ptr = '\0'; } return 1; } // return 1 if a date can be read, 0 otherwise int read_date(FILE *f, struct date *d) { int n_scanned = fscanf(f, "%d/%d/%d", &(d->year), &(d->month), &(d->day)); return n_scanned == 3; } 

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_2

Step: 3

blur-text-image_3

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

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago