Create a C program that can describe a restaurant. It should have members that i
ID: 3717355 • Letter: C
Question
Create a C program that can describe a restaurant. It should have members that include the name, address, average cost, and type of food. Suppose an array of structures representing restaurants has been created (include a few restaurants for each food type). Write a function that asks the user to input a food type and prints out all restaurants' names of a given food type in order of cost, least expensive first. (food types: American, Chinese, Italian, Mexican). Be sure to include the input/result.
Explanation / Answer
Hi,
Please find my code.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct retaurant
{
char name[20];
char address[50];
float avgCost;
char typeOfFood[30];
}Restaurant;
void print(Restaurant *restaurants[],char typeOfFood[],int size)
{
int i,j,temp;
for(i=1;i<size;i++)
{
temp=restaurants[i]->avgCost;
for(j=i-1;j>=0;j--)
{
if(temp<restaurants[j]->avgCost)
{
restaurants[j+1]->avgCost=restaurants[j]->avgCost;
}
else break;
}
restaurants[j]->avgCost=temp;
}
for(i=0;i<size;i++)
{
printf(" Name of restaurant %d: ",i+1);
puts(restaurants[i]->name);
printf(" Address of restaurant %d: ",i+1);
puts(restaurants[i]->address);
printf(" Average cost of food at restaurant %d: ",i+1);
printf("%f",restaurants[i]->avgCost);
printf(" Type of food served at restaurant %d: ",i+1);
puts(restaurants[i]->typeOfFood);
}
}
void main()
{
int i,num;
printf("Enter the number of restaurants: ");
scanf("%d",&num);
fflush(stdin);
Restaurant *restaurants[num];
for(i=0;i<num;i++)
{
Restaurant *restaurant=malloc(sizeof(Restaurant));
printf(" Enter name of restaurant %d: ",i+1);
fgets (restaurant->name, 30, stdin);
fflush(stdin);
printf(" Enter address of restaurant %d: ",i+1);
scanf("%[^ ]%*c",&(restaurant->address));
fflush(stdin);
printf(" Enter average cost of food at restaurant %d: ",i+1);
scanf("%f",&restaurant->avgCost);
fflush(stdin);
printf(" Enter type of food served at restaurant %d: ",i+1);
scanf("%[^ ]%*c",&(restaurant->typeOfFood));
fflush(stdin);
restaurants[i]=restaurant;
}
for(i=0;i<num;i++)
{
printf(" Name of restaurant %d: ",i+1);
puts(restaurants[i]->name);
printf(" Address of restaurant %d: ",i+1);
puts(restaurants[i]->address);
printf(" Average cost of food at restaurant %d: ",i+1);
printf("%f",restaurants[i]->avgCost);
printf(" Type of food served at restaurant %d: ",i+1);
puts(restaurants[i]->typeOfFood);
}
char food[30];
int k=0;
printf(" Enter the type of food by which you want to judge: ");
scanf("%[^ ]%*c",&food);
Restaurant *specificRestaurants[num];
for(i=0;i<num;i++)
{
if(strcmp(restaurants[i]->typeOfFood,food)==0)
{
specificRestaurants[k]=restaurants[i];
k++;
}
}
print(specificRestaurants,food,k);
}