Schedule‎ > ‎

# A2-Math and Memory

### Objectives

• Start practicing C++ commands
• Start declaring and using numerical variables
• Obtain and store user input
• Perform arithmetic operations to solve problems using C++.
• Make use of modulus operators
• Work with mathematical functions.
• Debug errors in your code
• Write your first complete C++ programs!

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

• You may not show your completed code to another person or look at another person's code until you complete and submit this assignment.
• You may get help from others if you get stuck, but only if they do not show or tell you what to type.
• Remember that the instructor performs similarity tests on programming project submissions, and copied or plagiarized code is usually very easy to detect.

## Project Specifications

Your solutions to these project must only use techniques we have covered so far.

#### Programming Style

For all programs, remember to follow all the style rules we covered including:

2. Placement of curly braces and indentation within curly braces.
3. Limiting line length to about 80 characters.

TextPad, and most other text editors, tell you both the line and column location, which lets you check the line length.

4. Spaces before and after operators.

### Project 1: Math Worksheet

For this worksheet, the user enters three numbers. You write code to display the value of the equations listed below.

Project Specifications

1. Start by copying the following code & saving it as mathwork.cpp in your cygwin home folder.
2. ``````/*
CS-11 Asn 2, mathwork.cpp
Purpose: Improve math and variable skills.

@version 1.0 Date Completed
*/
#include <cmath>
#include <iostream>
using namespace std;

int main()
{
// Do NOT change the next few lines
// Read 3 numbers from stdin and print their values to stdout.
cout << "***Math Worksheet***\n";
double a = 0.0, b = 0.0, c = 0.0, x = 0.0;
cout << "Enter three numbers separated by spaces, and press the Enter key";
cout << "\nFor example: 1 4 2[Enter]: ";
cin >> a >> b >> c;
cout << "\nYou entered: a = " << a << ", b = " << b << ", c = " << c
<< "\n\n";
// Make changes after this line

x = 0; // change this line for expression 1
cout << "Expression1 (7): " << x << endl; // do not change line

x = 0; // change line for expression 2
cout << "Expression2 (-17.2222): " << x << endl; // do not change line

x = 0; // change line for expression 3
cout << "Expression3 (9): " << x << endl; // do not change line

x = 0; // change line for expression 4
cout << "Expression4 (5): " << x << endl; // do not change line

x = 0; // change for expression 5
cout << "Expression5 (-0.585786): " << x << endl; // do not change line

return 0;
}``````
3. Add to the existing code to complete the project. Leave the existing code unchanged, except for comments.

4. You must name the source code file that you turn in `mathwork.cpp` and include all your code in this single file.

Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

5. Add your name and the date to the file comment block at the top of the file where shown in the comments.
6. User input is already coded into the worksheet.

Do not add any other input commands or change the input order.

7. Convert each of the following equations to C++ code. The letter xx  represents the expression (put this on the left side of equal sign).  You will need to convert the math functions to C++ math functions as in 2.3.5 in the Lesson 2 Lecture Notes.  Example:  (a + 2)(3) becomes (a + 2) * 3;  Don't forget to add a semicolon at the end of each formula in your source file.
1. Summation means to add up a sequence of numbers. For expression 1, use the following formula to the sum all the inputs

x = a + b + c        Where aabb and cc are the user input.

Code this equation in the `mathwork.cpp` program where indicated in the comments. See the Example Run to verify correctness

2. For expression 2, calculate the degrees in Celsius using the following formula, assuming the first value entered by the user, aa, is the degrees in Fahrenheit. Code this equation in the `mathwork.cpp` program where indicated in the comments. See the Example Run to verify correctness.

Celsius = 5/9 * (Fahrenheit - 32)   /*  You will need to declare two double variables Celsius and Fahrenheit.  Be sure to initialize them to 0.0.  BEFORE this formula,  set Fahrenheit = a;  After the formula,  set x = Celsius;  */

3. For expression 3, calculate the value using the following formula.

x = (a  - b)(a - b)    //   Where aa and bb are the user input.

Code this equation in the `mathwork.cpp` program where indicated in the comments. See the Example Run to verify correctness.

