#include <stdio.h>
#include <stdlib.h>
struct restaurant {
char *name;
char *address;
int average_cost;
char *type_of_food;
};
typedef struct restaurant restaurant;
restaurant my_restaurants[] =
{
{"Maxies", "Bredgade 2", 205, "fisk"},
{"Minies", "Tyvej 22", 100, "kinesisk"},
{"Fiskies", "Jyllandsgade 23", 150, "fisk"},
{"Blondies", "Fredrik Bajers Vej 122", 117, "fisk"},
{"Brownies", "Bredgade 2222", 502, "fisk"}
};
int compare_cost(const void *ell1, const void *ell2){
restaurant *el1 = (restaurant*)ell1;
restaurant *el2 = (restaurant*)ell2;
if (el1 ->average_cost == el2->average_cost) return 0;
else if (el1->average_cost < el2->average_cost) return -1;
else return 1;
};
/* print the name of those restaurants with food of kind.
Sort the restaurants according to prices. n is the number of restaurants passed. */
void print_food(char *kind, restaurant restaurants[], int n){
int i;
qsort(restaurants,n,sizeof(restaurant),&compare_cost);
for (i=0; i < n; i++)
if (!strcmp(kind,restaurants[i].type_of_food)) printf("%s\n",restaurants[i].name);
};
int main(void){
int number_of_restaurants = sizeof(my_restaurants)/sizeof(restaurant);
print_food("fisk", my_restaurants, number_of_restaurants);
}
![]() | struct restaurant {
char *name;
char *address;
int average_cost;
char *type_of_food;
};
typedef struct restaurant restaurant; |
defines a structure used to describe a restaurant including name, address, average cost and type of food |
![]() | restaurant my_restaurants[] =
{
{"Maxies", "Bredgade 2", 205, "fisk"},
{"Minies", "Tyvej 22", 100, "kinesisk"},
{"Fiskies", "Jyllandsgade 23", 150, "fisk"},
{"Blondies", "Fredrik Bajers Vej 122", 117, "fisk"},
{"Brownies", "Bredgade 2222", 502, "fisk"}
}; |
Declares a variable my_restaurants as an array of restaurants. Please notice the nice initializer. |
![]() | int compare_cost(const void *ell1, const void *ell2){
restaurant *el1 = (restaurant*)ell1;
restaurant *el2 = (restaurant*)ell2;
if (el1 ->average_cost == el2->average_cost) return 0;
else if (el1->average_cost < el2->average_cost) return -1;
else return 1;
}; |
Defines a function compare_cost that compares the average_cost field of two pointers to elements of type restaurant. The function returns 0 if they are equal, -1 if the first is the smallest and 1 otherwise. Before the comparison is carried out a type cast is performed. This looks strange, but later on the compare_cost function will be given as an argument to the library function qsort, and qsort has certain type requirements (see p. 546). |
![]() | void print_food(char *kind, restaurant restaurants[], int n){
int i;
qsort(restaurants,n,sizeof(restaurant),&compare_cost);
for (i=0; i < n; i++)
if (!strcmp(kind,restaurants[i].type_of_food)) printf("%s\n",restaurants[i].name);
}; |
A function which sorts the restaurants, and print those of a given kind. |
![]() | int main(void){
int number_of_restaurants = sizeof(my_restaurants)/sizeof(restaurant);
print_food("fisk", my_restaurants, number_of_restaurants);
} |
The main function which just calls print_food. Notice the way we calculate the number of restaurants by means of the sizeof operator. |
![]() | The result of the program is Blondies Fiskies Maxies Brownies |