HW 3
Before describing your assignment, let us provide a few reminders.
-
Approximately 20% of your score is based on visual inspection of your code for style (indenting and comments). Be sure to follow the instructions on this style guide page to ensure you do not get points deducted.
-
The other 80% of your grade is based on the correctness as determined by mostly automated tests. These tests compare the output your program produces to expected output using a tool called:
diff
. It essentially does a character by character comparison of your output to the expected. That means that if you leave out a comma or capitalize a word you were not supposed to, our tests may fail and your score will be deducted. In each homework, we will be very clear as to the format of the output you should produce. But there should be no guess work, because recall that a rudimentary set of tests are run when you click Submit. Please review the output of the Submission Report to see if your program matched our expected outputs. If there are differences, thediff
tool will produce output you can use to try to find the issue and resolve it. Let’s take a quick tour of thediff
tool output.
Suppose we have two files:
file1.txt (Imagine this is the text your program outputs)
I like the following on my sandwich
ham
cheese
mustard
mayo
Thanks!
file2.txt (And imagine this is the desired solution output)
I like the following on my sandwich
ham,
cheese
and mayo
Thanks!
If we ran the diff
tool on file1.txt
and file2.txt
it would compare them and list the line numbers where a difference exists and they kind of change that is required to make them equivalent. Essentially, diff
provides instructions for how to change the first file to match the second file. The output of diff
ing the two files above is shown below:
2c2
< ham
---
> ham,
4,5c4
< mustard
< mayo
---
> and mayo
8d6
<
Notice it only shows lines that differ. Things that are the same like the first line or the line with cheese
are left out to focus on what is different. diff
will show sections of text from each file. A line starting with <
is from the first file (file1.txt
) while lines starting with >
are from the second file (file2.txt)
. Before showing the differences a description like 2c2
will be listed. The numbers are the line numbers in the first and second file, and the letter is a code where c
means changed, d
means deleted, and a
means added and describes what needs to be done to make the first file match the second.
Let’s understand the output:
2c2
< ham
---
> ham,
This section indicates ham
on line 2 of the first file would need to be changed to ham,
to match line 2 of the second file.
4,5c4
< mustard
< mayo
---
> and mayo
This section indicates line 4 and 5 from the first file should be changed to just and mayo
on line 4 to match the second file.
Finally,
---
> and mayo
8d6
<
This section indicates, that the blank line on line 8 of the first file should be deleted to match line 6 in the second file. This is a common mistake in CS 102 where students often add an extra newline (endl
) character or miss a newline. So take care to look carefully at the diff
output.
A nice tutorial for understanding diff
output can be found at this webpage.
So please review the submission report and be sure you understand what the report is telling you. It will save you time and improve your scores. Good luck!
Part 1 - Inches Conversion
Write a program to converts a user-given number of inches to the equivalent total (i.e. sum) of yards, feet, and inches.
Use these conversions:
- 1 yard = 3 ft
- 1 ft = 12 inches
The user should be prompted to enter a number of inches (as an integer). You should then output the yards, feet, and inches which are all integers. These are not separate conversions but the sum of the yard, feet, and inches should match the original input: For example, if the user enters 50
inches, you should output:
Yards = 1
Feet = 1
Inches = 2
because 50 = 36 (1 yard) + 12 (1 foot) + 2 (2 inches).
If the user enters 91
inches, you should output:
Yards = 2
Feet = 1
Inches = 7
because 50 = 72 (2 yard) + 12 (1 foot) + 7 (7 inches).
Please post any questions about using Vocareum on EdStem.
Writing Your Code Using Vocareum (Recommended)
This assignment will be submitted through Vocareum.
When you start the assignment in Vocareum you should see a file inches.cpp
in the work
area in the left hand window pane. Click inches.cpp
to open the file in the editor. You should see some comments in the editor. Enter your name and email in the indicated lines. From there you can read the other comments and then either delete them or simply leave them and start your program code beneath the comments.
Write your program (it will save automatically when you click outside of the editor window). When your ready to test it, you can:
- Click the
Build
button to compile it. Any compile errors will appear in the command/output (aka “terminal”) window at the bottom. You can read the errors to try to understand what the error is and what line it appears on. - Click the
Run
button to execute your program. You’ll see any output generated by your program in the “terminal” window at the bottom and can also type in input in that same window to test your code.
Repeat the build and run process until you believe your code works. Then click the Submit
button. This will submit your code. You are allowed to submit as many times as you like. When you click Submit
, our scripts will automatically run a few (not all) of our grading test cases through your program and report the results. Look at those results to ensure your code is passing these basic tests. If everything passes, you are done! Your code is submitted. If something fails, you can simply go back edit, build, run, and submit again (as much as you like).
If you have submitted on Vocareum at this point you are done! The instructions below are only if you want to use some other editor/compiler to write your code.
Writing Your Code Using Other (Non-Vocareum) Alternatives (Not Recommended)
You can write your program using any editor and compiler (if you have one installed on your machine) but will eventually need to upload your code in a file named inches.cpp
to Vocareum.
When you believe your program is finished leave that browser tab/window open and in a new tab/window, login to Vocareum, start the assignment (LeapYear Part), and in the upper-left of the resulting window, click New
..File
. A textbox will appear where you can type in the filename (leave the work/
portion there and) just type inches.cpp
. In the left window pane you should now see inches.cpp
appear (it must be named inches.cpp
) and you can click on it to open it. A blank window should appear on the right. Cut/paste your code from your other tab/window to this window and you can click Submit
. When you click submit it should run a few automated tests to let you know if it compiled and passed some basic sanity checks. Look at the output to ensure things are working. If not, you’ll need to modify your code in Vocareum or back in your other tab/window and then cut/paste back to Vocareum.
Be sure you click submit otherwise your code will just be saved, but not submitted!!
Part 2 - Interest
The formula for computing interest on a loan is:
\[\Large Amount = P*({1+r})^t\]where P
= Initial Principal, r
= intereset rate, and t
is time (i.e. number of years).
Assuming no payments are made and given the initial principal and interest rate, print out the amount the total owed on the loan after each of the first 4 years.
You must print the amount owed with only 2 decimal places. To aide you in printing double
s with 2 decimal precision, please watch the lecture video on I/O Manipulators.
For example, if the user enters: 15000.00 .05
as input, your ouput must be formatted as:
After year 1 you owe 15750.00
After year 2 you owe 16537.50
After year 3 you owe 17364.38
After year 4 you owe 18232.59
Writing Your Code Using Vocareum (Recommended)
This assignment will be submitted through Vocareum.
When you start the assignment in Vocareum you should see a file interest.cpp
in the work
area in the left hand window pane. Follow the similar procedure you used for the previous part of the assignment to edit, compile, run, and submit your code.
Writing Your Code Using Other (Non-Vocareum) Alternatives (Not Recommended)
You can write your program using any editor and compiler (if you have one installed on your machine) but will eventually need to upload your code in a file named interest.cpp
to Vocareum. Follow the procedure outline in the previous part of the assignment for naming/uploading your code and submitting it.