Question: Modify the program listing.c to read the input file listings.csv and write two new functions that will sort the data based on the host_name and
Modify the program listing.c to read the input file listings.csv and write two new functions that will sort the data based on the host_name and price. You can perform the following steps in the C program:
- Use any sorting algorithm to sort by the keys host_name and price separately. Make sure when you sort on any given attribute that you rearrange the entire structure. For example, if you are sorting the whole list by host_name then you need to change the order of structures based on the host_name. If you like, you can use the qsort function provided by the C library for sorting. Here is an example from the man page on how to use qsort function: qsort.c or qsort2.c. A even better example can be found here: bsearch.c.
- After each sort step, display the contents of the sorted structure.
- Test your program to ensure that the output is correctly sorted based on host_name and price. You can use the small input file tiny.csv to debug and test your program.
- Create a git repository and add your source code to this git repository following the instructions from previous lab.
- Create a file called README.md and include instructions for compiling and executing your program.
- You do not have to use Makefile for this assignment or use file operations to read the file, just I/O redirection as shown in the example above.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
listing.c:
##########
/* Sample program to read a comma separated file into a structure and display the array of structures */
#include
#define LINESIZE 1024
struct listing { int id, host_id, minimum_nights, number_of_reviews, calculated_host_listings_count,availability_365; char *host_name, *neighbourhood_group, *neighbourhood, *room_type; float latitude, longitude, price; };
/* create a struct, initialize it, and return it */ struct listing getfields(char* line){ struct listing item;
/* Note: you have to pass the string to strtok on the first invocation and then pass NULL on subsequent invocations */ item.id = atoi(strtok(line, ",")); item.host_id = atoi(strtok(NULL, ",")); item.host_name = strdup(strtok(NULL, ",")); item.neighbourhood_group = strdup(strtok(NULL, ",")); item.neighbourhood = strdup(strtok(NULL, ",")); item.latitude = atof(strtok(NULL, ",")); item.longitude = atof(strtok(NULL, ",")); item.room_type = strdup(strtok(NULL, ",")); item.price = atof(strtok(NULL, ",")); item.minimum_nights = atoi(strtok(NULL, ",")); item.number_of_reviews = atoi(strtok(NULL, ",")); item.calculated_host_listings_count = atoi(strtok(NULL, ",")); item.availability_365 = atoi(strtok(NULL, ",")); return item; }
/* display the struct */ void displayStruct(struct listing item) { printf("ID : %d ", item.id); printf("Host ID : %d ", item.host_id); printf("Host Name : %s ", item.host_name); printf("Neighbourhood Group : %s ", item.neighbourhood_group); printf("Neighbourhood : %s ", item.neighbourhood); printf("Latitude : %f ", item.latitude); printf("Longitude : %f ", item.longitude); printf("Room Type : %s ", item.room_type); printf("Price : %f ", item.price); printf("Minimum Nights : %d ", item.minimum_nights); printf("Number of Reviews : %d ", item.number_of_reviews); printf("Calculated Host Listings Count : %d ", item.calculated_host_listings_count); printf("Availability_365 : %d ", item.availability_365); }
int main(int argc, char* args[]) { struct listing list_items[22555]; char line[LINESIZE]; int i, count;
count = 0; /* read input till end of input */ while (fgets(line, LINESIZE, stdin) != NULL){ list_items[count++] = getfields(line); } /* display structure */ for (i=0; i ------------------------------------------------------------------------------------------------------------------------ sample of listings.csv: #############################
########################
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