4. For expression 4, calculate the value using the following formula.

x = a * (1 - b^c)/(1 - b)      //Where aabb and cc are the user input.

Code this equation in the `mathwork.cpp` program where indicated in the comments. See the Example Run to verify correctness.

5. For expression 5, calculate the value of the quadratic formula (positive value only) shown below.

x = (-b + sqrt(b^2 - 4ac)) / (2a)       //Where aabb and cc are the user input.

6. Code this equation in the `mathwork.cpp` program where indicated in the comments. See the Example Run to verify correctness.

8. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
```***Math Worksheet***
Enter three numbers separated by spaces, and press the Enter key
For example: 1 4 2[Enter]: 1 4 2

You entered: a = 1, b = 4, c = 2

Expression1 (7): 7
Expression2 (-17.2222): -17.2222
Expression3 (9): 9
Expression4 (5): 5
Expression5 (-0.585786): -0.585786
```
```***Math Worksheet***
Enter three numbers separated by spaces, and press the Enter key
For example: 1 4 2[Enter]:  10 20 1

You entered: a = 10, b = 20, c = 1

Expression1 (7): 31
Expression2 (-17.2222): -12.2222
Expression3 (9): 100
Expression4 (5): 10
Expression5 (-0.585786): -0.0513167
```

In the above example runs, the user entered the values shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics. The numbers in parenthesis show the result using the example values.

9. Display the output using the default formatting and precision for the numbers -- do NOT add any formatting statements to the code.
10. After displaying the output, exit the program.
11. Submit the source code file `mathwork.cpp` with the rest of Assignment 2 in Canvas.

### Project 2: Distances in Whole Numbers

In the United States we use the system of measurement known as the United States customary units [1]. These units were developed from English units as used in the British Empire before the U.S. became an independent country.

Customary units are primarily used in commercial activities, as well as for personal and social use. Metric units are most often used in science and engineering, though Mechanical Engineering still often uses both customary and metric units.

In this project we convert from integer inches to miles, feet, yards and inches all expressed as whole numbers.

##### Project Specifications
1. Write a program that asks the user for an input in inches and displays the same distance as integer miles, feet, yards and inches. The miles, yards, feet and inches must collectively add up to the same distance.

For example 37 inches is: 0 miles, 1 yard, 0 feet and 1 inch.

2. Name the source code file `distances.cpp` and include all your code in this single file.

Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

3. Ask the user for the number of inches, and no other input, as shown in the Example Run. Assume the user enters only whole numbers.
4. Display all numbers as whole numbers with no decimal or fractional parts.
5. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
```Enter number of inches: 37
In whole numbers: 0 miles, 1 yards, 0 feet, and 1 inches.
```
```Enter number of inches: 1234567
In whole numbers: 19 miles, 853 yards, 1 feet, and 7 inches.
```

In the above example runs, the user entered the values shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics. The output must include labels after the number as shown.

6. The plurality of the labels may not match the numbers, which is fine.  This means 1 inches instead of 1 inch is ok.

We will learn how to correct this problem when we learn `if`-statements. However, do not use `if`-statements, or any conditional statements, in this program

7. After displaying the output, exit the program.
8. Submit the source code file `distances.cpp` with the rest of the assignment in Canvas.
Hints:
• After converting to a unit like miles, take the remainder for conversion of the next lower unit. See lesson 2.3.4 for an example.

### Project 3: Mowing Quotes

Your friend is starting a gardening business to mow lawns as a way to help pay for living expenses while going to school. He needs a good way of estimating how much to charge for his services. Here is how he has decided to charge for his work:

• He's going to charge \$12.75 for travel time, no matter how much time it takes him to get to the house.
• He's going to charge \$25.00 per hour he spends cutting the lawn; and his estimates will be based on cutting 2500 square feet (SF) of lawn per hour.
• He has decided he will round up the estimated number of hours for a job. For example, if someone has a 30 feet by 90 feet lawn (2700 SF), he'll estimate that it's a 2 hour job; but a 2500 SF lawn would only be a 1 hour job.
• Because he's starting out, he's going to give a 10% discount to all his clients. This discount will apply to both travel time, and to the cost of cutting the lawn.
• Finally, he needs to charge tax, which is currently 15.3%  Tax is applied after the 10% discount is given to the sum of travel cost and mowing cost.
##### Project Specifications
1. Write a program to calculate and print a mowing quote for a specific lawn.
2. Name the source code file `mowing.cpp` and include all your code in this single file.

Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

