# 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 project work to another person or look at another person's project work 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 very easy to detect.

This is a solo assignment.

## Preparation

1. Make sure you have completed the exercises from lesson 2.
2. Complete the Review Exercises in CodeLab 2. These exercises will help prepare you for the problem-solving program and should be completed first.

## Project Specifications

Note that these are solo programming projects and not pair-programming projects. 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

Use this worksheet to improve your understanding of how to convert math equations to C++ code. Refer to lesson 2 for more information on C++ maths.

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

##### Project Specifications

Add to the existing code to complete the project. Leave the existing code unchanged, except for comments.

2. 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.

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

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

5. Convert each of the following equations to C++ code in the worksheet.
1. Summation means to add up a sequence of numbers. For expression 1, use the following formula to the sum all the inputs.

Where a, b, c and d are the user input already coded into the worksheet.

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

2. Weights are used to give some elements of an equation more influence. For expression 2, calculate the weighted sum using the following formula.

Where a, b, c and d are the user input and the weights are predefined in the starter code.

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

3. Miles per gallon (mpg) is the measurement of the distance in miles that a vehicle can travel using only one gallon of fuel. For expression 3, calculate the mpg of the input using the following formula.

Where b is the gallons of fuel consumed and d is the distance traveled from the user input already coded into the worksheet.

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

4. The trip cost for a car is the distance traveled divided by the miles per gallon (mpg) times the cost per gallon. For expression 4, calculate the trip cost using the following formula.

Where b is the gallons of fuel consumed, c is the cost per gallon, and d is the distance traveled from the user input already coded into the worksheet.

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

5. The distance formula is used to determine the distance between two points. For expression 5, calculate the distance (dist) using the following formula.

Where a, b are x-coordinates and c, d are y-coordinates from the user input.

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

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 exact 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 four numbers separated by spaces, and press the Enter key
For example: 4 3 2 1[Enter]: 4 3 2 1

You entered: a = 4, b = 3, c = 2, d = 1
Parenthesis show correct results with example numbers 4 3 2 1.
Expression1 (10): 10
Expression2 (32): 32
Expression3 (0.333333): 0.333333
Expression4 (6): 6
Expression5 (1.41421): 1.41421

***Math Worksheet***
Enter four numbers separated by spaces, and press the Enter key
For example: 4 3 2 1[Enter]: 5 2.5 2.87 100

You entered: a = 5, b = 2.5, c = 2.87, d = 100
Parenthesis show correct results with example numbers 4 3 2 1.
Expression1 (9): 110.37
Expression2 (32): 135.37
Expression3 (0.333333): 40
Expression4 (6): 7.175
Expression5 (1.41421): 97.1622


In the above example runs, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics. The values in (parentheses) are expected values when entering the example input of  4 3 2 1 .

7. Display the output using the default formatting and precision for the numbers -- do NOT add any formatting statements to the code.
8. After displaying the output, exit the program.
9. Submit the source code file mathwork.cpp with the rest of the assignment as described in Deliverables.

#### Project 2: Composing Student ID's

Cabrillo issues every student a unique seven-digit Student Identification Number (Student ID). Cabrillo uses these numbers during registration and other processes, as well as printing the number on Student Activity Cards.

For this project, you will generate and print your unique Student ID number derived from a starting integer number. You will need to use integer division and modulus to extract each digit from the starting number and arrange the digits in the correct order.

##### Getting Started

To get started, review the example in lesson 2.3.4 in the section: Program using Modulus to "Split" Digits. Try coding the same process using the starter code in the project. For example, if we print SEED % 10 your program will display a 9.

cout << SEED % 10; // last SEED digit


If we print SEED / 10 we see everything but the last digit: 134265870

cout << SEED / 10; // everything but the last digit


Then notice that 134265870 % 10 will yield a 0. So to get 0 we use:

cout << SEED / 10 % 10; // second from last digit


If we keep going we can get any number from the SEED variable with a combination of / and % operations. Then just print the digits in the correct order.

##### Project Specifications
1. Write a program that prints your unique seven-digit Student ID number by extracting it from the SEED number in the starter code: sidmod.cpp
2. Name the source code file sidmod.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. Do NOT ask the user for any input in this project.
4. Use the division operator (/) and remainder operator (%) to extract digits from the SEED variable and then print the digits to the console using cout.

At a minimum, your code must include 6 division operations and 6 modulus operations to extract the digits form SEED. Do not use any other arithmetic operators. Do NOT directly print your Student ID or any literal number.

5. Example Run: The outputs of the program must look like the following for full credit. Your program must display your unique Student ID number.
My student id: 0123456


In the above example run, the programmer's Student ID is 0123456. Your number will be different.

6. Do NOT change the original SEED statement in any way. Your code must use the SEED number exactly as provided.
7. Do not use if-statements, strings or techniques we have not covered.
8. After displaying the output, exit the program.
9. Submit this project with the rest of the assignment as described in Deliverables.

#### Project 3: Gas Pains

In this project we compare the cost of traveling 100 miles in both a gas-powered and an electric car. In addition, for extra credit, we look at the greenhouse gas emissions of both forms of travel.

