## CS 1401, Exam #2

Date: Friday, October 6, 2006, 11:30 am
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1. Use if-statements to write down a code that, given three numbers n1, n2, and n3, finds the smallest of these three numbers, and assigns this value to the variable whose name is smallest.

Hint: you do not need to read anything from the keyboard or from a file, you do not need to print anything to the screen or place it into a file, just compute and assign. Assume that n1, n2, and n3 have been assigned some values already, which are the numbers that you are comparing.

```Solution:

if (n1 <= n2 && n1 <= n3)
smallest = n1;
else if (n2 <= n1 && n2 <= n3)
smallest = n2;
else
smallest = n3;
```
2. Write down a code that, given two strings s1 and s2, sorts them in inverse alphabetic order: i.e., if s1 precedes s2 in alphabetic order, your code should swap the strings. You can assume that both s1 and s2 only contain small letters.

Example: if s1 is "air" and s2 is "water", then after your code, s1 should contain "water" and s2 should contain "air". Trace your code, step by step, on this example: draw all the boxes and show how their values change after each operation.

For extra credit: make the code work also if some of the letters are capital letters.

```Solution:

if (s1.compareTo(s2) < 0)
{String temp = s1;
s1 = s2;
s2 = temp;
}

Tracing:

originally

--------    -------   -------    -------
|  22    |  |   33  | |  air  |  | water |
--------    -------   -------    -------
s1           s2        22        33

after the line String temp = s1:

--------    -------   -------    -------   -------
|  22    |  |   33  | |  air  |  | water | |   22  |
--------    -------   -------    -------   -------
s1           s2        22        33       temp

after the line s1 = s2:

--------    -------   -------    -------   -------
|  33    |  |   33  | |  air  |  | water | |   22  |
--------    -------   -------    -------   -------
s1           s2        22        33       temp

after the line s2 = temp:

--------    -------   -------    -------   -------
|  33    |  |   22  | |  air  |  | water | |   22  |
--------    -------   -------    -------   -------
s1           s2        22        33       temp

Extra credit:

if ((s1.toLowerCase()).compareTo(s2.toLowerCase()) < 0)
{String temp = s1;
s1 = s2;
s2 = temp;
}

```
3. Draw the truth tables for "and", "or", and "not" operations. Use these truth tables to find the truth value of the statement 3 < 5 || 2 < 4 && 4 < 2. Explain precedence of different logical operations, and how this precedence is related to precedence of different arithmetic operations.

For extra credit: what is the difference between || and |? What is the advantage of each of these operations? Give an example when they lead to different results.

```
Solution:

------------------------------------
| A |  B | A and B | A or B | not A |
------------------------------------
| F |  F |    F    |   F    |   T   |
------------------------------------
| F |  T |    F    |   T    |   T   |
------------------------------------
| T |  F |    F    |   T    |   F   |
------------------------------------
| T |  T |    T    |   T    |   F   |
------------------------------------

3 < 5 is True, 2 < 4 is True, 4 < 2 is False, so the above statement is
T || (T && F) By the table, T && F is F, so we get T || F, which is True.

Precedence: negation ! has the highest precedence, then &&, then ||.

Inside the computer, True is 1, False is 0, so && is actually
multiplication. Or is almost addition, and precedence for && and || is
the same as for multiplication and addition.

Extra credit: in computing A || B, if the computer finds out that A is
true, it stops computing and assigns the value True to A || B without
bothering to compute the truth value of B. If we use | instead of ||,
the computer evaluates both A and B. An example where we get different
results:

* 2<4 || (1/0 > 0) returns True because 2<4 is true.

* on the other hand, 2<4 | (1/0 > 0) returns an error message because
1/0 is not defined.

```
4. Use the while loop to write a piece of code that, given an integer n, finds the smallest square k^2 for which k^2 >= n. In other words, your code should find the smallest square which is larger than a given integer n. For example, for n = 7, we should return 9, because 3^2 = 9 > 7, and 9 is the smallest such square: 2^2 = 4 < 7.

Idea: start with k = 0, and while the condition k^2 >= n is not satisfied, keep increasing k by 1. Trace your code step-by-step for n = 7.

For extra credit: what is the advantage of using k++ instead of k = k+1?

```
Solution:

k = 0;
while (!(k * k >= n))
k++;
result = k * k;

Tracing:

First k = 0. The condition !(0 * 0 >= 7) is satisfied, so we increase
k by 1; the new value of k is thus 1.

The condition !(1 * 1 >= 7) is still satisfied, so we increase
k by 1; the new value of k is thus 2.

The condition !(2 * 2 >= 7) is satisfied, so we increase
k by 1; the new value of k is thus 3.

The condition (3 * 3 >= 7) is true, so !(3 * 3 >= 7) is false, and
we get out of the while loop. So, the variable result is assigned a
value k * k = 3 * 3 = 9.

Extra credit: in general, addition c = a + b means that we fetch a, fetch b,
and then send the result to c. In the computer, fetching requires
looking into the huge memory, so it takes much longer than performing
any arithmetic operation. Operation k++ requires only one fetching of k,
so it requires only half of the time of addition.

```
5. To convert a decimal integer n to binary form, you repeatedly divide n by 2 and keep the remainder until you get 0. Reading the remainders from bottom to top provides the desired binary number. Use this algorithm to convert 19 to binary code. Check the result by converting the binary number back into the decimal code.

Reminder: 137 in decimal code means 1 * 10^2 + 3 * 10^1 + 7 * 10^0. Similarly, 101 in binary code means 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 1*4 + 0*2 + 1*1 = 4 + 1 = 5.

```