3. Ask the user for the following inputs (and no other input) in this order as shown in the Example Run below:
1. width
2. height

Assume the user enters only valid numbers.

4. Numbers may not display with exactly two decimal places, which is fine. Display the output using the default formatting and precision for the numbers - do NOT add any formatting statements to the code.
5. Display the number of hours for mowing before the estimate as shown in the Example Run below.
6. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
```Enter the size of the lawn (width x height) in feet
47.5 67
To cut a lawn 47.5 by 67 will take 2 hours.

Estimate:
=========
Cutting (2H)    \$50
Travel Cost     \$12.75
--------
Subtotal        \$62.75
Discount (10%)  - \$6.275
Tax (15.3%)     + \$8.64067
========
Total Owing:    \$65.1157
```

In the above example run, the user entered the values shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics. Remember to include the \$ (dollar sign) characters.

7. The output must line up nicely as shown in the Example Run.
8. After displaying the output, exit the program.
9. Submit the source code file `mowing.cpp` with the rest of the assignment 2 in Canvas.
Hints:
• Calculating a tax of 15.3% is the same as multiplying by 0.153
• Print `"\t"` (inside quotes) or "\t\t-------" before the gap shown in the Example Run to line up the output.
• Use the mathematical ceiling function
1. Independent Contractors Must Pay Self-Employment Taxes: for Social Security and Medicare

## Extra Credit

Completing the following for extra credit:

1. Add an additional interesting and useful math equation derived from `mathworks.cpp` making use of all three of the variables and a math function. (1 point for completing with a math function and 1 point for interest and usefulness.)
1. Submit the source code with the extra math equation in a file named `xcmathwork.cpp`.
2. Describe what the function does in a comment beside the equation.
3. Follow the format of the previous equations by assigning the solution to the variable xx.
4. Follow the output of the previous equations by labeling the equation with a `cout` statement displaying the label `Equation6` followed by the value assigned to (xx) when entering the example values `1 4 2` and then the equation result itself, like:
5. `Equation6 (42): 42`
`Equation6 (42): 210`
2. Look up constant variables in the textbook (p.39) and declare and use constants integers in `distances.cpp` for all the following values: (1 point) Submit this in a file called xcdistances.cpp
1. Inches per mile
2. Inches per yard
3. Inches per foot

This extra credit will require three (3) constant variables minimum, one for each value. Your program must use the constant variables in place of their numbers in the equations.

3. Look up constant variables in the textbook (p.39) and declare and use constant variables in `mowing.cpp` for all the following values: (1 point) Submit this in a file called xcmowing.cpp
1. travel cost
2. square feet of lawn cut per hour
3. cost per hour
4. discount rate
5. tax rate

This extra credit will require five (5) constant variables minimum, one for each value. Your program must use the constant variables in place of their numbers in the equations.

## Grading Criteria (30 pts + 4 pts extra credit)

For each part:  10 pts: Header Comment 1pt, Compiles 2 pts, Proper Formatting 1 pt,  Works as Specified 6 pts
1. `mathwork.cpp (10pts)`
2. `distances.cpp (10pts)`
3. `mowing.cpp (10pts)`
4. Optionally, `xcmathwork.cpp, xcdistances.cpp, xcmowing.cpp ` (extra credit 4pts)

Note: Make certain your programs compile before you turn them in. When a program does not compile then it does not function either. For all programming projects, you should expect little or no credit if your program does not compile and run.

You must submit all the files needed to complete your assignment together. Your assignment must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline. If you resubmit, you must include all your assignment files in the last submission as Canvas hides prior submissions.

## Project 1:  mathwork.cpp

