CS 202 (FALL 2019) - EXAM 2 NAME: Points/time: questions in #s 1, 2, 3, 4 are all worth 1 point each and are estimated to take about 1 minute each. There are 18 of these. Question 0 parts are all worth 4 points and estimated to take about 5 minutes each. There are 3 of these, for 12 points total from #0. 0 C Programming (assessment) ============================ - Give C code for each of the following. Note - you DON'T need to give a complete program, and there WILL be partial credit (if merited). 1. Read an integer n from the user, print the n by n multiplication table. Just print the values, you donÕt need to print a header row or column. int n; scanf("%d", &n); for(int i=1; i <= n; i++) { for(int j=1; j <= n; j++) { printf("%d\t", i*j } printf("\n"); } 2. Read from stdin until EOF and print each line with a line # in front. You can print the line number for each \n (donÕt worry about a last line that is empty) char *line = NULL; int size = 0; int lineNo = 0; while (getline(&line, &size, stdin) > 0) { printf("%d %s", lineNo++, line); } int ch, prev = '\n'; int lineNo = 0; while ((ch = fgetc(stdin)) != EOF) { if (prev == '\n') printf("%d ", lineNo); if (ch == '\n') { lineNo++; } printf("%c", ch); prev = ch; } 3. Read a string (using scanf is fine, make sure at most 99 characters) from the user and print it out both as a string and a hex dump with one character per line. If they typed "012 abc" you would print 0 (0x31) 1 (0x32) 2 (0x33) (0x20) a (0x61) b (0x62) c (0x63) char s[100]; scanf("%99s", s); for(int i=0; s[i] != '\0'; i++) { char ch = s[i]; printf("%c (0x%x)", ch, ch); } 1 Functions Every Citizen Should Know ===================================== - Answer the following questions about standard C functions: 1. Explain the differences between memcpy and strcpy. strcpy - strings, stops at NULL byte memcpy - arbitrary data, you give it how many bytes to copy 2. What is the type of the second parameter to scanf? pointer, memory address 3. Explain the difference between fputc and putchar. fputc goes to a FILE * given as parameter, putchar goes to stdout 4. What is the return type of fopen? FILE *, file pointer 5. Explain the differences between malloc and realloc. malloc allocates a given amount of memory realloc resizes and copies if needed 2 Asymptotic Complexity ======================= - For each of the following pseudocodes, give the worst case run time in Big O notation. Note: Show your work / explain how you arrived to this conclusion. PAY ATTENTION TO THE DETAILS of each program! 1. Let a and b be arrays of size n. ,---- | for(int i = 0; i < n; ++i) { // this happens n times | for(int j = 0; j < n; ++j) { // this happens n times, for each value of i | if(a[i] == b[j]) { | count++; | } | } | } `---- O(n**2) 2. Let x be an integer. ,---- | for(int i = 0; i <= n; i += 2) { // this happens n/2 times | for(int j = 0; j <= n; j *= 2) { // for each value of i, happens log(n) times | x += (i - 1) * j | } | } `---- O(n log(n)) 3. Let a be a sorted array of size n. binary search - O(log(n)) - e-s shrinks by factor of 1/2 each time ,---- | int s = 0; | int e = n - 1; | while(e >= s) { | int i = (e - s) / 2; | if(a[i] < x) { | e = s + i - 1; | } else if(a[i] > x) { | s = s + i + 1; | } else { | print(x); | break; | } | } `---- 4-7 (3 points) Write down 10 different big-O functions that are each different in terms of big-O, and put them in big-O order from smallest to largest. Make at least one of your functions be O(log(n)) and at least one of your functions be Omega(2**n) 1 log(n) sqrt(n) =? 2**(log(n)/2) n = 2**(log(n)) n**2 n**3 n**4 n**5 n**6 n**7 n**8 2**n 3 Hash Tables ============= 1. Hash function - what is the purpose and give an example for strings (donÕt need to give code, can use math notation or however you want to specify) take a key and produce an index into an array example for strings: add up the ASCII values of all of the characters, % by the size of the table 2. Collision - what is it and give an example using your hash function from above two different keys hash to the same value. "cab" and "bac" because my hash function doesn't take into account the order of the letters 3. Bucket - what is it, what is its purpose linked list to store items with the same hash value. (array is fine) 4 Linked Lists ============== 1. Suppose you are given the expression "4 3 8 * + 8 2 / -", which is in reverse polish notation. Using a stack implemented with a linked list, evaluate this expression. Show the state of the linked list at each step. Note that if the expression were "6 2 Š" this would evaluate to 4. Stack, step 1: 4 Stack, step 2: 4, 3 4, 3, 8 4, 24 28 28, 8 28, 8, 2 28, 4 24 2. Draw a picture of what happens for the delete operation for a doubly linked list when deleting a node p. You can assume p is not the head or the tail. Write the few lines of code that would run to make this happen. PREVIOUS -> p -> NEXT <- <- p->prev->next = p->next p->next->prev = p->prev free(p) 4 Binary search / AVL Trees ============== 1. Draw what an AVL tree would look like if the following items are inserted in this order: 10, 5, 15, 2, 7, 12, 20 2. Choose two more numbers to insert that would cause a need for rebalancing. Draw the tree before rebalancing and after rebalancing. 3. Give an example of a sequence of numbers to insert (starting from an empty tree) where rebalancing would be less efficient (in terms of total running time, but not necessarily for an individual operation) than skipping the rebalancing. Why?