Linear Equations
In this assignment, you will write two small programs to solve systems of linear equations in different contexts. The second problem is more challenging and carries more weight in the rubric.
Stage, commit, and push your changes to your assignment repository on GitHub classroom (check the Canvas assignment for the invite link). After doing so, you should be able to see your up-to-date work on your assignment repository page in your web browser. If so, you have successfully submitted your assignment.
Make sure to follow the best practices and guidelines mentioned in the style guide, and check your assignment rubric in Canvas.
Don’t forget to attend your assignment demo, which you should have already scheduled by this point.
Problem 1: Costs and Revenue
As an engineer and entrepreneur, you have founded a startup and are ready to start production on your innovative product, the X Device 9000. The cost to manufacture each device is dollars. Additionally, there is a base cost of dollars, which includes all expenses before manufacturing begins (e.g., labor, materials for R&D, prototyping, and preparing the manufacturing plant).
If you manufacture devices, the total cost is given by:
Assume that , , and are all non-negative.
Each device generates dollars in revenue. Therefore, the total revenue is:
Your goal is to at least break even, meaning your total revenue should be at least equal to your total costs. You already know the values of , , and from your research and surveys. Now, you need to determine the minimum number of devices, , that you need to manufacture and sell to break even.
Instead of solving this math problem repeatedly, you decide to write a simple program to automate the process, as you plan to create many more devices in the future.
The Math
To break even, we need to solve for such that . Substituting the definitions of and into this equation, we get:
Using algebra, we can solve for :
This is the only equation you’ll need for the assignment.
However, this equation might give you a floating-point number for (e.g., it might determine that you need to manufacture and sell devices to break even). Since it’s not possible to sell a fraction of a device, you must round your answer up to the nearest integer to ensure you at least break even. You can use the ceil()
function from the <cmath>
header file to do this (refer to studio 3 for more details).
The Submission
The template code on GitHub Classroom comes with a file called revenue.cpp
. It’s mostly empty. Complete it to create a C++ program that does the following:
-
Prompt the user and accept values via the terminal for , , and . Your program should support floating point values for all of these variables (e.g., is a valid user input). You may assume that the user will enter at most two decimal places (i.e., to represent cents).
-
Solve for and print its value to the terminal (don’t forget to round the value up before printing it, as described).
Your program does not need to validate user inputs. That is, you may assume that the user will enter appropriate values when prompted.
Problem 2: Home Gardener
Starting a home garden is an exciting journey, but you may have noticed that fertilizers can be quite expensive. After researching, you’ve discovered that it’s possible to make your own slow-release fertilizers using affordable ingredients like bone meal, blood meal, and banana peels.
Each ingredient contains varying amounts of nitrogen (N), phosphorus (P), and potassium (K), the three key elements essential for plant growth. In fertilizers, the proportions of these elements are expressed as the NPK ratio. For beginner gardeners, the common recommendation is to use a balanced fertilizer, which means the NPK ratio should have equal amounts of nitrogen, phosphorus, and potassium.
Let’s say you have three fertilizer ingredients, and you already know their respective proportions of nitrogen, phosphorus, and potassium. Specifically:
- Let , , and (values between 0 and 1) represent the proportions of nitrogen in ingredients 1, 2, and 3, respectively.
- Similarly, let , , and represent the phosphorus proportions, and , , and represent the potassium proportions.
For example, if , , and , then ingredient 2 consists of 3% nitrogen, 7% phosphorus, and 0% potassium.
To create your final fertilizer mix, let , , and represent the mixing proportions of the three ingredients. These values must sum to 1 because they define how much of each ingredient contributes to the final blend. For instance, if , , and , your final fertilizer will consist of 50% ingredient 1, 20% ingredient 2, and 30% ingredient 3.
Now, suppose you know the NPK proportions for all three ingredients, i.e., for . Your task is to determine the values of , , and that create a balanced fertilizer with equal proportions of nitrogen, phosphorus, and potassium.
This involves solving a mathematical problem that balances the NPK ratios in the final fertilizer. Since this is something you may need to do repeatedly as you garden, you’ve decided to write a program to automate the calculations.
The Math
This isn’t a math class, so I’ve already done the math for you! If you’re in a hurry, feel free to skip to the last paragraph of this section, where you’ll find the specific equations you need to implement to solve the problem. However, I recommend reviewing this entire section to understand how the solution is derived.
To calculate the total amount of any element (nitrogen, phosphorus, or potassium) in the final fertilizer, you use a weighted average of the element’s proportions in the three ingredients. The weights are the mixing proportions of each ingredient. For example, let’s assume the following values:
- , , and , meaning ingredient 1 is 7% nitrogen, ingredient 2 is 1% nitrogen, and ingredient 3 is 4% nitrogen.
- , , and , meaning the final mix consists of 50% ingredient 1, 20% ingredient 2, and 30% ingredient 3.
The total proportion of nitrogen in the final fertilizer is calculated as:
This means the final fertilizer contains 4.9% nitrogen.
The same approach applies to calculate the final proportions of phosphorus and potassium. However, we’re not concerned with the exact proportions of these elements. What we want is for the three proportions to be equal, creating a balanced fertilizer. Additionally, we know that:
This gives us the following system of linear equations to solve:
In this system:
- The lowercase letters are known constants representing the proportions of nitrogen, phosphorus, and potassium in the ingredients.
- The uppercase letters are the variables to solve for.
Since there are three equations and three variables, the system is solvable. Let’s start by rearranging the third equation to express in terms of and :
Next, substitute into the first two equations:
Simplify by distributing and refactoring terms:
Now, we’re left with two equations and two variables. Let’s solve the first equation for in terms of . After some algebra, we get:
Next, substitute this expression for into the second equation and solve for . After simplifying, the result is:
At this point, can be calculated by substituting all the known constants into the equation. Once you’ve found , use it in the earlier equation for to calculate its value. Finally, substitute the values of and into the equation for to complete the solution.
The Submission
The provided template code includes a file named gardening.cpp
, which is mostly empty. Your task is to complete this file to create a C++ program that performs the following steps:
- Prompt the user in the terminal (in the specified order) to input values for , , , , , , , , and . The program should accept floating-point values for all these variables.
- Solve for the mixing proportions , , and .
- Print the values of , , and to the terminal.
- Calculate the total proportion of nitrogen in the fertilizer as , and display the result using
std::cout
. - Calculate the total proportion of phosphorus in the fertilizer as , and display the result using
std::cout
. - Calculate the total proportion of potassium in the fertilizer as , and display the result using
std::cout
.
Limitations and Edge Cases:
- Linear Dependency: In rare cases, the element proportion constants may exhibit linear dependency, leading to either no solutions or infinite solutions.
- Zero Denominators: If any denominator in the computations is zero, your program will crash.
- Negative Values: The program may output negative values for , , and , indicating that it’s impossible to create a balanced fertilizer using the given inputs.
If any of these scenarios occur, it is not considered a failure of your program. These are inherent limitations of the problem, and such cases will not be penalized. You don’t need to account for these issues while writing your program.
To avoid running into these edge cases during testing, ensure that each of the three ingredients is concentrated in a different element. For example:
- Ingredient 1 should contain significantly more nitrogen than phosphorus or potassium.
- Ingredient 2 should contain significantly more phosphorus than nitrogen or potassium.
- Ingredient 3 should contain significantly more potassium than nitrogen or phosphorus.
When the ingredients are well-differentiated in this way, the program should compute positive values for , , and without any issues.
In the last part of your solution design, you’ll be expected to come up with a few other test cases, including at least one other “good” test case such as this one.
Simulators
Problem 1 Simulator
Revenue per product must be greater than cost per product.