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

Assigned:
March 16, 2000.
Due:
March 24, 2000, at start of class.
Credit:
30 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

Temperature conversion, revisited again

Write a program to do Fahrenheit-to-Celsius and Celsius-to-Fahrenheit conversions. The program should repeatedly prompt the user to enter "f" (for Fahrenheit-to-Celsius conversion), "c" (for Celsius-to-Fahrenheit conversion), or "q" (to quit). If the user enters "f" or "c", the program should prompt for a temperature and then perform the appropriate conversion and print the result. If the user enters "q", the program should exit. If the user enters any other character, the program should print an error message (and prompt again). Here is a sample execution of this program. (Boldface indicates user input.)
$ a.out
Please choose what to do next:
        f to convert a Fahrenheit temperature to Celsius
        c to convert a Celsius temperature to Fahrenheit
        q to quit (exit the program)
Your choice?  f
Enter the temperature to convert:  212
That temperature in Celsius is 100
Please choose what to do next:
        f to convert a Fahrenheit temperature to Celsius
        c to convert a Celsius temperature to Fahrenheit
        q to quit (exit the program)
Your choice?  f
Enter the temperature to convert:  98.6
That temperature in Celsius is 37
Please choose what to do next:
        f to convert a Fahrenheit temperature to Celsius
        c to convert a Celsius temperature to Fahrenheit
        q to quit (exit the program)
Your choice?  c
Enter the temperature to convert:  37
That temperature in Fahrenheit is 98.6
Please choose what to do next:
        f to convert a Fahrenheit temperature to Celsius
        c to convert a Celsius temperature to Fahrenheit
        q to quit (exit the program)
Your choice?  x
Invalid response
Please choose what to do next:
        f to convert a Fahrenheit temperature to Celsius
        c to convert a Celsius temperature to Fahrenheit
        q to quit (exit the program)
Your choice?  c
Enter the temperature to convert:  100
That temperature in Fahrenheit is 212
Please choose what to do next:
        f to convert a Fahrenheit temperature to Celsius
        c to convert a Celsius temperature to Fahrenheit
        q to quit (exit the program)
Your choice?  q

It is up to you how many functions to use and how to structure your program, but you may find it helpful to use the english_to_metric, program presented in class as a guideline.

Recall that the formula for converting from Fahrenheit to Celsius is C = (5/9)(F - 32), while the formula for converting from Celsius to Fahrenheit is F = (9/5)C + 32.

Character processing

Write a program that reads text input (characters) up to end-of-file (control-D on Unix) and prints: Here is a sample execution of the program. (Boldface indicates user input.)
$ a.out
Enter one or more lines of text, control-D to end:
abcd!
1234?
x y z w
 user enters control-D 
8 letters
4 digits
20 characters in all
Note that the total number of characters includes the "newline" characters at the end of each line, but does not include the control-D typed to signal end of file.

Hints: You can easily check whether a character is a letter, or whether it is a digit, by using library functions described in Section 8.3. For an example of reading input a character at a time, see example program echo_input.cc (presented in class).

After you write your program and test it with some input from the keyboard, also try it out with input from a file, using Unix input redirection. "Input redirection" means that the program's standard input (normally entered from the keyboard) will instead be redirected to come from some other source, such as a file. To use this to run program a.out getting input from text file input.txt rather than from the keyboard, type the following:

    a.out < input.txt 
input.txt can be any file, for example one created using a text editor. It could even be a file containing program source. So you could count the characters in your program's source code by typing something like a.out < mypgm.cc .

Patterned text strings

Write a program to print all text strings of the following form: A lowercase letter, a decimal digit, another lowercase letter, another decimal digit, and then the two lowercase letters again. Examples of such strings include "a1b2ab" and "x2x2xx", but not "a3b4cd".

To solve this problem, it may be useful to know that each character is represented inside the computer as a small integer. For example, try the following lines of code:

    int x = 'a'; 
    cout << x << endl;
One should be very careful about treating characters as integers since the same character may be represented using different numbers on different computers. However, it is fairly safe to write the following loop to print all the lowercase characters a, b, ..., z.
    char c = 'a'; 
    while (c <= 'z')  
    {
      cout << c << endl; 
      ++c; 
    }

Note that this program should produce a lot of output, probably more than will fit into a terminal window or telnet window. You may be able to scroll the window up and down to see all of it; if not, you can use Unix output redirection to put the output in a text file, which you can then examine using your favorite text editor. (This approach also makes it very easy to determine how many strings the program printed, which could help you determine whether the program is working correctly.) "Output redirection" means that everything the program prints to standard output (normally the screen) will instead be redirected to somewhere else, such as a file. To use this to run program a.out and put its output in output.txt, type the following:

    a.out > output.txt 
Note that if output.txt already exists, the above command probably will not work; you will probably (depending on how your account is configured) get the message "Cannot clobber existing file". There are two ways to get around this. One is to delete output.txt and try the above command again. The other is to tell Unix you want to overwrite the previous contents of output.txt. Syntax for doing this depends on how your account is set up. One of the following two commands should work for you:
    a.out >| output.txt 
    a.out >! output.txt 
To find out which one to use, first type
    echo $SHELL 
If the response is /bin/bash, use the first form. If the response is /bin/tcsh, use the second form. If the response is anything else, check with your instructor about what to do.

What to turn in

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