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

Assigned:
March 29, 2000.
Due:
April 5, 2000, at start of class.
Credit:
20 points (10 points per problem).

Instructions

Write a C++ program to solve each of the problems below. Your code should begin with comments that describe what the program is supposed to do, in terms of its inputs and outputs. Any function you write should begin with brief comments describing its purpose and preconditions/postconditions. See the sample programs for examples.

Problems

Computing variance of numbers read from file

Write a C++ program that reads numbers (not necessarily integers) from a text file innums.txt and computes and prints (to standard output) their variance. The variance of a set of N numbers x1, x2, .... xN is defined to be the sum (x1 - avg)2 + (x2 - avg)2 + .... (xN - avg)2, where avg is the average of the N numbers.

For example, suppose innums.txt consists of the following lines:

	4.0
	5.0
	-2.0
	-3.0
The program should compute and print the value 50, because the average of the four numbers in the file is 1.0, and (4.0 - 1.0)2 + (5.0 - 1.0)2 + (-2.0 - 1.0)2 + (-3.0 - 1.0)2 equals 50.

Your program should make no assumptions about how many numbers there are. (For those of you reading ahead, this means that the program cannot use an array to store the numbers.)

If input file innums.txt does not exist, the program should print a user-oriented error message saying so, rather than crashing or printing a semi-cryptic message about a failed assertion. See the temperature_records sample program for an example of checking that a file has been opened successfully and printing an error message if not.

Hint: Since the numbers are stored in a file (rather than being read in from standard input), your program can read through them more than once. To read the contents of a file a second time, close the file and open it again.

Converting file to upper and lower case

Write a program that takes an input text file intext.txt and produces two output text files: For example, suppose file intext.txt consists of the following lines:
    Now is the time for all good persons 
    to come to the aid of their country.
    Testing, Testing, 1 2 3 4.
    Over and out!
Then after executing the program, file lowercase.txt should consist of the following lines:
    now is the time for all good persons 
    to come to the aid of their country.
    testing, testing, 1 2 3 4.
    over and out!
and file uppercase.txt should consist of the following lines:
    NOW IS THE TIME FOR ALL GOOD PERSONS 
    TO COME TO THE AID OF THEIR COUNTRY.
    TESTING, TESTING, 1 2 3 4.
    OVER AND OUT!

If input file intext.txt does not exist, the program should print a user-oriented error message saying so, rather than crashing or printing a semi-cryptic message about a failed assertion. See the temperature_records sample program for an example of checking that a file has been opened successfully and printing an error message if not.

Hint: You may find library functions tolower and toupper, described in Chapter 8, useful. Be advised, however, that these functions actually return ints, so the following lines of code produce a result that may surprise you (try them!):

	cout << tolower('A') << endl;
	cout << toupper('a') << endl;
To produce the expected result, you must convert the values returned by these functions to chars, as in the following:
	cout << char(tolower('A')) << endl;
	cout << char(toupper('a')) << endl;

What to turn in

Submit your source code as described in the guidelines for programming assignments. For this assignment: