Midterm Review (overview of fall 2005 midterm)

Download Midterm Review (overview of fall 2005 midterm)

Post on 08-Jan-2016

31 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

Midterm Review (overview of fall 2005 midterm). Professor Jennifer Rexford COS 217. void f(unsigned int n) { do { putchar(0 + (n % 10)); } while (n /= 10); putchar(\n); }. 1. Modulo Arithmetic and Character I/O. What does f(837) produce? - PowerPoint PPT Presentation

TRANSCRIPT

<ul><li><p>Midterm Review(overview of fall 2005 midterm)Professor Jennifer RexfordCOS 217</p></li><li><p>1. Modulo Arithmetic and Character I/Ovoid f(unsigned int n) { do { putchar(0 + (n % 10)); } while (n /= 10); putchar(\n); } What does f(837) produce? What does this function do?</p></li><li><p>1. Modulo Arithmetic and Character I/Ovoid f(unsigned int n){ for ( ; n; n /= 10) putchar(0 + (n % 10)); putchar(\n);} When is the answer different?</p></li><li><p>2. Pointers and Stringsvoid f(char *s) { char *p = s; while (*s) s++; for (s--; s&gt;p; s--,p++) { char c = *s; *s = *p; *p = c; } } What does this function do?bar\0</p></li><li><p>3. Short AnswerIn the memory layout for a UNIX process:Why does the heap grow from the top down and the stack from the bottom up, instead of both growing from the top down or both growing from the bottom up? TextDataBSSStackHeap</p></li><li><p>4. Deterministic Finite AutomataValid numbers-3478.1+298.3-34.7e-134.7E-17..7999.99e99 Invalid numbersabc-e91e+17.9A0.38+.38.38f9 Identify whether or not a string is a floating-point number</p></li><li><p>4. Deterministic Finite AutomataOptional + or -Zero or more digits+-###</p></li><li><p>4. Deterministic Finite AutomataOptional + or -Zero or more digitsOptional decimal pointFollowed by zero or more digits.+-#####..</p></li><li><p>4. Deterministic Finite AutomataOptional + or -Zero or more digitsOptional decimal pointFollowed by zero or more digits.#####.Optional exponent E or eFollowed by optional + or -Followed by one or more digits</p><p>.EEee#+-+-##</p></li><li><p>5: Abstract Data TypesInterface for a Queue (a first-in-first-out data structure)#ifndef QUEUE_INCLUDED #define QUEUE_INCLUDED typedef struct Queue_t *Queue_T; </p><p>Queue_T Queue_new(void); int Queue_empty(Queue_T queue); void Queue_add(Queue_T queue, void* item); void* Queue_remove(Queue_T queue); #endif </p></li><li><p>5: Abstract Data TypesAn implementation for a Queue (in queue.c)#include #include #include "queue.h" struct list { void* item; struct list *next; }; struct Queue_t { struct list *head; struct list *tail; }; Why void*?Why declared here and not in queue.h?</p></li><li><p>5: Abstract Data TypesAn implementation for a Queue_newQueue_T Queue_new(void) { Queue_T queue = malloc(sizeof *queue); assert(queue != NULL); queue-&gt;head = NULL; queue-&gt;tail = NULL; return queue; } Implement a check for whether the queue is empty.</p></li><li><p>5: Abstract Data TypesAn implementation for a Queue_emptyint Queue_empty(Queue_T queue) { assert(queue != NULL); return queue-&gt;head == NULL; } </p></li><li><p>5: Abstract Data TypesAn implementation for a Queue_addheadtailnewnodeheadtail</p></li><li><p>5: Abstract Data TypesAn implementation for a Queue_addheadtailnewnodeheadtailNULL</p></li><li><p>Queue_add() Implementationvoid Queue_add(Queue_T queue, void *item) { struct list *newnode; assert(queue != NULL); newnode = (struct list*)malloc(sizeof(*newnode)); assert(newnode != NULL); newnode-&gt;item = item; newnode-&gt;next = NULL; if (queue-&gt;tail == NULL) queue-&gt;head = newnode; else queue-&gt;tail-&gt;next = newnode; queue-&gt;tail = newnode; } </p></li><li><p>5. ADT Common MistakesAdding to the queueImplementing a stack rather than a queueAdding element to the head, rather than the tailNot handling the case where the queue is emptyMissing assert() after call to malloc() for new entryRemoving from the queueMissing assert() when removing an element from an empty queueNot handling removing the last item from the queueNot doing a free() to return space used by the head element</p></li><li><p>Midterm Review(overview of spring 2008 midterm)</p></li><li>Bit-Wise ManipulationsConsider the following code, where k is an unsigned int: printf(%u\n, k ((k &gt;&gt; 2) </li><li><p>What Does This Function Do?char* f(unsigned int n) { int i, numbits = sizeof(unsigned int) * 8; char* ret = (char *) malloc(numbits + 1); for (i=numbits-1; i&gt;=0; i--, n&gt;&gt;=1) ret[i] = 0 + (n &amp; 1); ret[numbits] = \0; return ret; } n = 1900010011</p></li><li><p>Good Bug HuntingConsider this function that converts an integer to a string</p><p>Where the sprintf() function prints to a formatted string, e.g., sprintf(retbuf, %d, 72) places the string 72 starting at the location in memory indicated by the address retbuf: char *itoa(int n) { char retbuf[5]; sprintf(retbuf, %d, n); return retbuf; } Not enough spaceTemporary memory</p></li><li><p>Fixing the Bug: Rewritechar *itoa(int n) { int size = 0; int temp = n; </p><p> /* Count number of decimal digits in n */ while (temp /= 10) size++; size++; </p><p> /* If n is negative, add room for the "-" sign */ if (n &lt; 0) size++; </p></li><li><p>Fixing the Bug: Rewrite /* Allocate space for the string */ char* retbuf = (char *) malloc(size + 1); assert(retbuf != NULL); </p><p> /* Convert the number to a string of digits */ sprintf(retbuf, "%d", n); </p><p> return retbuf; } </p></li><li><p>Preparing for the ExamStudying for the examRead through lecture and precept nodesStudy past midterm examsRead through exercises in the bookTaking the examRead briefly through all questionsStrategize where you spend your timeExam logisticsWednesday 10-10:50am in COS 104Open book, open notes, open mind just no computerNo questions on UNIX tools (e.g., emacs, gcc, gdb, )No Wednesday/Thursday precept this weekHave a great spring break!</p><p>738 (That is, the characters '7', '3', and '8' followed by a newline character.)</p><p>The function prints to stdout the decimal digits that comprise n, in reverse order. </p><p>Common mistakes: treating + as string concatenation, rather than numerical addition.When n=0, the previous version produces a 0. This produces a newline.Reverses the string.</p><p>Common to misunderstand the while-loop idiom, which is true till you reach the \0</p><p>Common idiom of swap.Heap and stack *could* grow in the same direction, but less space efficient.</p><p>Common mistake: that they couldnt grow in the same direction. (They can, its just not as efficient.)The code produces a string of the unsigned integer ns binary representation. The variable numbits is the number of bits needed to represent an unsigned int (i.e., the number of bytes multiplied by 8 bits/byte). The malloc() allocates enough space to store one character for each bit, plus room for the \0 to terminate the string. The for loop starts at the position for the last character and proceeds up to and including the 1st character, assigning the ith character to the ith bit in the unsigned int. The assignment to ret[i] extracts the last bit of the current value of n (which is shifting out the rightmost bit on each iteration of the for loop), and assigns a 0 if the value is 0, and a 1 if the value is 1. Then, the string is terminated with \0 and the function returns the pointer to the string. A common mistake was to assume the code reversed the order of the bits, because the loop counts backwards. The counting backwards is necessary because the n &amp; 1 extracts the last bit, rather than the first, and this bit should appear at the end of the string. A common mistake was to mishandle the case where n is 0, which requires 1 character (rather than 0 characters). </p><p>Another common mistake was to assume that n is an unsigned int, and not allocated space for the minus sign. Another common mistake was to omit the assert(retbuf) after the call to malloc, though no points were taken off for this. Another mistake a few students made was to use sizeof(n) to compute the length; this is incorrect because it returns the number of bytes in a int, not the number of digits in the decimal representation of n. Some students extracted each of the decimal digits of n, using code similar to question 1b (though modified to manipulate n in base 10 rather than base 2). This is perfectly valid (and, as such, no points were taken off), though using sprintf is simpler. An interesting, and clever, answer was to create a large array of characters as a local variable, use sprintf to place the string representation of n in the array, use strlen() to compute the length of the string, use malloc() to allocate the appropriate amount of space to retbuff, and then copy the string from the local variable to retbuf. </p></li></ul>