Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please use the code provided below and see the hint and please make sure it is 100% correct and please don't use gets this is
please use the code provided below and see the hint and please make sure it is 100% correct and please don't use gets
this is C program
#includeA citizen science project monitoring whale populations has files of containing large numbers of whale observations. Each line in these files contains: the date the observation was made the number of whales in the pod the species of whales Here is a file of example data which you can download to test your program. The first 10 lines are: $ head whales.txt 18/01/18 9 Pygmy right whale 01/09/17 21 Southern right whale 16/02/18 4 Striped dolphin 02/07/17 4 Common dolphin 19/02/18 4 Blue whale 21/02/18 38 Dwarf sperm whale 14/06/17 29 Southern right whale 20/06/17 3 Spinner dolphin 22/07/17 34 Indo-Pacific humpbacked dolphin 20/03/18 7 Long-finned pilot whale Download orca.c here, or copy it to your CSE account using the following command: $ cp -n /web/dp1091/2111/activities/orca/orca.c. Your task is to add code to this function in orca.c: // // return the number of Orca pods in sightings 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 } orca.c already contains the functions which you discussed in your tutorial which read the file of whale sightings into an array of structs. You do not need need to change these functions. The main function in orca.c has this code: int n_orca_pods = count_orca_sightings (n_sightings, whale_sightings); printf("%d Orca sightings in %s ", n_orca_pods, argv[1]); Your task in this exercise is only to complete count_orca_sightings Do not change any other function. It should return a count of the number of sightings of Orca pods in the file. Note, "pod" is the collective noun for a group of whales. count_orca_sightings should return the number of Orca pods. It does not have have to sum the number of individual whales in these pods. When you have completed the function count_orca_sightings this is how orca.c should behave: $ dcc -o orca orca.c $ ./orca whales.txt 53 Orca sightings in whales.txt Hint: hint use strcmp from string.h Hint: most of the work for this exercise is figuring out what to do - only a short loop containing an if statement is needed#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 Orca pods in sightings // 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 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started