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:

  1. 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.
  2. After each sort step, display the contents of the sorted structure.
  3. 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.
  4. Create a git repository and add your source code to this git repository following the instructions from previous lab.
  5. Create a file called README.md and include instructions for compiling and executing your program.
  6. 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 #include #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:

#############################

Modify the program listing.c to read the input file listings.csv and write########################

A B C D F G H - J K L M N 1 60 4 118 4 141 2015 2695 2 2217 lan 2986 Michael 3718 Britta 17 2 6 1 0 E Brunnenstr. S%d Prenzlauer Berg Nordwest Prenzlauer Berg S%dwest Schqneberg-Nord Helmholtzplatz 3 Mitte Pankow Pankow Tempelhof - Schqneberg Pankow 3176 52.53453732 52.54851279 52.53499619 52.49885493 52.54315726 13.40255693 Entire home/apt 13.40455283 Private room 13.41757867 Entire home/apt 13.34906453 Private room 13.4150911 Private room 90 62 143 1 220 297 4 26 5 25 1 3309 7071 4108 Jana 17391 Bright 5 42 2 197 1 26

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!