Question

In: Computer Science

I am using a Macbook and trying to use the terminal and a VIM editor to...

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!!


in C language, please

Solutions

Expert Solution

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


Related Solutions

My code does not compile, I am using vim on terminal and got several compiling errors...
My code does not compile, I am using vim on terminal and got several compiling errors this is C++ language I need help fixing my code below is the example expected run and my code. Example run (User inputs are highlighted): Enter your monthly salary: 5000 Enter number of months you worked in the past year: 10 Enter the cost of the car: 36000 Enter number of cars you’ve sold in the past year: 30 Enter number of misconducts observed...
Open the terminal and using vi/vim create a new script and name it lab6 Insert at...
Open the terminal and using vi/vim create a new script and name it lab6 Insert at the top of your file a comment with your name, class and section number (e.g. #John Doe, EMT 2390L - OL123). In this lab you are going to display a multiplication table of a number entered by the user between 2 and 10 using a for loop and without using multiplication. The table will display the values that are less than or equal to...
you can use either vim or nano as text editor Implement the following code in ARM...
you can use either vim or nano as text editor Implement the following code in ARM on Raspberry Pi, compile and run. g=12, h=8, i=2, j=5; f = (g + h) - (i + j); Your program displays the message: f = (g + h) – (i + j) = 13 Note: answer should be calculated not hardcoded
This is Using MATLAB: I am trying to store the solution of this matrix because I...
This is Using MATLAB: I am trying to store the solution of this matrix because I want to do something with the result like find the norm of that answer however I am stuck and cannot seem to be able to. Help would be appreciated! --------------------------------------------- MATLAB CODE: close all clear clc A = [1 -1 2 -1; 2 -2 2 -3; 1 1 1 0; 1 -1 4 5]; b = [-8 -20 -2 4]'; x = gauss_elim(A,b) function...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
I am trying to solve this problem, but I don't know which test to use: For...
I am trying to solve this problem, but I don't know which test to use: For average risk funds only, test the proportion of funds with Sales Charges is not 50% use α = 0.10. Explain your conclusion. Here is the contingency table with the data: Sales charge Yes No total Risk Low 6 8 14 Avg 42 34 76 High 24 23 47 Total 72 65 137 Context of the problem: Mutual funds are the most common way people...
I am trying to work on LCD display using VHDL. I want to display the word...
I am trying to work on LCD display using VHDL. I want to display the word “TECH” and make it blink on LCD. I need the complete source code (If anyone helps me and the code works I will offer bonus cash for it )
I am trying to tokenize a string using a function by passing the char string[] and...
I am trying to tokenize a string using a function by passing the char string[] and char *pointer[100]. While I have working code inside the int main(), I am having trouble actually declaring the parameters for the function. I know how to pass the char array (char string[]), but not how to pass the char pointer array (char *pointer[100]). This is my code below: int main() {    // Declare variables    char str[] = "this is a test only...
I am trying to use the countif or countifs function to count the number of genres...
I am trying to use the countif or countifs function to count the number of genres used to describe a film and no matter what syntax I use, the result is 0. Can I use the countif(s) function to count columns? I want to count the number of movies that have 3 or more genre's associated with them. Here is a sample of the data I am working with: primarykey Movie Genre1 Genre2 Genre3 Genre4 Genre5 Genre6 Genre7 tt0499549 Avatar...
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT