CS16: Programming Assignment 04

Introduction

The assignment for this week will utilize functions.

This assignment is due on July 15th at 23:59.

Top

Step 1: Open a Terminal

The first step in every assignment will be to open a terminal window, which will be the environment you use to write, compile, and run your programs.

For a reminder on how to do this step, take a look at Step 2 of PA 01.

Top

Step 2: Programming Assignment 04 Directory

Start by changing into your CS 16 directory:

$ cd cs16

And then create and move into a PA 04 directory:

$ mkdir pa04
$ cd pa04

Remember that at any time, you can check what directory you are current in with the command pwd.

Top

Step 3: Create Your C++ Files

This week, you will need to create a three files called inflation.cpp, mortgage.cpp, and change.cpp:

$ touch inflation.cpp mortgage.cpp change.cpp
Top

Step 4: Edit Your C++ Files

You will now need to complete each source file. Each corresponds to one of the problems listed below, which make up this lab.

For a reminder on how to open the text editor and how edit files, take a look at Step 5 of PA 01.

Top

Step 5: Write the Code

This assignment consists of three problems, each of which is described below. The first and second problems are worth 30 points each, and the last one is worth 40 points. Each should be solved in its own file and all three must be submitted for full assignment credit.

Chapter 4, Practice Program 4

This should be solved in the inflation.cpp file.

Write a program to gauge the rate of inflation for the past year. The program asks for the price of an item (such as a hot dog or a 1-carat diamond) both one year ago and today. It estimates the inflation rate as the difference in price divided by the year-ago price. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the rate of inflation. The inflation rate should be a value of type double giving the rate as a percent, for example 5.3 for 5.3 percent.

Your program must use a function to compute the rate of inflation. A program which does not use a function will be awarded a score of zero, even if all tests pass.

The program should print a string of text to the terminal before getting each piece of input from the user. A session should look like the following example (including whitespace and formatting), with possibly different inputs and numbers in the output:

Enter the item price one year ago (or zero to quit):
54.10
Enter the item price today:
61.50
The inflation rate is 13.68 percent.
Enter the item price one year ago (or zero to quit):
16.16
Enter the item price today:
16.16
The inflation rate is 0.00 percent.
Enter the item price one year ago (or zero to quit):
0

Each string printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).

The inflation rate must be displayed to exactly two digits after the decimal point.

Chapter 4, Programming Project 1

This should be solved in the mortgage.cpp file.

Write a program that computes the annual after-tax cost of a new house for the first year of ownership. The cost is computed as the annual mortgage cost minus the tax savings. The input should be the price of the house and the down payment. Inputs should be of type double. The annual mortgage cost can be estimated as 3 percent of the initial loan balance credited toward paying off the loan principal plus 6 percent of the initial loan balance in interest. The initial loan balance is the price minus the down payment. Assume a 35 percent marginal tax rate and assume that interest payments are tax deductible. So, the tax savings is 35 percent of the interest payment. Your program should use at least two function definitions (other than main function) and should allow the user to repeat this calculation as often as the user wishes.

Your program must use at least two functions (excluding main). A program which does not use functions will be awarded a score of zero, even if all tests pass.

The program should print a string of text to the terminal before getting each piece of input from the user. A session should look like the following example (including whitespace and formatting), with possibly different inputs and numbers in the output:

Enter the home price (or zero to quit):
1200000
Enter the down payment:
500000
The after-tax cost is $48300.00 annually.
Enter the home price (or zero to quit):
160000
Enter the down payment:
160000
The after-tax cost is $0.00 annually.
Enter home price (or zero to quit):
0

Each string printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).

The cost must be displayed to exactly two digits after the decimal point.

Chapter 5, Programming Project 4

This should be solved in the change.cpp file.

Write a program that tells what coins to give out for any amount of change from 1 cent to 99 cents. For example, if the amount is 86 cents, the output would be something like the following:

86 cents can be given as 3 quarters, 1 dime, 1 penny.

Use coin denominations of 25 cents (quarters), 10 cents (dimes), and 1 cent (pennies). Do not use nickel and half-dollar coins. Your program will use the following function (among others):

void compute_coins(int coin_value, int& num, int& amount_left);
// Precondition: 0 < coin_value < 100; 0 <= amount_left < 100.
// Postcondition: num has been set equal to the maximum number
// of coins of denomination coin_value cents that can be obtained
// from amount_left. Additionally, amount_left has been decreased
// by the value of the coins, that is, decreased by
// num * coin_value.

For example, suppose the value of the variable amount_left is 86. Then, after the following call, the value of number will be 3 and the value of amount_left will be 11 (because if you take 3 quarters from 86 cents, that leaves 11 cents):

compute_coins(25, number, amount_left);

Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program.

Your program must use the compute_coins function declaration shown above. A program which does not will be awarded a score of zero, even if all tests pass.

The program should print a string of text to the terminal before getting each piece of input from the user. A session should look like the following example (including whitespace and formatting), with possibly different inputs and numbers in the output:

Enter number of cents (or zero to quit):
86
86 cents can be given as 3 quarters, 1 dime, 1 penny.
Enter number of cents (or zero to quit):
35
35 cents can be given as 1 quarter, 1 dime.
Enter number of cents (or zero to quit):
99
99 cents can be given as 3 quarters, 2 dimes, 4 pennies.
Enter number of cents (or zero to quit):
0

Each string printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).

Top

Step 6: Compile the Code

To compile our code, we will use the same g++ command as last week. The following three commands will compile the three source files (in the same order as listed above):

$ g++ -std=c++11 -o inflation inflation.cpp
$ g++ -std=c++11 -o mortgage mortgage.cpp
$ g++ -std=c++11 -o change change.cpp

If the compilation is successful, you won't see any output from the compiler. You can then use the following commands to run your programs:

$ ./inflation
$ ./mortgage
$ ./change

If you encounter an error, use the compiler hints and examine the line in question. If the compiler messsage is not sufficient to identify the error, you can search online to see when the error occurs in general.

Remember to re-compile the relevant files after you make any changes to the C++ code.

Top

Step 7: Submit

Once you are satisfied that your program is correct, it is time to submit it. Login at https://submit.cs.ucsb.edu/session, then navigate to “CS16_m16” and click on “Programming Assignment 4”. Then click “Make Submission”, and make your submission the same way as last week. Remember to submit all three .cpp files.

Please remember that you must submit the program to obtain any credit for the assignment; just completing the program is not enough.

Once you submit, you should see a page detailing your submission. The system will automatically grade your program and will show you the results on this page after a 1 minute delay.

You can alternatively submit your code from the command line (terminal) on any CS machine, including the Phelps lab machines or the CSIL server. You can use this method when logged in remotely. To submit the the three source files to this assignment by running the command:

$ ~submit/submit -p 502 inflation.cpp mortgage.cpp change.cpp

You can copy the URL shown in the output of the above and paste into a web browser to reach the submission result page.

Top

Step 8: Check Submission Results

After the 1 minute delay, the submit system will show your score and give you feedback on your submission. Refresh the webpage after a minute to see this information.

You may submit multiple times. You should submit only after local compilation does not produce any errors and runs as expected. The score of the last submission uploaded before the deadline will be used as your assignment grade.

Top

Step 9: Done!

Once your submission receives a score of 100/100, you are done with this assignment.

If you are in the Phelps lab or in CSIL, make sure to log out of the machine before you leave. Also, make sure to close all open programs before you log out. Some programs will not work next time if they are not closed. Remember to save all your open files before you close your text editor.

If you are logged in remotely, you can log out using the exit command:

$ exit