I have started to learn C and decided to recreate my bakery task in C.
As I am new to the language, I am unsure if I have approached the task in the right way using structs. Feedback on the style of the code would also be appreciated.
#include <stdio.h>
double cup_ingredients[4] = {4.0,0.1,12.0,14.0}; // Amount of each ingredient for 1 cupcake = {Butter, eggs, flour, sugar}
double lemon_ingredients[4] = {80.0,4.5,240.0,300.0}; // Amount of each ingredient for 1 lemon cake = {Butter, eggs, flour, sugar}
double total[4];
double cup_req;
double lemon_req;
struct Bags {
int big_bag;
int med_bag;
int small_bag;
};
void calc_bag(double total_ingredient, struct Bags* bag_sizes, struct Bags* type);
int main() {
printf("How many cupcakes would you like? ");
scanf("%lf", &cup_req);
for (int x = 0; x<cup_req; x++){ // For the number of cupcakes required:
for (int y = 0; y<4; y++){ // For each ingredient
total[y] += cup_ingredients[y]; // Add the amount of each ingredient to the total amount of that ingredient
}
}
printf("How many lemon cakes would you like? ");
scanf("%lf", &lemon_req);
for (int x = 0; x<lemon_req; x++){ // For the number of lemon cakes:
for (int y = 0; y<4; y++){ // For each ingredient
total[y] += lemon_ingredients[y]; // Add the amount of each ingredient to the total amount of that ingredient
}
}
//Structs for the amount of each ingredient a bag can hold
struct Bags Butter_size = {.big_bag = 500, .med_bag = 250, .small_bag = 125};
struct Bags Egg_size = {.big_bag = 12, .med_bag = 10, .small_bag = 6};
struct Bags Flour_size = {.big_bag = 750, .med_bag = 500, .small_bag = 250};
struct Bags Sugar_size = {.big_bag = 600, .med_bag = 400, .small_bag = 200};
//Set the bags required to 0
struct Bags Butter_req = {0,0,0};
struct Bags Egg_req = {0,0,0};
struct Bags Flour_req = {0,0,0};
struct Bags Sugar_req = {0,0,0};
//Calculate the amount of each ingredient bag required
calc_bag(total[0], &Butter_size, &Butter_req);
calc_bag(total[1], &Egg_size, &Egg_req);
calc_bag(total[2], &Flour_size, &Flour_req);
calc_bag(total[3], &Sugar_size, &Sugar_req);
printf("\nButter: %d large bags, %d medium bags, %d small bags.", Butter_req.big_bag, Butter_req.med_bag, Butter_req.small_bag);
printf("\nEgg: %d large bags, %d medium bags, %d small bags.", Egg_req.big_bag, Egg_req.med_bag, Egg_req.small_bag);
printf("\nFlour: %d large bags, %d medium bags, %d small bags.", Flour_req.big_bag, Flour_req.med_bag, Flour_req.small_bag);
printf("\nSugar: %d large bags, %d medium bags, %d small bags.", Sugar_req.big_bag, Sugar_req.med_bag, Sugar_req.small_bag);
}
void calc_bag(double total_ingredient, struct Bags* bag_sizes, struct Bags* type){
while (total_ingredient > 0){
if (total_ingredient > bag_sizes->big_bag) {
type->big_bag++;
total_ingredient -= bag_sizes->big_bag;
}
else if (total_ingredient > bag_sizes->med_bag) {
type->med_bag++;
total_ingredient -= bag_sizes->med_bag;
}
else if (total_ingredient > bag_sizes->small_bag) {
type->small_bag++;
total_ingredient -= bag_sizes->small_bag;
}
else {
type->small_bag++;
total_ingredient = 0;
}
}
}