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. listing0.c is as follows:
  7. * 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

AutoSave Off H. tiny - Search Chenimineni, Hemanthi CH Share Comme 47 O 5 File Home Insert Page Layout Formulas Data Review View Help Insert Calibri 11 A A Wrap Text General X Delete Paste BIU A Conditional Format as Cell Sort & Find & SEE $ % -28 Es Merge & Center Formatting Table Styles Format Filter Select Clipboard Font Alignment Number Styles Cells Editing 0 POSSIBLE DATA LOSS Some features might be lost if you save this workbook in the comma-delimited (.csv) format. To preserve these features, save it in an Excel file format. Don't show again Save As... WS Sensitivity Data Analysis Analysis Sensitivity A1 > fx 2015 A 1 J K L M N o P Q R R S T 60 4 118 4 4 141 1 2 17 2 6 1 3 B D E F G H 2217 lan Mitte Brunnensi 52.53454 13.40256 Entire hor 2986 Michael Pankow Prenzlaue 52.54851 13.40455 Private ro 3718 Britta Pankow Prenzlaue 52.535 13.41758 Entire hor 4108 Jana Tempelhc Schqnet 52.49885 13.34906 Private ro 17391 Bright Pankow Helmholt: 52.54316 13.41509 Private ro 33852 Philipp Pankow Prenzlaue 52.53303 13.41605 Entire hor 90 143 2015 2695 3176 3309 7071 9991 1 62 5 4 26 25 1 0 220 297 26 137 5 1 42 180 2 6 197 6 6 1 7 8 9 10 11 12 13 14 15 16 17 18 19 20

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!