---------------------------------------- ** On paper portion. Last Name: First Name: FOR EVERY PROBLEM, SHOW YOUR WORK... NOTE: do the last problem last, if too many people run out of time we won't count the last problem (#9). NOTE: for #8, only do 2 of the 3 parts, I'll only count 2 of them. Points: 1/2 pt each for 3b, 6a, 6b, 6c, 6d, 7a, 7b, 7c, 7d 1 pt each for 1a, 1b, 2a, 2b, 3c, 3d 2 pts each for 3a, 4, 5, 8a, 8b, 8c, 9 Total points: 24.5 NOTE: parts that are 1/2 pt will be graded all or nothing (no partial credit. parts that are 1 pt will be graded as right, wrong, or 1/2 credit. parts that are 2 pts will be graded as 0, 1/2, 1, 1.5, 2. 1a) Convert 8 from decimal to binary. 1000 1b) Convert 0110 0111 from binary to decimal. What is this as an ASCII character? 64+32+4+2+1 = 103, 'g' 'a' = 97 'A' = 65 '0' = 48 2a) What is the range of values that can be stored in a 5 bit unsigned integer? 00000 to 11111 0 to 31 0 to 2^5 - 1 Note: if asked for signed, then -16 to 15 2b) Give a formula for the range of values that can be stored in a 16 bit unsigned integer. 0 to 2^16 - 1 3a) Fill in the table of big-O running times, where n is the # of numbers. NOTE: the order of the rows is not the same as on the sample exam. Algorithm Running time ------------------------------------------------- Linear search O(n) Merge sort O(n log(n)) Quick sort O(n log(n)) Selection sort O(n^2) Insertion sort O(n^2) Binary search O(log(n)) 3b) What is the value of log_2(1024)? (Note that 1024 = 2^10)? NOTE: _ means subscript, ^ means superscript. 10 3c) Compute the running time (# of steps) of linear search when n=1024: 1024 or O(1024) or 1024-1 3d) Compute the running time (# of steps) of merge sort when n=1024: O(n log(n)) = 1024 * 10 = 10,240 4) Given the following list of numbers, show what the list looks like after each step of selection sort. 2 8 5 6 7 1 3 4 1 8 5 6 7 2 3 4 1 2 5 6 7 8 3 4 1 2 3 6 7 8 5 4 1 2 3 4 7 8 5 6 1 2 3 4 5 8 7 6 1 2 3 4 5 6 7 8 5) Decrypt the following message using the caesar cipher and a shift value of 4. csy teww abcdefghijklmnopqrstuvwxyz right by 4 would give gwc xiaa left by 4 would give you pass 6) For each of the following, what is the final value of x? 6a) int x = 1 + 4 * 2; 1 + 8 9 6b) float x = 11 / 3 * 3.0; // note: / and * and % are all same precedence 3 * 3.0 9.0 6c) char x = 'a'; x += 3; 'd' or 100 6d) int x = 12 % 5; 2 7) For each of the following boolean expressions, determine whether it is true or false. 7a) (10 == 3 || 3 != 10) F || 3 != 10 F || T T 7b) (strcmp("hi","HI") == 0) non-zero == 0 F 7c) ('a' < 'b' && 'b' != 'a') T && 'b' != 'a' T && T T 7d) ! ( strcmp("hi","HI") == 0) ! ( non-zero == 0) ! ( F ) T 8) Be the computer. For each of the following, keep track of the values of all the variables as the code runs - as we have done on the board in class. NOTE: CHOOSE ONLY 2 OF THESE TO DO, ONLY 2 WILL COUNT! 8a) int decrease(int x) { // x: 3 return x - 1; // return 2 } int isZero(int x) { // x: 2 if (x == 0) return 1; else return 0; // return 0 } int main() { // y: 3, then 2, then is 0 int y = 3; y = decrease(y); // decrease(y) has the value 2 printf("%i\n", y); y = isZero(y); // isZero(y) has the value 0 printf("%i\n", y); } screen: 2 0 012345 8b) char msg[10] = "great"; int i; for(i=0; msg[i] != '\0'; i++) { if (msg[i] < 'k') msg[i] = '*'; } printf("%s\n", &msg); // msg: "great", "*reat", "*r*at", "*r**t" // i: 0, 1, 2, 3, 4, 5 // screen: *r**t 8c) char msg[10] = "10.45+7"; int i=0; while (isdigit(msg[i])) { i++; } msg[i] = '\0'; int x = atoi(msg); printf("%i\n", x); 01234567 // msg: "10.45+7", "10" // i: 0, 1, 2 // x: 10 screen: 10 9) Memorize program. Write down the code for basic_smallest.c Assume you have an array of 10 integers in a variable calls numbers. Write the code for the main function to determine the smallest number in the array, and print that number. NOTE: JUST WRITE THE MAIN FUNCTION. HERE's A START... #include #include int main() { // first, declare the numbers array... // second, write a loop to determine the smallest... }