In: Computer Science
I am using a Macbook and trying to use the terminal and a VIM editor to create program. This program will allow you to enter the costs of each ingredient and product, then prompt for the amount of each ingredient bought (individually by ingredient) and product sold (also individually), and from there show profit or loss, as well as profit on average for each unit sold. There will be say, 2 ingredients to buy and 3 products to sell. Please give really good explanation so there's no bumps met when compiling or swapping back and forth between the terminal and vim editor. Thanks in advance!!
Code-
# include<stdio.h>
int main()
{
/*Give total ingredients, by which one product is made of*/
int total_ingredients;
printf("Give total ingredients: ");
scanf("%d",&total_ingredients);
/*Give cost of each ingredients, that are store in array*/
float cost[total_ingredients];
printf("\n Give cost of each ingredient: ");
int i;
for(i=0;i<total_ingredients;i++)
{
printf("\n cost of %dth ingredient: ",i);
scanf("%f",&cost[i]);
}
/*Give total number of products to be made*/
int total_products;
printf("\n Total products to be made: ");
scanf("%d",&total_products);
/*Give cost of each product, on which we sell to customer*/
float cost_to_sell;
printf("\n Give cost to sell each product: ");
scanf("%f",&cost_to_sell);
int profit,loss;
float product_cost;
/*Compute actual cost of product*/
product_cost=0;
for(i=0;i<total_ingredients;i++)
{
product_cost=product_cost+cost[i];
}
/* Computing profit or loss on average */
if(product_cost<cost_to_sell)
{
printf("\n We made Total Profit of %f
",cost_to_sell-product_cost);
printf("\n Profit percentage = %f
",((cost_to_sell-product_cost)/product_cost)*100);
printf("\n We made Average Profit of %f
",(cost_to_sell-product_cost)/total_products);
}
else if(product_cost>cost_to_sell)
{
printf("\n We have Loss of %f
",product_cost-cost_to_sell);
printf("\n Loss percentage = %f
",((product_cost-cost_to_sell)/product_cost)*100);
printf("\n We have Average Loss of %f
",(product_cost-cost_to_sell)/total_products);
}
else
{
printf("\n We have neither Loss or profit ");
printf("\n We have Average Loss or profit of 0
");
}
return 0;
}
See comments in code to understand what is happening. Also see one test case in image.
Store code in purchase.c
To run, Press these two commands.
gcc purchase.c
./a.out