# Midterm Review (overview of fall 2005 midterm)

Post on 08-Jan-2016

31 views

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 PresentationTRANSCRIPT

Midterm Review(overview of fall 2005 midterm)Professor Jennifer RexfordCOS 2171. 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?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?2. Pointers and Stringsvoid f(char *s) { char *p = s; while (*s) s++; for (s--; s>p; s--,p++) { char c = *s; *s = *p; *p = c; } } What does this function do?bar\03. 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? TextDataBSSStackHeap4. 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 number4. Deterministic Finite AutomataOptional + or -Zero or more digits+-###4. Deterministic Finite AutomataOptional + or -Zero or more digitsOptional decimal pointFollowed by zero or more digits.+-#####..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.EEee#+-+-##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; 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 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?5: Abstract Data TypesAn implementation for a Queue_newQueue_T Queue_new(void) { Queue_T queue = malloc(sizeof *queue); assert(queue != NULL); queue->head = NULL; queue->tail = NULL; return queue; } Implement a check for whether the queue is empty.5: Abstract Data TypesAn implementation for a Queue_emptyint Queue_empty(Queue_T queue) { assert(queue != NULL); return queue->head == NULL; } 5: Abstract Data TypesAn implementation for a Queue_addheadtailnewnodeheadtail5: Abstract Data TypesAn implementation for a Queue_addheadtailnewnodeheadtailNULLQueue_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->item = item; newnode->next = NULL; if (queue->tail == NULL) queue->head = newnode; else queue->tail->next = newnode; queue->tail = newnode; } 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 elementMidterm Review(overview of spring 2008 midterm)Bit-Wise ManipulationsConsider the following code, where k is an unsigned int: printf(%u\n, k ((k >> 2) 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>=0; i--, n>>=1) ret[i] = 0 + (n & 1); ret[numbits] = \0; return ret; } n = 1900010011Good Bug HuntingConsider this function that converts an integer to a stringWhere 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 memoryFixing the Bug: Rewritechar *itoa(int n) { int size = 0; int temp = n; /* Count number of decimal digits in n */ while (temp /= 10) size++; size++; /* If n is negative, add room for the "-" sign */ if (n < 0) size++; Fixing the Bug: Rewrite /* Allocate space for the string */ char* retbuf = (char *) malloc(size + 1); assert(retbuf != NULL); /* Convert the number to a string of digits */ sprintf(retbuf, "%d", n); return retbuf; } 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!738 (That is, the characters '7', '3', and '8' followed by a newline character.)The function prints to stdout the decimal digits that comprise n, in reverse order. 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.Common to misunderstand the while-loop idiom, which is true till you reach the \0Common idiom of swap.Heap and stack *could* grow in the same direction, but less space efficient.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 & 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). 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.

Recommended

View more >