Completed Code: Need help adjusting this code to fit activity 3. /* 1. Insert th
ID: 3796621 • Letter: C
Question
Completed Code:
Need help adjusting this code to fit activity 3.
/* 1. Insert the required items in the other 2 enumerated types*/
typedef enum { RED = 1, WHITE = 2 } Color;
typedef enum {Roses =1, Lilies = 2, Daisies = 3} Flower; // Added enum type for flowers
typedef enum {Bouquet =1, Vase = 2} Arrangement; // Added enum type for the arrangements
/*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;
//Declared variable for arrangement enum type
Arrangement arr;
/* 3. Declare the other required variable(s)*/
// cost variable for storing the total cost returned by the function getCost
int 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("%d", &flower);
printf(" Color choices ");
printf("1. Red ");
printf("2. White ");
printf("Please enter the item number for your choice: ");
scanf("%d", &color);
printf(" Arrangements ");
printf("1. Bouquet ");
printf("2. Vase ");
printf("Please enter the item number for your choice: ");
scanf("%d", &arr);
/*4 complete the function call and print the cost*/
// Added the parameter to the function
cost = getCost(flower, color, arr);
// Printed the total cost returned by the getCost method
printf("The total cost for the arrangement of flowers is $%d", cost);
return 0;
}
/* 2. Implement the function get_cost*/
double getCost(Flower flower, Color color, Arrangement arr){
int base_price =0; // base_price variable stores the price of the flower chosen
int additional_cost = 0; // additional_cost stores the additional cost for the color of the flower chosen if applicable
int arrangement_cost = 0; // arrangement_cost stores the cost for the chosen arrangement of the flowers
int total_cost; // total_cost stores the total cost which is the sum of the above three variables
//you can use your enum in expressions
//Possible example: if(flower == Rose) {...}
//TODO: Compute the base price here
if(flower == Roses)
base_price = 30;
else if(flower == Lilies)
base_price = 20;
else
base_price = 45;
//TODO: add additional color-flower costs here
if(flower == Roses && color == WHITE)
additional_cost = 10;
if(flower == Lilies && color == RED)
additional_cost = 5;
if(flower == Daisies && color == RED)
additional_cost = 5;
//TODO: add additional cost for vases
// if the vase arrangement is chosen, then arrangement_cost is $10 otherwise it remains 0 for bouquet arrangement
if(arr == Vase)
arrangement_cost = 10;
//TODO: return the total cost here
//total cost is the sum of all the three costs evaluated as above
total_cost = base_price + additional_cost + arrangement_cost;
return total_cost;
}
Chrome File Edit View History Bookmarks People Window Help AA A) 58% E Wed Feb 22 1:22:27 AM Karina Q E Karina Micros x D Micro X Bb Lecture x https:// x S30 C Prog X C Secure https://blackboard.unl edu bbcswebdav/pid-3864688-dt conten Microsoft Word-Lab-EnumsFunctions-Handout.docx n you andou Activity 3 Due to a harsh winter, Red Daisies are no longer available. We will make the appropriate changes to the getCost function to accommodate this change and to communicate errors through the function's return type. The cost will be communicated to the calling function by reference rather than as its return value. 1. Change the signature of the getCost function to: int getCost (double *cost, Flower flower Color color Arrangement arr 2. Change the definition of the function to do the following: a. If given invalid arguments, it returns a value of 1 (indicating an error) b. If given valid arguments, it returns a value of 0 (indicating no error) and places the cost in the cost variable (which is passed by reference) Lab Handout: Enums and Functions 3. In the main function, make other necessary changes to accommodate the above In addition, you should check the return value of the getCost function and print an error message in the case that it results in an error. Name the program as florist1.c 4. Test your changes and demonstrate them to a lab instructor. Activity 4Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
typedef enum { RED = 1, WHITE = 2 } Color;
typedef enum {Roses =1, Lilies = 2, Daisies = 3} Flower; // Added enum type for flowers
typedef enum {Bouquet =1, Vase = 2} Arrangement; // Added enum type for the arrangements
typedef enum { No_Error = 1, Unavailable_Selection = 2, Invalid_Argument = 3, Null_Pointer = 4 } Error;
int getCost( double *cost, Color color, Flower flower, Arrangement arr);
int main (void) {
Flower flower;
Color color;
Arrangement arr;
Error error;
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("%d", &flower);
printf(" Color choices ");
printf("1. Red ");
printf("2. White ");
printf("Please enter the item number for your choice: ");
scanf("%d", &color);
printf(" Arrangements ");
printf("1. Bouquet ");
printf("2. Vase ");
printf("Please enter the item number for your choice: ");
scanf("%d", &arr);
getCost(&cost, color, flower, arr, error);
if (cost > 0){
printf("Total cost : $%.2lf", cost);
}
else {
printf("Invalid_Argument");
}
return 0;
}
int getCost( double *cost, Color color, Flower flower, Arrangement arr){
Error error;
if((flower == 3) && (color == 1)) {
error = 2;
}
else if((flower != 1) && (flower != 2) && (flower != 3)){
error = 3;
}
else if((color != 1) && (color != 2)){
error = 3;
}
else if((arr != 1) && (arr != 2)){
error = 3;
}
else if(cost == NULL){
error = 4;
}
//Compute cost
else{
if(flower == Roses){
*cost = 30;
}
else if(flower == Lilies){
*cost = 20;
}
else if(flower == Dasies){
*cost = 45;
}
if(flower == Roses && color == White){
*cost = *cost + 10;
}
if(flower == Lilies && color == Red){
*cost = *cost + 5;
}
if(flower == Dasies && color == Red){
*cost = *cost + 5;
}
if(arr == Vase){
*cost = *cost + 10;
error = 1;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.