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 two decimal places. 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:
char c; while (cin.get(c)) { // Do something with the character in c. }
Use your program's C++ source code as one of the test inputs. (That is, run the program using its source code as input and turn in the resulting output.)
// 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.
Clarification:
We have not talked about static_cast in class. We will do so in the next class meeting. Briefly, however, the following expression:
static_cast<int>(x)means "take x and convert it to an int". Another, more common, way to write this is as follows:
(int) (x)The difference between the two is that the compiler will check the first and print a warning/error message if the requested conversion does not "make sense" (for example, a request to convert a char to a double does not make sense).
g++ -Wall hello.ccTo have the compiler attempt to check whether array accesses are out of bounds, try using
g++ -pedantic hello.ccOf course, you can simultaneously use both options:
g++ -Wall -pedantic hello.cc