/*
CS-11 Asn 2, mathwork.cpp
Purpose: Improve math and variable skills.

@author Sharon Strelitz
@version 1.0 1/28/2018
*/
#include <cmath>
#include <iostream>
using namespace std;

int main()
{
// Do NOT change the next few lines
// Read 3 numbers from stdin and print their values to stdout.
cout << "***Math Worksheet***\n";
double a = 0.0, b = 0.0, c = 0.0, x = 0.0;
double Celsius = 0.0, Fahrenheit = 0.0;
cout << "Enter three numbers separated by spaces, and press the Enter key";
cout << "\nFor example: 1 4 2[Enter]: ";
cin >> a >> b >> c;
cout << "\nYou entered: a = " << a << ", b = " << b << ", c = " << c
<< "\n\n";
// Make changes after this line

x = a + b + c; // change this line for expression 1
cout << "Expression1 (7): " << x << endl; // do not change line
Fahrenheit = a;
Celsius =  5.0/9.0 * (Fahrenheit - 32); // change line for expression 2
x = Celsius;
cout << "Expression2 (-17.2222): " << x << endl; // do not change line

x = (a - b) * (a - b); // change line for expression 3
cout << "Expression3 (9): " << x << endl; // do not change line

x = a * (1 - pow(b,c)) / (1 - b); // change line for expression 4
cout << "Expression4 (5): " << x << endl; // do not change line

x =  (-b + sqrt(pow(b,2) - 4 * a * c)) / (2 * a);; // change for expression 5
cout << "Expression5 (-0.585786): " << x << endl; // do not change line

return 0;
}

## Project 2:  distances.cpp

`/*`
`    CS-11 Asn 2, distances.cpp`
`    Purpose: Converts user input of inches to miles+yards+feet+inches.`

`    @author Sharon Strelitz`
`    @version 1.0 1/28/2018`
`*/`
`#include <cmath>`
`#include <iostream>`
`using namespace std;`

`int main()  {`
`    int inches = 0, feet = 0, yards = 0, miles = 0, remainder = 0;`
`    cout << "Enter number of inches: ";`
`    cin >> inches;`
`    cout << endl << "You entered " << inches << " inches." << endl;`
`    miles = inches / 63360;`
`    remainder = inches % 63360;`
`    yards = remainder / 36;`
`    remainder = remainder % 36;`
`    feet = remainder / 12;`
`    remainder = remainder%12;`
`    cout << "In whole numbers: " << miles << " miles, " << yards << " yards, " `
`       << feet << " feet, " << remainder << " inches." << endl;`
`}`

## `Project 3:  mowing.cpp`

/*
CS-11 Asn 2, Project 3 mowing.cpp
Purpose: Produce quotes for a mowing business.

@author Sharon Strelitz
@version 1.0 1/28/2018
*/
#include <cmath>
#include <iostream>
using namespace std;

int main()  {
double width = 0.0, height = 0.0, squareFeet = 0.0;
double cutCost = 0.0, travelCost = 0.0;
double discount = 0.0, tax = 0.0, subtotal = 0.0;
int hours = 0;
cout << "Enter the size of the lawn (width x height) in feet";
cout << "\nFor example: 47.5 67 " << endl;
cin >> width >> height;
squareFeet = width * height;
hours = ceil(squareFeet/2500);
cout << "To cut a lawn " << width << " by " << height << height
<< " will take " << hours << " hours" << endl << endl;
cutCost = 25 * hours;
cout << "Cutting (" << hours << "H)\t\$" << cutCost << endl;
travelCost = 12.75;
cout << "Travel Cost\t\$" << travelCost << endl;
cout << "\t\t-------" << endl;
subtotal = cutCost + travelCost;
cout << "Subtotal\t\$" << subtotal << endl;
discount = .10 * (cutCost + travelCost);
cout << "Discount (10%)\t- \$" << discount << endl;
tax = .153 * (subtotal - discount);
cout << "Tax (15.3%)\t+ \$" << tax << endl;
cout << "\t\t-------" << endl;
cout << "\t\t-------" << endl;
cout << "Total Owing:\t\$" << subtotal - discount + tax << endl;
}

Subpages (1):