CS 1401 Assignment #5

Date Assigned: Monday, February 19, or Tuesday, February 20, 2007.

Due Date: Monday, February 26, or Tuesday, February 27, 2007, before the beginning of your lab section.

Objective: The main objective of this assignment is to practice using loops.

Programming assignment: It is well known that computers use a binary system to represent integers. In some computer applications, it is also useful to use octal code (base 8), and hexadecimal (hex) code (base 16). In the past, some computers have used ternary code (base 3) as well. The following algorithm is used to convert a decimal positive integer n into the binary form: you divide n by 2, keep a remainder. Divide the result by 2, keep a remainder, etc. until we get 0. The remainders, when read bottom to top, form the desired binary number.

Similarly, you can convert a decimal positive integer n into the hex form: you divide n by 16, keep a remainder. Remainders 10, 11, 12, 13, 14, and 15 are described as, correspondingly, A, B, C, D, E, and F. Divide the result by 16, keep a remainder, etc. until we get 0. The remainders, when read bottom to top, form the desired hex number.

Write a program that, given an integer, uses the above algorithm to return a hex representation of this number (as a string).

Example of a binary code: for n = 13, we have

```13 / 2 = 6 rem 1
6 / 2 = 3 rem 0
3 / 2 = 1 rem 1
1 / 2 = 0 rem 1
```
When we read the remainders from bottom to top, we get 1101, which is exactly the binary representation of 13 -- since 1 * 8 + 1 * 4 + 0 * 2 + 1 * 1 = 8 + 4 + 1 = 13.

Example of a hex code: for n = 92, we have

```92 / 16 = 5 rem 12 which is C
5 / 16 = 0 rem 5
```
When we read the remainders from bottom to top, we get 5C, which is exactly the binary representation of 92 -- since 5 * 16 + 12 = 80 + 12 = 92.

Hint: originally, the division-result is 92, the string-so-far is empty. Within the loop, you divide the division-result by 16, compute the remainder, and add the corresponding one-character string ("0", "1", ..., "9", "A", ..., "F") to the left of the string-so-far.

For extra credit: write a program that transforms hex numbers back into decimal. Main idea:

• start with the first (0th, in Java terms) symbol of the string, and make value-so-far 0, 1, ..., 9, 10, ..., 15 depending on whether this symbol is 0, 1, ..., 9, A, ..., or F; then, delete this symbol from the string;
• at any step, if you have one more symbol, you multiply the value-so-far by 16 and add the corresponding hex digit to the resulting value;
• stop when there are no symbols left.

Example: for the string 5C,

• the first symbol is 5, so the corresponding value-so-far is 5 and the remaining string is C;
• the remaining string is not empty, so we take the first symbol of the remaining string, it is C; we multiply the value-so-far by 16, it is now 5 * 16 = 80, and add the value (12) corresponding to this symbol C, we now get 80 + 12 = 92; the remaining string is empty.
Since the remaining string is empty, the value-so-far (92) is returned as the result.

Homework assignment: on a separate sheet of paper, solve Ex. 6, 10, 18, and 24 at the end of Chapter 5.

Deliverables: as instructed by your TA.