Before starting to code, develop an understanding and plan as we discussed in lesson 2.4. You must include both your problem restatement and your plan's algorithm within your C++ source code file (see specification 2). Use the following information to develop your plan.

##### Background Information

To calculate the cost of gas for a mile, we divide the cost-per-gallon (CPG) of gas by the miles-per-gallon (MPG) rate of the car.

Once we know the cost-per-mile, we multiply by the number of miles we travel.

Electric cars are rated by the EPA using miles per gallon gasoline equivalent (MPGe). For electric vehicles, the EPA has determined that 33.7 kilowatt hours (kWh) of electricity is energy-equivalent to one gallon of gasoline. Thus the cost-per-gallon equivalent (CPGe) for electric cars is:

After the above calculation, we use the same formula as for calculating the cost of traveling in a gas-powered car, substituting CPGe for CPG and MPGe for MPG. The cost per kWh of electricity from PG&E is as low as $0.12225 [1], or even lower with solar panels. ##### Project Specifications 1. Write a program to calculate the cost of driving 100 miles in a gas-powered car versus the same distance in an electric car. 2. The name of the source code file for this program must be carcost.cpp and all your code must be in this file. Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming. When starting your coding effort, restate the problem in the file comment at the top of the file and copy your algorithm into the main function as we discussed in lesson 2.4.4. 3. Ask the user for the following inputs (and no other input) in this order, as shown in the Example Run below: 1. Efficiency of the gas powered car in miles per gallon (MPG) 2. Price of a gallon of gas 3. Efficiency of the electric car in miles per gallon equivalent (MPGe) 4. Price of a kilowatt-hour (kWh) of electricity 4. Use the default formatting and precision for the numbers. Ignore significant figures at this time and do NOT add any formatting statements to the code. 5. Example Run: The input prompts and outputs of the program must look like the following, including the same order of input and wording of the output, for you to receive full credit. For the input shown you must get the same output. However, the output numbers must change if the inputs are different. Gas vs. Electric Car Travel Comparison Enter the fuel efficiency of the car in MPG: 24.7 Enter the cost of one gallon of gas: 2.89 Cost of traveling 100 miles in a gas car is:$11.7004

Enter the energy efficiency of electric car in MPGe: 112
Enter the cost of one KWH of electricity: .12503
Cost of traveling 100 miles in an electric car is: $3.76206  Gas vs. Electric Car Travel Comparison Enter the fuel efficiency of the car in MPG: 50 Enter the cost of one gallon of gas: 2.97 Cost of traveling 100 miles in a gas car is:$5.94

Enter the energy efficiency of electric car in MPGe: 123
Enter the cost of one KWH of electricity: .12225
Cost of traveling 100 miles in an electric car is: $3.34945  In the above example runs, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics. Note that the MPGe of 112 is for a Nissan Leaf and the 123 is for a Tesla Model 3. 6. After displaying the output, exit the program. 7. Submit this project with the rest of the assignment as described in Deliverables. Hints: • Get started by working though the steps by hand for calculating the cost of traveling 100 miles in gas powered car. Once you understand those steps, write them down so you can translate them to C++. • Once you understand the gas powered car, calculating the electric powered car cost is similar. Review how to calculate CPGe from the project Background Information, and then adapt the cost of traveling in a gas powered car to the cost of the electric powered car. ##### References and More Information 1. Electric Schedule EV:$0.12503 effective March 1, 2017 (retrieved 01/10/2019)
2. How much carbon dioxide is produced by burning gasoline and diesel fuel?: 19.64 lbs CO2 per gallon
3. How can a gallon of gasoline produce 20 pounds of carbon dioxide?: by combining with the oxygen in the air.
4. California Electricity Profile 2017: shows 0.474 lbs per kWh in 2017 (latest available), retrieved 1/10/2019.
5. All-Electric Vehicles: why electric vehicles are more efficient from the official U.S. government source for fuel economy information.

#### Project 4: Pair Programming Worksheet

Pair programming is where two programmers work together at one computer to develop code projects. One, the driver, writes code while the other, the observer or navigator,[1] reviews each line of code as it is typed in. The two programmers switch roles frequently [1]. Students generally have higher confidence in their work and perform better when pair programming [2]. These effects were tested at Cabrillo College among other places [3].

Programming can be beneficial, but following certain guidelines is important to maximize success [4][5]. This project introduces you to the concepts and best practices of pair programming for our course programming projects.

##### Project Specifications
1. Watch the video Introduction to Pair Programming, a professionally developed video (10 minutes).
2. Save this Pair Programming Worksheet following the menu File > Download as > Plain Text (.txt), and save the file as pairprogramming.txt.
3. Fill out and answer the questions in pairprogramming.txt without deleting any of the existing text.

Provide thoughtful answers for full credit.

4. Submit the pairprogramming.txt file with the rest of the assignment as described in Deliverables.
5. For reference, here is a link to the The Rules of Pair Programming
1. Pair programming: Wikipedia article
2. The effects of pair-programming on performance in an introductory programming course.
3. Pair Programming (Case Study 1)
4. The Rules of Pair Programming
5. All I Really Need to Know about Pair Programming I Learned in Kindergarten: Good suggestions on how to program in pairs.

