Due Thursday, 30 Sep 1999, at the beginning of class.
Read Section 9.1 of the text and then read Chapter 3.
When submitting a program, please submit a printed copy of the
program's code prepended with comments briefly describing its input
and output. Please indent your code to make it readable. See
Section 2.5 of the textbook for style hints. If you desire, you can
also submit examples demonstrating your program's correctness.
- 1.
- 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.''
- 2.
- Write a program to compute the exponentiation function BE
(B to the Eth power), where B is any number and E is an
integer. Remember that B0 is 1 for all B, and
B-E = .
Examples:
input |
output |
3.0 |
0 |
1 |
2 |
3 |
8 |
2 |
-2 |
0.25 |
- 3.
- 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 a loop to
print all the lowercase characters a, b, ..., z as
char c = 'a';
while (c <= 'z') {
cout << c << endl;
c += 1;
}
- 4.
- Write a simple currency-conversion program. Currencies will be
represented as single letters. The input should consist of two parts:
- A table of conversion factors, each in the form ``F T x'',
indicating that to convert from currency F to currency T one
multiplies by the exchange rate x. F and T will be single
characters; x will be a double. The end of the table will be
indicated by an entry with an exchange rate of zero.
- A line containing two letters indicating the ``from'' and ``to''
currencies a and b and the amount A to convert.
Output should consist of:
- If there is an entry in the table for converting currency a to
currency b, the result of using the table entry to convert
amount A.
- If there is no entry in the table, an error message.
Hints:
- Use three arrays, one for the ``from'' currencies, one for
the ``to'' currencies, and one for the conversion factors.
- To save yourself time in testing,
put your test input data in one or more files and use
input redirection.
1999-09-22