Lab 2: IO & Variables
- Due No Due Date
- Points 1
- Submitting a file upload
Note: You are encouraged to work on the following with a partner.
Overview
In this lab, you'll program solutions to a number of problems that involve processing input and computing formulas.
Learning outcomes
By the end of this lab, you should be more comfortable...
- using cin to read in numbers from the user
- performing integer and floating point arithmetic
- using cout to display data to the console
- using C++ math functions
Exercises
Complete the following zyLabs in the zyBook. While you do them, be sure to implement incrementally—write a line or two of code, then compile/run it. Test frequently by clicking "Submit mode" and then "Submit for grading". You have unlimited submits, so don't worry about using some quota up.
- zyLab 2.25 Divide by x
- zyLab 2.28 Expression for calories burned during workout
- zyLab 2.29 Using math functions
- zyLab 2.38 Computing areas of trapezoids.
Submitting
Click "Submit mode" then "Submit for grading" when you are finished so I can see that you have done the zyLab.
PA options
Please see the syllabus and course schedule (both on the homepage) for more information about how many programming assignments you are required to pass, due dates, etc. Of note: you do not need to attempt every or even most PAs.
PA 2.1: Area under the curve
There are many applications for which we must calculate the area under a function (in the mathematical sense). If you've taken a Calculus II course, you'll know that this is done using integration when possible. However, we can also approximate the area under a function. In this programming assignment, you will create a program that will approximate the area under 2-degree polynomial functions in the form: y = a + bx + cx2 between some starting and ending value of x. For example, here is the polynomial y = 1 + 2x + 3x2:
The area between the interval starting at x = 0.1 and ending at x = 3 is shaded in blue here:
To calculate the area, you will divide the interval into thirds. In each third, you will compute the area of the trapezoid that fits the points (x1, 0), (x1, y1), (x2, y2), (x2, 0):
Computing the area of a trapezoid with a square end involves splitting it into a rectangle and triangle, computing the area of each of those, and adding them together (as covered in zyLab 2.38). Note that we need to allow for a trapezoids angled both upwards and downwards (e.g., y = 1 + 2x + 3x2 curves upward while y = 1 + 2x - 3x2 curves downward as x increases).
After computing the area of the trapezoid for each third of the interval, they can be summed to produce the approximate area under the polynomial within that interval:
The total approximate area for this polynomial between x=0.1 and x=3 is 40.244 (when not limited by the rounding shown in the figure above).
Write your program in using VS Code. Your program should ask the user for each of the coefficients in the polynomial (i.e., in a + bx + cx2, the coefficients are a, b, and c) and the beginning and end of the interval. The program should compute the approximate area under the polynomial within that interval using three non-overlapping trapezoids of equal width, then output the polynomial equation, interval, and area approximate area. For example, in the example above, the output might look like this:
Approximate area under y = 1 + 2x + 3x^2 between x = [0.1, 3] is 40.244
Complete the following header and specifications checklist and include it at the top of your source code. Before you submit, check off each of the specifications that you satisfied (note: you need to meet them all to get pass the assignment, so don't submit if you cannot check one of them off).
// Name:
// Date:
// Partner:
//
// Specifications checklist for PA Option 2.1
// [ ] your header includes your name and anyone you worked with
// [ ] your header includes this specifications checklist
// [ ] your code in indented properly (use the autoformatter if you're not sure)
// [ ] your identifiers are well named
// [ ] your program compiles
// [ ] your program runs without crashing
// [ ] all output and prompts look clean (correct spelling, capitalization, nothing squished)
// [ ] your program prompts and reads from the user the following information:
// [ ] a, b, c in the polynomial y = a + bx + cx^2 under which to approximate the area
// [ ] the starting and ending interval of x to calculate the area between
// [ ] your program approximates the area under the polynomial within the given interval using three trapezoids, each a non-overlapping third of the interval
// [ ] your program outputs the approximate area at the end with a label (e.g., "Approximate area under y = 1 + 2x + 3x^2 between x = [0.1, 3] is 40.244")
// [ ] your program is accurate -- your program's approximations match the expect results
Here are the expected results for a number of polynomials:
Polynomial | Interval start | Interval end | Approximate area under |
1 + 2x + 3x2 | 0.1 | 3 | 40.2439 |
1 + 2x + 3x2 | 0 | 10 | 1165.56 |
5 - x + 2x2 (note: the second coefficient is -1) |
0 | 5 | 100.463 |
1 + x + x2 | 0 | 0.5 | 0.668981 |
3 + 4x - 0.1x2 | 10 | 30 | 778.519 |
Submit your PA to the First programming assignment. If you are working with a partner, both of you must submit individually.
Rubric
Criteria | Ratings | ||
---|---|---|---|
Worked diligently on the lab problems
|
|
||
Submitted all materials according to the instructions
|
|
||
|