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

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

Image source (Modified)

#### Project 1: Costly Habits

One of the keys to financial well-being is to understand the impacts of our spending. We all have little habits, such as buying coffee or snacks every day, that add up over time. In this program we explore how much a habit will cost us, or a friend, by time we reach age 75.

##### Project Specifications
1. Write a program that calculates the cost of a daily habit by the time you reach age 75. Because the amounts may be large, we want to see the cost in both dollars and millions of dollars.

For example, $75,000 is 0.075 million dollars. 2. You must name the source code file habits.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. age in years 2. habit cost per day Assume the user enters valid integers for age and decimal numbers for habit cost per day. 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. Requirements about the computation here. 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. Current age (years): 19 Cost of daily habit: 10.75 This year your habit will cost you$3923.75
Between now and age 75, it will cost you \$219730!
Cost in millions of dollars is 0.21973!


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.

7. Ignore leap years, and ignore that the user may be part way through the year.

For example, if the user enters age 50, assume there are 25 full years until he or she reaches age 75.

8. Assume the user is no older than age 75, and that the user enters correct data.
9. After displaying the output, exit the program.
10. Submit the source code file habits.cpp with the rest of the assignment as described in Deliverables.

Image source: Wikipedia

#### Project 2: Calculating Elapsed Time

C++ allows us to perform modular arithmetic using integer division and the modulus operator. We make use of both these arithmetic operators in this assignment.

A familiar use of modular arithmetic is in the 12-hour clock, in which the day is divided into two 12-hour periods. If the time is 9:00 now, then 4 hours later the time will be 1:00. Thus hours are modulo 12 on a clock.

Similarly, there are 60 minutes in a clock hour. If the time is 12:34 now, then 33 minutes later the time is 1:07. Thus minutes are modulo 60 on a clock.

##### Project Specifications
1. Write a program that calculates elapsed time based on a 12 hour clock.
2. You must name the source code file clock.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. Current clock hours
2. Current clock minutes
3. Elapsed clock hours
4. Elapsed clock minutes

Assume the user enters valid numbers.

4. 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 clock hours: 8
Enter the clock minutes: 34
Current time is 8:34
Enter the elapsed hours: 5
Enter the elapsed minutes: 33
New time is 2:7


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.

Notice that a colon (:) separates the minutes and seconds. While a common convention is to precede minutes less that 10 with a zero (like 07 instead of 7), it is OK to not add a leading zero.

5. Perform the computations using integer division and modulus; no if-statements or other conditional statements are allowed.

Hint: To calculate hours, sum the hours and add the sum of the minutes divided by 60 to the hours. Remember that 67 / 60 is 1, so we add 1 to the hours. However, 57 / 60 is 0, so we add 0 to the hours. See lesson 2.3.4 Integer Division and Modulus for more information.

6. After displaying the output, exit the program.
7. Submit the source code file clock.cpp with the rest of the assignment as described in Deliverables.
1. Lesson 2.3.4: Integer Division and Modulus
2. Modular arithmetic: Wikipedia article

Image source (modified)

#### Project 3: Interest Equations

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.

Remember that to convert a percentage to a decimal we divide by 100. For example, 5% is 0.05 as a decimal.

##### 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 interest.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. When you borrow money from a bank, you must pay back an extra amount called interest. The equation for simple interest is:

Where:
P = principal
r = interest rate as a decimal number
t = term (number of time periods, usually expressed in years)

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

2. Compound interest is interest calculated on the initial principal and also on the accumulated interest of previous periods of a deposit or loan. In essence, compound interest is "interest on interest," and will make a sum grow at a faster rate than simple interest. The rate at which compound interest accrues depends on the frequency of compounding; the higher the number of compounding periods, the greater the compound interest.

Where:
P = principal
r = annual interest rate as a decimal number
n = number of compounding periods per year (or other time unit)
t = overall length of time the interest is applied (usually expressed in years)

Code this equation in the interest.cpp program where indicated in the comments. Be sure to use the pow() function. See the Example Run to verify correctness.

3. A third way of calculating interest is to do so continuously, known as continuous compounding. This is an extreme case as most interest is compounded on monthly, quarterly or semiannual basis. The equation for continuously compounded interest is:

Where:
P = principal
r = annual interest rate as a decimal number
e = the mathematical constant e
t = overall length of time the interest is applied (usually expressed in years)

Code this equation in the interest.cpp program where indicated in the comments. Be sure to use the exp() function. 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 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.
***Interest Worksheet***
Principal amount: 1000
Percent interest rate: 3
Length of term (years): 5
Number of compounding periods: 4

Simple interest is 150
Compounded interest is 161.184
Continuously compounded interest is 161.834


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.

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 interest.cpp with the rest of the assignment as described in Deliverables.
1. Compound Interest: from Investopedia.
2. Continuously Compounded Interest: from Math Warehouse

## Extra Credit

Completing the following is worth extra credit. (2 points)

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

#### Project 4: Nibbles of Numbers

Converting a binary number to a decimal number is important in understanding computers. In this project you will convert a binary (base 2) number into a decimal (base 10) number. The size of the binary number will be limited to four (4) binary digits, which is known as a nibble (or nybble to match the letters in byte) [1].

Here is an example of converting the binary number 11012 to the decimal number 1310:

1101 / 10 = 110 r 1 * 1 =  1
110 / 10 = 11  r 0 * 2 =  0
11 / 10 = 1   r 1 * 4 =  4
1 / 10 =  0  r 1 * 8 = +8
13


Notice that the remainders, multiplied by consecutive powers of 2, sum up to 1310 == 11012.

##### Project Specifications
1. Write a program that inputs a binary number between 0 and 1111 and outputs the decimal equivalent.
2. You must name the source code file nibble2dec.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 a binary number between 0 and 1111, as shown in the Example Runs below.
4. Convert the binary number into an integer decimal number using a series of division, modulus, and other mathematical operations as described in lesson 2.3.4. Your solution must use at least three division operators and four modulus operators. Do not use if-statements or other techniques we have not covered.
5. Example Runs: 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 if the input is different.
Enter a binary number between 0 and 1111: 10
The decimal equivalent is: 2

Enter a binary number between 0 and 1111: 1101
The decimal equivalent is: 13


In the above two example runs, the user entered "10" and "1101" (without the quotes) as the binary numbers to convert.

6. After displaying the output, exit the program.
7. Remember that your solution must use only the techniques we have covered so far, so loops are NOT allowed.
8. Submit the source code file nibble2dec.cpp with the rest of the assignment as described in How To Submit.
1. Nibble: Wikipedia, retrieved 2017-01-03.
2. How to Convert from Binary to Decimal: WikiHow

## 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

#### Projects (x3)

• 5: 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
• 4: 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
• 3: 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
• 2: 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
• 1: Does not compile or wrong file turned in
• 0: Not turned in or uses techniques not covered

#### Program Style

• 3: Code is well-documented including:
• 2: Code has a minor documentation error
• 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
• errors.txt
• erroneous.cpp
3. habits.cpp
4. clock.cpp
5. interest.cpp
6. Optionally, nibble2dec.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. 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: January 17 2018 @16:03:57