Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi! I have a question concerning programming with C I am asked to do the following exercise (below) and implemented it already. But my code

Hi!

I have a question concerning programming with C I am asked to do the following exercise (below) and implemented it already. But my code does not work (see code below as well). Can you make my code work correctly?

Thank you very much in advance!

Best regards,

Fiona

The exercise:

Familiarize yourself with the lecture_hall_t structure, which was declared using typedef to store properties of lecture halls. A lecture hall has a name name, a size size and number of seats num_seats. It is assumed that the lecture hall name is at most 64 characters long. Note that the lecture_hall_t structure must not be modified! Implement a function print_lecture_hall which is passed a pointer to a lecture hall as parameter and prints the lecture hall in one line on the standard output. Read the man page for qsort and familiarize yourself with the function. Make sure you can explain each parameter of the qsort function. Next, implement the following comparison functions to sort an array of auditorium data: name_comparator: compares the name of two lecture halls lexicographically. size_comparator: Compares the size of two auditoriums numerically. num_seats_comparator: Compares the number of seats in two lecture halls numerically. lecture_hall_comparator: Compares lecture hall data numerically by number of seats as well as size. If two lecture halls have the same number of seats, size is also used. Using qsort, implement the function with the signature void sort_lecture_halls(lecture_hall_t *lecture_halls, const size_t num_lecture_halls, int (*comparator)(const void *, const void *)), which sorts an array lecture_halls of length num_lecture_halls using the comparator function. In the main function, create an array with data from at least 4 lecture halls and test your sort_lecture_halls implementation with the four different comparator functions.

my code:

#include  #include  #define MAX_LEN_NAME 64 typedef struct lecture_hall { char name[MAX_LEN_NAME + 1]; // max. 64 signs long unsigned short size; unsigned short num_seats; } lecture_hall_t; void print_lecture_hall(const struct lecture_hall *lecture_hall) { printf("Name: %s", lecture_hall->name); printf("Size: %d", lecture_hall->size); printf("Number of seats: %d", lecture_hall->num_seats); } int name_comparator(const void *p, const void *q) { //takes two pointers for name // get the values at given address // char a = *(const int *)p; // char b = *(const int *)q; // char a = &lecture_halls[0].name; // char b = &lecture_halls[1].name // compare the names of two lecture halls lexicographical if ((*p) < (*q)) { return -1; } if ((*p) > (*q)) { return 1; } return 0; } int size_comparator(const unsigned short *a, const unsigned short *b) { // takes two pointer for size // compare the size of two lecture halls numerical if ((*a) < (*b)) { return -1; } if ((*a) > (*b)) { return 1; } return 0; } int num_seats_comparator(const void *c, const void *d) { // takes two pointers for # of seats // compare # of seats of two lecture halls numerical if ((*c) < (*d)) { return -1; } if ((*c) > (*d)) { return 1; } return 0; } int lecture_hall_comparator(const void *c, const void *d) { // compare # of seats of two lecture halls numerical if ((*c) < (*d)) { return -1; } if ((*c) > (*d)) { return 1; } // if # of seats is identical, size is used for comparison if ((*c) == (*d)) size_comparator(){ return -1 } if ((*c) == (*d)) { return 1; } return 0; } void sort_lecture_halls(lecture_hall_t *lecture_halls, const size_t num_lecture_halls, int (*comparator)(const void *, const void *)) { // use of build-in function qsort for sorting qsort(lecture_hall_t *lecture_halls, const size_t num_lecture_halls, int (*comparator)(const void *, const void *)); } int main(void){ // array definition of 4 lecture halls lecture_hall_t lecture_halls[]= {{"HSA", 50, 25}, {"HSB", 100, 28}, {"HSC", 40, 27}, {"HSD", 20, 17}}; // base: pointer to the first element of the array to be sorted *lecture_halls = lecture_halls[0]; // length of array = the number of elements in the array pointed by base size_t num_lecture_halls = sizeof(lecture_halls) / sizeof(*lecture_halls); // define lecture_hall pointer lecture_hall_t *lecture_hall = &lecture_halls[0]; // test case1 const void *p = &lecture_halls[0].name; const void *q = &lecture_halls[1].name; sort_lecture_halls(lecture_halls, num_lecture_halls,name_comparator(p,q)); print_lecture_hall(lecture_hall); // test case2 unsigned short *a = &lecture_halls[0].size; unsigned short *b = &lecture_halls[1].size; sort_lecture_halls(lecture_halls, num_lecture_halls, size_comparator(a,b)); print_lecture_hall(lecture_hall); // test case3 unsigned short *c = &lecture_halls[0].num_seats; unsigned short *d = &lecture_halls[1].num_seats; sort_lecture_halls(lecture_halls, num_lecture_halls, num_seats_comparator(c,d)); print_lecture_hall(lecture_hall); // test case4 sort_lecture_halls(lecture_halls, num_lecture_halls, lecture_hall_comparator(c,d)); print_lecture_hall(lecture_hall); return EXIT_SUCCESS; }

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

Step: 3

blur-text-image

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

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions

Question

3. How would this philosophy fit in your organization?

Answered: 1 week ago

Question

How would you assess the value of an approach like this?

Answered: 1 week ago