15-122 Practice Exam - I Page 1 of 10 15-122 : Principles of Imperative Computation Victor Adamchik Practice Exam - I Name: Andrew ID: Answer the questions in the space provided following each question. We must be able to clearly understand your answer. It it is vague or confused, it will be marked wrong. Be sure to read the directions to each question carefully so that you know what it is asking for. Question Points Score 1 39 2 27 3 19 4 15 Total: 100
15-122 Practice Exam - I Page 2 of 10
15-122 Practice Exam - I Page 3 of 10 1. Modular Arithmetic. (a) In C 0, values of type int are defined to have 32 bits. In this problem we work with a version of C0 where values of type int are defined to have only 7 bits. All integer operations are still in two s complement arithmetic, but now modulo 2 7. For each of the following statements fill in the missing quantities, in the specified notation. (3) i. The minimal negative integer, in decimal: (3) ii. The maximal positive integer, in decimal: (3) iii. 4, in hexadecimal: (3) iv. 44, in hexadecimal: (3) v. 0x48, in decimal:
15-122 Practice Exam - I Page 4 of 10 (b) Assume int x, y, z has been declared in C 0 and initialized to unknown values. For each of the following, indicate if the expression always evaluates to true, or if it could sometimes be false. Explain your answer. (3) i. x-1 < x (3) ii. y * (x/y) + x % y = x. (3) iii. ~(x ~ (~ x)) == -1 (3) iv. x <= (1<<31)-1 (3) v. x+x == 2*x (3) vi. If x+z = y+z, then x = y.
15-122 Practice Exam - I Page 5 of 10 (3) vii. Using only bitwise operators, find the k-th bit in the two s complement of the C 0 integer n. (3) viii. Compute 33 14 in binary using 8-bit two s complement representation.
15-122 Practice Exam - I Page 6 of 10 2. Wild Elephant Safari. Your computer is on safari to find an elephant in Africa. It assumes that Africa corresponds to a rectangular array represented as a one-dimensional array A. In the array A, the presence of an elephant is marked by number 9. The program on the next page looks at every element of the array from north to south and, on each row, from west to east. Unfortunately, it fails to find the elephant, even though there is at least one elephant in Africa. The reason is that elephants may move while your computer is searching for them. An elephant may move to a position that your program already looked at in an earlier iteration. A computational zoologist confirms that elephants may move at every step of your function implementation that executes an assignment or increment/decrement (like i++ or i--). Elephants move either 1 cell in the north-south direction or 1 cell in the east-west direction, which concurrently changes the memory contents of array A by swapping an elephant to a neighbor cell. The computational zoologist also knows that elephants never travel farther than 1 move away from their home cell! Hint: You decide to change your program so that when it checks for elephants at a cell, it also looks for the elephant in the immediate neighbor cells, just in case the elephant has moved. The computational zoologist warns you that you need to look around to neighboring cells without using assignments or increment/decrements or else the elephant may move away again. (5) (a) Fill in the requires and ensures clauses of the safari function (see next page). (5) (b) Fix the loop invariants in the safari function if they are broken (see next page). (12) (c) The safari function is broken. Fix the implementation of the safari function (see next page), so that it always finds an elephant. Make sure you clearly add a new statement, and clearly modify or delete a given statement, or clearly rewrite the implementation as needed. (d) (5) What is the worst-case asymptotic complexity of your safari implementation as a function of width and height, in big-o notation? Please briefly justify your answer.
15-122 Practice Exam - I Page 7 of 10 typedef int elephants; int safari(elephants[] A, int width, int height) //@requires ; //@requires ; //@requires ; //@ensures ; { } for (int i = 0; i < height; i++) //@loop_invariant 0 < i && i <= height; { for (int j = 0; j < width; j++) //@loop_invariant 0 < j && j <= width; { int r = i * width; int e = r + j; if (A[e] == 9) return e; } } // there IS an elephant in Africa, so cannot get here //@assert false; return -1;
15-122 Practice Exam - I Page 8 of 10 3. Asymptotic Notations. (5) (a) Provide the formal definition of the big-o notation and briefly state the two key ideas behind this definition in two sentences and briefly explain why and how it captures those key ideas: (b) For each of the following, indicate if the statement is true or false. (2) i. O(n 2 + 1024n log n + 10 10 ) = O(5n 2 1) (2) ii. O(n log(n)) = O(n 2 ) (2) iii. O(n 2 ) = O(n log(n)) (2) iv. O(3 log 2 n) = O(2 log 3 n) (2) v. O((10 log n + 3 5) n) = O(n log(n 3 )) (2) vi. O((log n + 3 n) (n 2 + (log n)/2 + n)) = O((1 + n) 2 (log(log n) + n log n)) (2) vii. O(2 n ) = O(2 2n )
15-122 Practice Exam - I Page 9 of 10 (15) 4. Linked Lists. Recall the definition of linked lists: struct list { int data; // a decimal digit struct list* next; }; typedef struct list* list; We define struct ring { list start; // a pointer to the first node in the ring int size; // the ring size }; typedef struct ring* ring; A ring is represented as a circular linked list with one node designated as a start. For example, a ring with items 1, 2, 3, 4, 5 (in this order, 1 at the front and 5 at the back) would be represented as the following ring: 4 3 5 2 data next data next 1 ring 5 start size
15-122 Practice Exam - I Page 10 of 10 As an example, here is a function that checks if the ring is empty. bool ring_empty(ring R) //@requires is_ring(r); { return R->start == NULL && R->size == 0; } Write a function (and contracts) that creates a new ring from a given string, such that each character is a node in the list. Any errors in inputs must be handled by contracts. The first digit of the string must be pointed by R->start. For example, the function call ring_new("12345") will create a ring as depicted in the picture above. To convert a char to an int, you should use ascii values, in particular 0 has value 48 and 9 has value 57. The C 0 string library is provided to you in the appendix. ring ring_new(string str){