C Programming A flower shop sells various arrangements of a dozen flowers (roses
ID: 3796592 • Letter: C
Question
C Programming
A flower shop sells various arrangements of a dozen flowers (roses, lilies, daisies) in two colors each (red or white) with a choice of bouquet or vase. You are given an (incomplete) source file, florist.c, of a program that takes the order for a flower arrangement from the user and displays the cost. The program uses three enumerated types to define the type of flowers, color and flower arrangement. Your task is to complete the program and implement the getCost function to compute the cost based on the following rules.
1. The base price for each of the flowers is described in the following table.
Flower Price
Roses $30
Lilies $20
Daisies $45
2. Red Lilies and Red Daisies have an added cost of $5 and White Roses have an added cost of $10. There is no additional cost for other flower/color combinations
3. Bouquets are free of charge while vases add an additional $10 charge to the total
To complete the program, perform the following tasks.
1. Insert the required items in the 3 enumerated types. The Color enumerated type has already been completed for you. Note: the rest of the program assumes that all enumerated types begin with 1, you should keep this assumption so that you don’t need to change the block of printf and scanf menu statements.
2. After taking input from the user, the program calls the function getCost to determine the cost. This function takes three input parameters, the flower type, the color and the arrangement and returns the cost (a double).
(a) The function prototype has been completed for you. Implement the definition of this function to return the cost of a given arrangement using the costs shown on the price list.
(b) In the main function, declare the other required variable(s), complete the function call with appropriate arguments and print the cost.
----Partially Completed Program----
Explanation / Answer
#include <stdio.h>
/* 1. Insert the required items in the other 2 enumerated types*/
typedef enum { RED = 1, WHITE } Color;
typedef enum { ROSES = 1, LILIES, DAISIES} Flower;
typedef enum { BOUQUET = 1,VASE} Arrangement;
/*Function prototype */
double getCost(Flower flower, Color color, Arrangement arr);
int main (void) {
//You can declare variables that hold your enums just like regular variables
Flower flower;
Color color;
Arrangement arr;
/* 3. Declare the other required variable(s)*/
double cost;
//DO NOT CHANGE THIS BLOCK OF printf AND scanf STATEMENTS
printf(" Types of flowers ");
printf("1. Roses ");
printf("2. Lilies ");
printf("3. Daises ");
printf("Please enter the item number for your choice: ");
scanf(" %u ",&flower);
printf(" Color choices ");
printf("1. Red ");
printf("2. White ");
printf("Please enter the item number for your choice: ");
scanf("%u", &color);
printf(" Arrangements ");
printf("1. Bouquet ");
printf("2. Vase ");
printf("Please enter the item number for your choice: ");
scanf("%u", &arr);
/*4 complete the function call and print the cost*/
cost = getCost(flower,color,arr);
printf(" COST = $%.2lf ",cost);
return 0;
}
/* 2. Implement the function get_cost*/
double getCost(Flower flower, Color color, Arrangement arr){
double cost;
//you can use your enum in expressions
//Possible example: if(flower == Rose) {...}
if(flower == ROSES)
cost = 30;
else if(flower == LILIES)
cost = 20;
else if(flower == DAISIES)
cost = 45;
else
cost = 0;
//TODO: Compute the base price here
//TODO: add additional color-flower costs here
if(color == RED && (flower == LILIES || flower == DAISIES))
cost = cost + 5;
//TODO: add additional cost for vases
else if(color == WHITE && flower == ROSES)
cost = cost +10;
else if(arr == VASE)
cost = cost + 10;
else
cost = cost;
//TODO: return the total cost here
return cost;
}
Output:
version 2 -pointers
#include <stdio.h>
/* 1. Insert the required items in the other 2 enumerated types*/
typedef enum { RED = 1, WHITE } Color;
typedef enum { ROSES = 1, LILIES, DAISIES} Flower;
typedef enum { BOUQUET = 1,VASE} Arrangement;
/*Function prototype */
void getCost(Flower flower, Color color, Arrangement arr,double *cost);
int main (void) {
//You can declare variables that hold your enums just like regular variables
Flower flower;
Color color;
Arrangement arr;
/* 3. Declare the other required variable(s)*/
double cost;
//DO NOT CHANGE THIS BLOCK OF printf AND scanf STATEMENTS
printf(" Types of flowers ");
printf("1. Roses ");
printf("2. Lilies ");
printf("3. Daises ");
printf("Please enter the item number for your choice: ");
scanf(" %u ",&flower);
printf(" Color choices ");
printf("1. Red ");
printf("2. White ");
printf("Please enter the item number for your choice: ");
scanf("%u", &color);
printf(" Arrangements ");
printf("1. Bouquet ");
printf("2. Vase ");
printf("Please enter the item number for your choice: ");
scanf("%u", &arr);
/*4 complete the function call and print the cost*/
getCost(flower,color,arr,&cost);
printf(" COST = $%.2lf ",cost);
return 0;
}
/* 2. Implement the function get_cost*/
void getCost(Flower flower, Color color, Arrangement arr,double *cost){
//you can use your enum in expressions
//Possible example: if(flower == Rose) {...}
if(flower == ROSES)
*cost = 30;
else if(flower == LILIES)
*cost = 20;
else if(flower == DAISIES)
*cost = 45;
else
*cost = 0;
//TODO: Compute the base price here
//TODO: add additional color-flower costs here
if(color == RED && (flower == LILIES || flower == DAISIES))
*cost = *cost + 5;
else if(color == WHITE && flower == ROSES)
*cost = *cost +10;
else
*cost = *cost;
//TODO: add additional cost for vases
if(arr == VASE)
*cost = *cost + 10;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.