Question

C Programming: I am working on the problem below and have got all of the code...

C Programming:

I am working on the problem below and have got all of the code down, except for one part I can't figure out.

The logic for the calculation where the the total at each shop is giving after each iteration of the loop.

this is the formula I've got:

ingredientPrice += ingredient1;

It calculates the total for the first shop properly, but for the shops that follow it gives a cumulative total rather than the singular total for shops #2, 3, 4 etc.

I hope I am clear enough, hoping someone can help me with the formula

Prompt:

Your mentor has tasked you with going to several different stores to place orders. This program should ask for the total number of shops that will be visited. At each shop, ask for the number of ingredients that need to be purchases. For each ingredient, ask for the price. Keep track of the total for the order so that you can write it down before leaving the shop. This program should also track with order was the cheapest and which shop the cheapest order was at. This will help your mentor decide who to keep doing business with!

Sample Run:


How many shops will you be visiting?
3
You are at shop #1.
How many ingredients do you need at shop #1?
2
How much is ingredient #1?
3.5
How much is ingredient #2?
10
Your total at shop #1 is $13.50.
You are at shop #2.
How many ingredients do you need at shop #2?
3
How much is ingredient #1?
2.75
How much is ingredient #2?
3
How much is ingredient #3?
1.25
Your total at shop #2 is $7.00.
You are at shop #3.
How many ingredients do you need at shop #3?
2
How much is ingredient #1?
4
How much is ingredient #2?
5
Your total at shop #3 is $9.00.
Your cheapest order was at shop #2 and cost $7.00.

Homework Answers

Answer #1

Here is the code for you:

#include <stdio.h>
int main()
{
int numOfShops;
printf("How many shops will you be visiting? ");
scanf("%d", &numOfShops);
float subTotal = 0;
float cheapestTotal = 0;
float ingredient;
int cheapestShopIndex = 1;
int numOfIngredients;
for(int i = 1; i <= numOfShops; i++)
{
printf("You are at shop #%d.\n", i);
printf("How many ingredients do you need at shop #%d? ", i);
scanf("%d", &numOfIngredients);
subTotal = 0;
for(int j = 1; j <= numOfIngredients; j++)
{
printf("How much is ingredient #%d? ", j);
scanf("%f", &ingredient);
subTotal += ingredient;
}
if(i == 1)
cheapestTotal = subTotal;
if(subTotal < cheapestTotal)
{
cheapestTotal = subTotal;
cheapestShopIndex = i;
}
printf("Your total at shop #1 is $%.2f.\n", subTotal);   
}
printf("Your cheapest order was at shop #%d and cost $%.2f.\n", cheapestShopIndex, cheapestTotal);
}

And the output screenshot is:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I have done these problems but am unsure if the answers I got were correct, Please...
I have done these problems but am unsure if the answers I got were correct, Please show all steps so I can fully understand! 1.) How many microliters of original sample are required to produce a final dilution of 10-3 in a total volume of 1 mL?       INFO: 1 microliter is 10-6 L or 10-3 mL. 2.) If you suspect your culture of bacteria has 240 x 106 cells per mL, what would you want the final dilution to be...
Hello, I am working on an assignment but I am unsure of how to solve it....
Hello, I am working on an assignment but I am unsure of how to solve it. Please help me. The assignment details are below. Consider this scenario: Your friend starts a website, nothingbutflags.com, which is not making money. Your friend asks you to help generate more traffic. You ask your friend how much traffic the website had last month? And your friend replies and says only 500 visits. You also ask how many flags did you sell? Your friend replies...
Hello, I am working on a internet marketing/business assignment but I am unsure of how to...
Hello, I am working on a internet marketing/business assignment but I am unsure of how to solve it. Please help me. The assignment details are below. Consider this scenario: Your friend starts a website, nothingbutflags.com, which is not making money. Your friend asks you to help generate more traffic. You ask your friend how much traffic the website had last month? And your friend replies and says only 500 visits. You also ask how many flags did you sell? Your...
I have done these problems but am unsure if the answers I got were correct, Please...
I have done these problems but am unsure if the answers I got were correct, Please show all steps so I can fully understand! 1.) A 0.1 dilution is performed on a culture of bacteria in order to perform viable plate counts. From the dilution, *0.1 mL* of solution is plated on solid media, and 154 colony forming units grow on the plate. How many bacteria are in a single mL of the original culture? Express your answer to two...
I have the below homework problem for a python programming class, and i really need some...
I have the below homework problem for a python programming class, and i really need some help!! First, take a set of 6 grades from a user and average them. Provide the average to the user. You need to check to make sure the grades are within the normal range. If the grade is less than 0 or more than 100, issue a warning to the user. You don't need to take the grade again, just let the user know....
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have this much public class Main { int WIDTH = 0; double HEIGHT = 0; public static void drawSmileyFaces() { System.out.println(" :-):-):-):-):-):-)"); } public static void main (String[] args) { for (int WIDTH = 0; WIDTH < 3; WIDTH++ ) { for(double HEIGHT = 0; HEIGHT < 2; HEIGHT++) { drawSmileyFaces(); } } } } I am pretty sure that alot of this is wrong...
Microsoft Visual Studio in C#: I have this code and need to loop through each element...
Microsoft Visual Studio in C#: I have this code and need to loop through each element of the patient's array and add to a listbox (lbAccountDisplay) each property of the patient's object. This is the 2nd time to ask this question; it should have all the detail you need to help me. It is a piece of a project and I am trying not to give you the whole project. How do I use a foreach loop to loop through...
This is the code I am working on for a project using MATLAB: clc,clear G=27.7*10.^9; b=0.286*10.^-9;...
This is the code I am working on for a project using MATLAB: clc,clear G=27.7*10.^9; b=0.286*10.^-9; v=0.334; x=(-5*10.^-9:0.1:5*10.^-9); y=(-5*10.^-9:0.1:-1*10.^-9); [xx,yy]=meshgrid(x,y); stress=(-G*b)*yy*(3^xx+yy^2)/(2*pi.*(1-v))*(xx^2+yy^2).^2; figure mesh(xx,yy,stress); grid on xlabel('X') ylabel('Y') zlabel('Stress') Whenever I try to run this I keep getting the "Error using mesh" line, and then an error for "mesh(xx,yy,stress)". I know I am not catching the issue but how do I fix this? If you could explain as well that be extremely helpful!
I am working on the below problem: Problem 11-15 Risky Cash Flows The Bartram-Pulley Company (BPC)...
I am working on the below problem: Problem 11-15 Risky Cash Flows The Bartram-Pulley Company (BPC) must decide between two mutually exclusive investment projects. Each project costs $7,500 and has an expected life of 3 years. Annual net cash flows from each project begin 1 year after the initial investment is made and have the following probability distributions: PROJECT A PROJECT B Probability Net Cash Flows Probability Net Cash Flows 0.2 $7,000 0.2 $        0   0.6 6,750 0.6 6,750 0.2 8,000...
I am diluting wastewater (ww) with creek water. Say I have an initial concentration of 10%...
I am diluting wastewater (ww) with creek water. Say I have an initial concentration of 10% wastewater in a bin. I have 1 liter wastewater and 9 liters creek water. I keep pouring 1 liter aliquots of creek water into this bin to further dilute. My trash bin only holds a volume of 50 liters, but I need to keep diluting. If I take out half of the total volume in my bin (to have only 25), but I keep...