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

Assigned:
September 24, 1999
Due:
October 1, 1999, at start of class. (New due date: October 6, 1999.)
Credit:
40 points.

Programming problems

Credit:
10 points each.
Instructions:
Write a C++ program to solve each of the problems below. See the guidelines for programming assignments for what to turn in (briefly, listings of your program, its output, and any test data). See also the individual problem descriptions. Your code should begin with comments that describe what the program is supposed to do (not how it does it, but what it does -- i.e., its input and output). See the sample programs for examples.

Problem: Decimal to binary conversion

In class we developed an algorithm for converting non-negative integers to binary representation. Write a C++ program based on this algorithm. Input to the program will be a non-negative decimal integer X. Output will be the binary representation of X. For example, an input of "5" will yield "101", and an input of "25" will yield "11001".

Clarification: You can assume that the number entered by the user is not too big to fit in the computer's int data type. You will probably need to make an assumption about how many bits are needed to represent the number; make an assumption you think is reasonable and document it in the program header. (For example, if you assume 4 bits will be enough, you would include in the program header a comment like "Input is a non-negative decimal integer X, with X < 16.")

Problem: Exponentiation

Write a program to compute the exponentiation function BE (B to the Eth power), where B is any non-zero number (not necessarily an integer) and E is an integer. Remember that B0 is 1 for all B, and B-E is 1/BE.

Examples:

Clarification: You can assume that B is non-zero.

Problem: Patterned text strings

Write a program to find and print all text strings of the following form: 2 letters, 1 number, another letter, and then the first two letters again. "ab3cab" and "aa1baa" would be included; "abcdef" and "ab4cde" would not be included. Assume the letters are in lowercase.

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 += 1; 
    }

Correction: For this problem, your output may be more than you want to print out and turn in. If that is the case, print a sample of the output (a page's worth, say) and indicate how many text strings your program prints in all.

Additional hint: You can capture output of a program into a text file outputfile like this (assuming your program executable is named a.out):

    a.out > outputfile 
You can then view outputfile with vi, print it with lpr, etc.

Problem: Currency conversion

Write a simple currency-conversion program. Currencies will be represented as single letters. The input should consist of two parts: Output should consist of: Hints:

Clarification: Input to this program is somewhat long and complicated. First, each row in the table is represented by a line containing two characters and a number (F T x). (This is meant to represent a currency exchange rate, i.e., to convert currency F to currency T, we should multiply by x.) For example, the line d p 0.25 represents a row in the table indicating that to convert currency d (dollars?) to currency p (pounds?) we should multiply by 0.25. The end of the table is signalled by a line consisting of two characters (could be any characters) and the number 0. This is followed by one more line of input that contains two currency names (called a and b in the above) and an amount (A). Your program should look for a table row showing how to convert from a to b and use it to convert amount A. So for example with the table row shown previously, if the final line is d p 100.0, your program should compute and print 0.25 * 100.0 or 25.0. (You don't need to worry about how many decimal places are printed.)

Sample inputs and desired output:

Because it would be very tedious to type in the whole table every time, I suggest that you put your input data in files and use input redirection. So to test your program with the examples above, you could put the input lines shown in three text files (which you can create with whatever text editor you use to write your programs -- vi, emacs, etc.). If your program executable is called a.out, and you have put the above sets of input data in files called input1.data, input2.data, and input3.data, you can run the program by saying:

    a.out < input1.data 
    a.out < input2.data 
    a.out < input3.data 

Also, you will probably have to make an assumption about the maximum number of entries in the table. As in the binary-to-decimal problem, make an assumption that seems reasonable to you and document it in the header of your program.