EE 1301 UMN Introduction to Computing Systems Fall 2013 Sample Problems for Quiz # 2 (with solutions) Here are sample problems to help you prepare for Quiz 2 on Oct. 31. 1. Bit-Level Arithmetic (a) Consider the following C program. # include <stdio.h> int main ( int argc, char ** argv ) unsigned int v; v = atoi ( argv [1]); v - -; v = v >> 1; v = v >> 2; v = v >> 4; v = v >> 8; v = v >> 16; v ++; printf ("%d\n", v); What will it print out if it is called as follows?./ rndpow2 32./ rndpow2 42./ rndpow2 1023 Hint: the program computes 1 << (log 2 (v 1) + 1).
EE 1301, Fall 13 2 (b) What will the following program print out? # include <stdio.h> int main () int i; int m = 0xA; unsigned int n = 0; for (i = 0; i < 8; i ++) n = (m << i *4); for (i = 0; i < 32; i ++) printf ("%d", (n >> i) & 1); printf ("\n"); Answer: 01010101010101010101010101010101
EE 1301, Fall 13 3 (c) Consider the following program. # include <stdio.h> enum boolean false, true ; int main ( int argc, char ** argv ) int i; int n = atoi ( argv [1]); enum boolean sign = atoi ( argv [ 2]) > 0? true : false ; unsigned int r; if ( sign ) r = 0; r = (1 << n); else r = ~0; r ^= (1 << n); for (i = 0; i < 32; i ++) printf ("%d", (r >> i) & 1); printf ("\n"); Suppose that it is called with the following arguments. What will it print out?./ set - given - bit 7 0 Answer: 11111110111111111111111111111111./ set - given - bit 27 1 Answer: 00000000000000000000000000010000
EE 1301, Fall 13 4 2. Finite-Precision Arithmetic Consider the following program. # include <stdio.h> int main () unsigned char a = 100; unsigned char b = 200; unsigned char c = a + b; unsigned char d = a - b; char e = a + b; char f = a - b; char g = -(a + b); int h = a + b; printf ("%d %d %d %d %d %d %d %d %o %x\n", a, b, c, d, e, f, g, h, h, h); What will it print out? Answer: 100 200 44 156 44-100 -44 300 454 12 c
EE 1301, Fall 13 5 3. Optimal Cost Paths Through Graphs Find the minimum cost path from A to be B, where the cost is the sum of the numbers along that path. 1 ----- 25 ----- 3 ------ 6 ------ 12 ----- 12 ----- 5 ------ 25 ----- 1 / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / 2 \ / 11 \ / 8 \ / 7 \ / 4 \ / 1 \ / 1 \ / 8 \ A / / / / / / / / B \ / \ 9 / \ 5 / \ 1 / \ 25 / \ 8 / \ 1 / \ 15 / \ 9 / \ / \ / \ / \ / \ / \ / \ / \ / \ / 7 ----- 10 ----- 17 ----- 9 ------ 1 ------ 11 ----- 7 ------ 1 ------ 3
EE 1301, Fall 13 6 4. Number Systems (a) Suppose that the following bits represent positive numbers. their decimal equivalents. i. 10101011 2. Answer: 171 10. ii. 01111110 2. Answer: 126 10. iii. 10000000 2. Answer: 128 10. iv. 11111111 2. Answer: 255 10. Convert them to (b) Suppose that the following bits represent positive and negative numbers in two s complement notation. Perform the operation indicated. If the result cannot be represented with 8 bits, say so. i. 10000000 2 + 00000001 2. Answer: 10000001 2 = -127 10. ii. 01111111 2 + 01111110 2 Answer: 11111101 2. Positive number plus positive number yields negative number. Conclude that result cannot be represented with 8 bits. iii. 10000000 2-01000000 2. Answer: 10000000 2 + 11000000 2 = 101000000 2. Extra bit falls off. But then we have a negative number plus a negative number yields a positive number. Conclude that result cannot be represented with 8 bits. iv. 11111100 2-11111110 2. Answer: 11111100 2 + 00000010 2 = 11111110 2 = -00000010 2 = -2 10. (c) Suppose that the following are all positive numbers. Perform the indicated conversion. i. 101101101000111101110 2 to hexademical. Answer: 0001 0110 1101 0001 1110 1110 2 = 16D1EE 16. ii. 623401 8 to hexademical. Answer: 110 010 011 100 000 001 2 = 0011 0010 0111 0000 0001 2 = 32701 16. iii. AF AB 16 to octal. Answer: 1010 1111 1010 1011 2 = 001 010 111 110 101 011 2 = 127653 8
EE 1301, Fall 13 7 5. Ninjas and Knapsacks This problem will test your understanding of dynamic programming, an algorithmic archetype that we discussed in class Suppose that a ninja has broken into the Imperial Palace in Tokyo to steal valuable trinkets. He will catch the direct return flight with Delta to Minneapolis, but he s very annoyed at Delta s new fees for checked-in luggage. Out of principle, he refuses to pay such fees; everything that he steals from the Imperial Palace must fit in his backpack as carry-on. The tensile strength of his UMN backpack is such that he can safely pack 10 lbs. The ninja assesses the value and weight of the trinkets to be as follows: item value weight 1 3 1 2 4 3 3 9 6 4 2 3 5 11 4 6 2 3 7 4 2 8 3 1 9 1 1 10 7 3 What is the value of the best choice of trinkets? Solution See attached spreadsheet.
EE 1301, Fall 13 8 6. Sorting This problem tests your understanding of sorting (and, of course, recursion). Here is the algorithm that we discussed in class, mergesort(). What does the code print out? # include <stdio.h> mergesort ( int a[], int low, int high ) int mid ; if(low < high ) mid =( low + high )/2; mergesort (a, low, mid ); mergesort (a, mid + 1, high ); merge (a,low, high, mid ); return (0); merge ( int a[], int low, int high, int mid ) printf ("%d, %d, %d\n", low, high, mid ); int i, j, k, c [10]; i = low ; j = mid + 1; k = low ; while ((i <= mid ) &&( j <= high )) if(a[i] < a[j]) c[k] = a[i]; k ++; i ++; else c[k] = a[j]; k ++; j ++; while (i <= mid ) c[k] = a[i]; k ++;
EE 1301, Fall 13 9 i ++; while (j <= high ) c[k] = a[j]; k ++; j ++; for (i = low ; i < k; i ++) a[i] = c[i]; int main ( int argc, char ** argv ) int i; int v [10] = 6, 21, 3, 89, 2, 77, 1, 78, 79, 99; mergesort (v, 0, 9); for ( i = 0; i < 10; i ++) printf ("%d ", v[i ]); printf ("\n"); Answer 0, 1, 0 0, 2, 1 3, 4, 3 0, 4, 2 5, 6, 5 5, 7, 6 8, 9, 8 5, 9, 7 0, 9, 4 1 2 3 6 21 77 78 79 89 99
EE 1301, Fall 13 10 Here is one the most common sorting algorithm, quicksort(). What does the code printout? # include <stdio.h> /* swap : interchange v[i] and v[j] */ void swap ( int v[], int i, int j) int temp ; temp = v[i]; v[i] = v[j]; v[j] = temp ; /* qsort : sort v[ left ]... v[ right ] into increasing order */ void qsort ( int v[], int left, int right ) int i, last ; printf (" left %d, right %d\n", left, right ); /* do nothing if array contains fewer than two elements */ if ( left >= right ) return ; /* move " pivot " element to v [0] */ swap (v, left, ( left + right )/2); last = left ; /* partition */ for ( i = left + 1; i <= right ; i ++) if (v[i] < v[ left ]) swap (v, ++ last, i); /* restore " pivot " element */ swap (v, left, last ); qsort (v, left, last -1); qsort (v, last +1, right ); int main ( int argc, char ** argv )
EE 1301, Fall 13 11 int i; int v [10] = 6, 21, 3, 89, 2, 77, 1, 78, 79, 99; qsort (v, 0, 9); for ( i = 0; i < 9; i ++) printf ("%d ", v[i ]); printf ("\n"); Answer left 0, right 9 left 0, right 0 left 2, right 9 left 2, right 4 left 2, right 2 left 4, right 4 left 6, right 9 left 6, right 5 left 7, right 9 left 7, right 6 left 8, right 9 left 8, right 7 left 9, right 9 1 2 3 6 21 77 78 79 89 99
EE 1301, Fall 13 12 7. Graphs for Coding This problem asks you to formulate codes for characters of the alphabet, using graphs specifically binary decision diagrams. In 1951, David A. Huffman and his MIT information theory classmates were given the choice of a term paper or a final exam. The professor, Robert M. Fano, assigned a term paper on the problem of finding the most efficient binary code. Huffman, unable to prove any codes were the most efficient, was about to give up and start studying for the final when he hit upon the idea of using a frequency-sorted binary tree and quickly proved this method the most efficient. In doing so, the student outdid his professor, who had worked with information theory inventor Claude Shannon to develop a similar code. Huffman avoided the major flaw of previous coding by building the tree from the bottom up instead of from the top down. Suppose that the following characters appear with the given frequency. Determine an optimal binary code for these characters. Char Frequency space 7 a 4 e 4 f 3 h 2 i 2 m 2 n 2 s 2 t 2 l 1 o 1 p 1 r 1 u 1 x 1
EE 1301, Fall 13 13 Solution Char Frequency Code space 7 111 a 4 010 e 4 000 f 3 1101 h 2 1010 i 2 1000 m 2 0111 n 2 0010 s 2 1011 t 2 0110 l 1 11001 o 1 00110 p 1 10011 r 1 11000 u 1 00111 x 1 10010