Due Thursday, 14 Oct 1999, at the beginning of class.
Read Sections 3.1-3.5 and 7.3 of the text. Begin reading Section 10.1.
Besides producing error messages, a compiler can also produce warnings about risky statements that may be erroneous. To have these warnings printed, use a command line like
To have the compiler attempt to check whether array accesses are out of bounds, try using
Of course, one can simultaneously use both options.
When submitting a program, please submit a printed copy of the program's code prepended with comments briefly describing its input and output. Please indent your code to make it readable. See Section 2.5 of the textbook for style hints. If you desire, you can also submit examples demonstrating your program's correctness.
Write a function to compute interest on a credit-card account balance. The function takes arguments for the initial balance (a double), the monthly interest rate (a double), and the number of months for which interest is to be paid (an int). The value returned (a double) is the interest due. Interest is to be compounded monthly; that is, the first month interest is charged only on the initial balance, but in subsequent months interest is charged on the sum of the initial balance and the interest due in previous months. See Display 2.14 in the text for an example of such a calculation. (Hint: In your program you may want to use a for loop instead of a while loop.)
Also write a program to test your function. This program should read an initial balance, a monthly interest rate, and a number of months, and then use the function to compute the interest due and print the result with 2 decimal places. (See page 53 of the text for instructions on how to accomplish this.) It should repeat this (accepting input, and then computing and printing a result) as often as the user wishes. Choose an appropriate method for indicating end of input, and document your choice in the comments at the top of the program.
The following table shows what the function should return for some example sets of input.
Initial balance | Monthly interest | Months | Interest due |
100 | 0.01 | 1 | 1.00 |
100 | 0.02 | 1 | 2.00 |
100 | 0.01 | 2 | 2.01 |
100 | 0.01 | 12 | 12.68 |
Hints: See the character functions in Appendix 4. The >> operator ignores all whitespace characters so use the cin.get function. For example, if c is a character variable, the function call cin.get(c) stores the next character in c. Here is part of a program that reads its input, one character at a time.
char c; while (cin.get(c)) { // Do something with the character in c. }
What is the result of running the program on your program's C++ source code?
// input: lowercase alphabetic character // output: for an input of 'a', returns 0 // for an input of 'b', returns 1 // ... // for an input of 'z', returns 25 // for any other input, returns an unspecified value int charToIndex(char c) { return static_cast<int>(c) - static_cast<int>('a'); } // input: index in the range 0, 1, ..., 25 // output: for an input of 0, returns 'a' // for an input of 1, returns 'b' // ... // for an input of 25, returns 'z' // for any other input, returns an unspecified value char indexToChar(int i) { return static_cast<char>(i + static_cast<int>('a')); }
Count both upper and lower case characters. For example, the count of a's should include both a's and A's. (Hint: Consider modifying the first function.)
Using a WWW browser, find an interesting short WWW page or file, save it to your local directory, and run your program on it, submitting the page or file and the associated counts.
No extra credit will be given if the preceding homework problems are not solved.