CS 1320 (Principles of Algorithm Design I):
Homework #4

Assigned:
October 6, 1999.
Due:
October 13, 1999, at start of class. (New due date: October 18, 1999.)
Credit:
30 points.

Programming problems

Credit:
10 points each.
Instructions:
Write a C++ program to solve each of the problems below. For each program, unless otherwise instructed, turn in: See the guidelines for programming assignments for a more detailed description. Remember that at least one other human will read your program, so try to make it clear and readable. See the guidelines for tips on what makes for readable code, and the sample programs for examples.

Credit-card interest

(This problem is based on programming project 5 in Chapter 3 of the text.)

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

Character processing, part 1

In C++, most characters fall into one of five different categories: letters, decimal digits, whitespace characters, punctuation characters, and control characters. Write a C++ program that reads its input as a sequence of characters and prints the number of characters in each category.

Hints:

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.)

Character processing, part 2

Write a C++ program to count the frequency of all alphabetic characters in a file, producing a printout listing the number of a's, b's, ..., z's. The following functions may be useful. The first one converts a lowercase alphabetic character to an integer. The second function reverses the conversion.

// 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).

General hints and tips

Compilation tips

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 the following.
        g++ -Wall hello.cc
To have the compiler attempt to check whether array accesses are out of bounds, try using
	g++ -pedantic hello.cc
Of course, you can simultaneously use both options:
	g++ -Wall -pedantic hello.cc