## Extra Credit

Completing the following is worth the extra credit points shown in parenthesis.

1. Calculate the carbon emissions for both the gas and the electric car. Display the emissions after completing the cost analysis for both cars. (2 points)

For example, after entering 24.7 mpg, 2.89 per gallon, 112 mpge and .12503 per kWh:

Carbon emitted traveling 100 miles in gas car: 79.5142 pounds
Carbon emitted traveling 100 miles in electric car: 14.2623 pounds

After entering 50 mpg, 2.97 per gallon, 123 mpge and .12225 per kWh:
Carbon emitted traveling 100 miles in gas car: 39.28 pounds
Carbon emitted traveling 100 miles in electric car: 12.9868 pounds


To calculate the carbon emissions of a gas car, we first need to calculate the gallons of gas consumed to travel the distance, which is simply distance in miles / miles-per-gallon (MPG) of the car. Then we multiply the gallons consumed by the amount of carbon produced when consuming a gallon of gasoline.

The EPA says that burning each gallon of gas emits 19.64 pounds of CO2 [2]. This number does NOT include the carbon produced when extracting or refining the gasoline. One may wonder how a gallon of gasoline can produce almost 20 pounds of carbon dioxide. The answer is that the carbon atoms combine with the oxygen in the air to produce energy and CO2, increasing the weight of the carbon by 3.7 times [3].

To calculate the carbon emissions of an electric car, we first calculate the kWh consumed per mile, which is simply 33.7 / MPGe. Then we multiply by the amount of CO2 created to produce a kWh of electricity. California emits 0.474 lbs of CO2 per kWh generated [4], or 0 with solar panels.

2. Look up constant variables in the textbook (p.39) and declare and use constants integers in carcost.cpp for all numbers in equations. (1 point)

Look up magic numbers in the textbook (p.39) or on the internet to understand the solution to magic numbers.

3. Attend an SI session for this assignment and sign the roll sheet. (2 points)

Make sure to list the extra credit you complete in the README.txt file.

## Tutorial Lab

In preparation for next weeks lessons, complete the following:

2. Complete the Tutorial Exercises in CodeLab 2 before the specified due date.

Refer to the assigned reading for the next lesson to help you understand the problems. Also, you can use the online lecture notes for more information as the notes become available. You can look at solutions if you miss your first few attempts and are stuck by clicking the "Solution" tab.

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

#### Lesson Exercises

• 2: All lesson exercises attempted and turned in
• 1: Some lesson exercises completed and turned in
• 0: No lesson exercises completed or turned in

#### Programming Projects (x3)

• 4: Demonstrates mastery of the program
• Applies concepts from the lessons appropriately
• Meets all specifications (see above)
• Runs to completion with no abnormal error conditions
• Generates correct output given correct input
• Correct file name
• 3: Has most of the functionality expected of the program
• Demonstrates some techniques from the lesson
• Attempts to meet all but one of the specifications (see above)
• Implementation seems more complicated than necessary.
• May have one minor error
• 2: Has some of the functionality expected of the program
• Demonstrates some techniques from the lesson
• Attempts to meet at least 1/2 of the specifications (see above)
• Implementation seems excessively complicated.
• May have 2-3 minor errors
• 1: Serious functional problems but shows some effort and understanding
• Attempts to meet less than 1/2 of the of the specifications (see above)
• Has a major error or many minor errors
• Implementation seems very convoluted
• Demonstrates few techniques from the lesson
• 0: Not turned in or uses techniques not covered

#### Pair Programming Worksheet

• 4: Student completed and submitted the learning worksheet with masterful effort
• 3: Student completed and submitted the learning worksheet with substantial effort
• 2: Student completed and submitted the learning worksheet with minimal effort
• 1: Student submitted the learning worksheet
• 0: No learning worksheet submitted

#### Programming Style

• 2: Code is well-documented including:
• 1: Code has some documentation errors
• 0: No apparent attempt to follow documentation standards or write documentation comments

#### CodeLab Exercises

Number completed correctly / number exercises * 8 and rounded up to the nearest integer.

#### README.txt File

• 2: README.txt file submitted following the instructions
• 1: README.txt file submitted but some information was missing
• 0: No README.txt file submitted

Total possible: 30, plus extra credit

## Deliverables

Students submit some homework as they work on it like CodeLab. However, students must submit other homework in Canvas following the link to A2-Math and Memory. For detailed instructions see: How To Submit Homework Assignments. Include the following items when submitting to Canvas:

1. README.txt file prepared by following the instructions for submitting homework.
2. All the exercise files from Lesson 2
• hellome.cpp
• syntax.txt
• variables.cpp
• arithmetic.cpp
• plan.txt
• double.cpp
• errors.txt
• erroneous.cpp
3. mathwork.cpp
4. sidmod.cpp
5. carcost.cpp
6. pairprogramming.txt
7. Optionally, xcmathwork.cpp (extra credit)

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. For more information see the Grading Criteria.

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.

Last Updated: February 13 2019 @23:36:59