I don't know what to do.

My teacher assigned the class a lab, and I don't know what to do. To be more specific, I don't how to setup the calculation formula.
The instructions are:

Problem Statement – A large apple juice carton can hold 3.35 liters of juice. Each morning, Dale’s Apple Orchard ships cartons of apple juice to a nearby Costco. The profit of producing one liter of apple juice is $0.37.
Write a program that:
A.Gets the total amount of apple juice Dale’s Apple Orchard produced on a given day (in liters) as input from the user.
B.Given the input, the program should then calculate:
a. The total number of cartons needed, and
b. The profit of producing the apple juice
C.Next, the program should print the following information on screen:
a. The number of cartons needed, and
b. The profit of producing the apple juice


Last edited on
If you were to do this by hand, how would you solve this problem?
Before you start programming and automating a process, you need to understand the problem and how to solve it.

Here's the information you've been given.
1. 1 carton holds 3.35 liters.
2. Profit from one liter is $0.37.
3. User will provide you with the total amount of apple juice produced on a given day (In liters).

Let's say that the amount given by user is 40 liters.

a. How do you calculate the total number of cartons needed to hold 40 liters?
(Remember that 1 carton holds 3.35 liters)

b. How do you calculate the profit from 40 liters?
(Remember that profit from 1 liter is $0.37)

Once the calculation is done, you can print the results to the screen.




Last edited on
So if I understood this correctly:

1
2
carton = amount / 3.35;
profit = amount * .37;


Am I correct?
Horror,

They are correct. Now, you need to think about the data type of the values.

For amounts (liters and dollars), you would need to use decimal. Do you know what data type in C++ can represent decimal numbers?

For cartons, you would need to use integers. What C++ data type represents integer numbers?

Also, when you calculate the number of cartons required to hold the amount of juice, you should note that the calculated value can very well be a decimal number - for example, 11.94 cartons. Normally, when people talk about the number of containers required to hold something, they expect a natural number like 1, 2, 3, 4, and 5. You wouldn't tell them that they need 11.94 cartons. Rather, you would say that they need 12 cartons (Always rounding up a decimal number is referred to as ceiling). I don't know if your teacher would expect you to round up the number of containers or just represent it as a decimal number. You would have to find out.

Now that you understand the problem, try coding.
Hello Horror,

You wrote:
So if I understood this correctly:

1
2
carton = amount / 3.35;
profit = amount * .37;


Am I correct?

Yes and no.

The formula for "carton" is correct, but you would do better writing it as:carton = amount / AMOUNTPERCARTON; Where "AMOUNTPERCARTON" is defined as a constant at the beginning of "main". Using "3.35" in the calculation is called using a magic number and these should be avoided. In your small program finding "3.35" and changing it would be easy, but in a large program with hundreds or thousands of lines this becomes a problem.

By defining a constant like: constexpr double AMOUNTPERCARTON{ 3.35 }; // <--- In liters. you can use the variable name in the program and only have one place to change its value if needed. And for the value, that should be written as 0.37, the same applies. Also the value of profit/liter is likely to change more often.

The formula for "profit" is wrong. This would be based on what you can sell not what you have to start with. You will need to know the number of cartons you have to sell and multiply it by the "AMOUNTPERCARTON" to find the number of liters you have to sell then multiply that result by the profit/liter. Hint: parentheses are involved in the formula.

Hope that helps,

Andy
Andy,

So if I'm understanding what you're saying correctly, the profit is:
1
2
profit = carton * AMOUNTPERCARTON;
const double AMOUNTPERCARTON = 3.35;


Hello Horror,

Yes except that you lines are out of order. You need to define the constant before you use it.

My personal choice if to define constants at the beginning of "main", the first lines, of near the beginning of the file. This way they are easy to find when you go looking for them.

For the equation for the variable "profit" you are half way there. "carton * AMOUNTPERCARTON" will give you the amount of liters that you have to sell. It may not be necessary, but I put parentheses around this part. You are missing the second part. Take a look at http://www.cplusplus.com/forum/beginner/249687/#msg1099771 which is half correct, but in the opposite direction.

This is a portion of code to give you an idea what I did:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	constexpr double AMOUNTPERCARTON{ 3.35 };  // <--- In liters.
	constexpr double PROFITPERLITER{ 0.37 };

	double totalJuice{ 1000 }, totalProfit{};
	int cartons{};

	std::cout << "\n Enter the amount of juice to process in liters: ";
	std::cout << totalJuice;
	//std::cin >> totalJuice; 

Notice that "totalJuice" has been given a value when its defined and the "std::cin" is commented out. This way you can test the program without having to enter something each time the program runs. Just a little trick to speed up testing. When testing is finished remove the " 1000 " and switch the comments on lines 10 and 11.

Hope that helps,

Andy
Topic archived. No new replies allowed.