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

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.

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.

Ask the user for the following inputs (and no other input) in this order, as shown in the Example Run below:

age in years

habit cost per day

Assume the user enters valid integers for age and decimal numbers for habit cost per day.

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.

Requirements about the computation here.

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.

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.

Assume the user is no older than age 75, and that the user enters correct data.

After displaying the output, exit the program.

Submit the source code file habits.cpp with the rest of the assignment as described in Deliverables.

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

Write a program that calculates elapsed time based on a 12 hour clock.

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.

Ask the user for the following inputs (and no other input) in this order, as shown in the Example Run below:

Current clock hours

Current clock minutes

Elapsed clock hours

Elapsed clock minutes

Assume the user enters valid numbers.

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.

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.

After displaying the output, exit the program.

Submit the source code file clock.cpp with the rest of the assignment as described in Deliverables.

Project Specifications

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

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.

Add your name and the date to the file comment block at the top of the file where shown in the comments.

User input is already coded into the worksheet.

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

Convert each of the following equations to C++ code in the worksheet.

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.

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.

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:

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.

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.

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.

Display the output using the default formatting and precision for the numbers -- do NOT add any formatting statements to the code.

After displaying the output, exit the program.

Submit the source code file interest.cpp with the rest of the assignment as described in Deliverables.

Extra Credit

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:

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

Project Specifications

Write a program that inputs a binary number between 0 and 1111 and outputs the decimal equivalent.

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.

Ask the user for a binary number between 0 and 1111, as shown in the Example Runs below.

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.

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.

After displaying the output, exit the program.

Remember that your solution must use only the techniques we have covered so far, so loops are NOT allowed.

Submit the source code file nibble2dec.cpp with the rest of the assignment as described in How To Submit.

References and More Information

Tutorial Lab

In preparation for next weeks lessons, complete the following:

Read the assigned reading in the textbook

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.

Grading Criteria

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.

README.txt File

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:

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.