Vous êtes sur la page 1sur 4

CS107 Handout #3

J Zelenski April 29, 2011

Midterm practice
Midterm Exam: Friday, May 6th 11am-12:15pm
Nvidia Auditorium (if your last name begins with A-L)
Cubberly Auditorium (if your last name begins with M-Z)

SCPD students: Local SCPD students attend the regular on-campus exam. This ensures
you will be able to ask questions and receive clarifications with the group. Those outside
the Bay Area should arrange for your SCPD site coordinator administer the exam on
Friday. Remote students must send e-mail to our head TA Nate (hardison@stanford.edu)
by Monday May 2nd to initiate arrangements for an on-site exam.

Open book/notes
You may bring textbooks, notes, handouts, code printouts, etc. to refer to during the
exam. No computers, phones, PDAs, or other electronic devices may be used. The exam
will include a list of relevant stdlib function prototypes. We are not expecting you to
memorize minutiae and the exam will not focus on them. However, don't let the "open-
book" nature delude you into not preparing. There will not be time to (re-)learn the
material during the exam. Plan to be facile and knowledgeable enough to readily answer
the questions, only referring to your notes for the occasional detail.

Material
The exam will concentrate on material covered in the labs and assignments. For the
midterm, this means questions that delve into strings, pointers, arrays, function pointers,
low-level memory manipulation, and data representation. There will light coverage of
IA32 assembly.

When we ask you to write C code, we will not be picky about syntax errors/oversights
(missing braces around a block of obviously indented code, forgetting to declare an
integer variable, we don't ask for #include's etc.), but some small subtleties will matter
immensely, e.g., a int** is just one character different than int*, but there is a world of
difference between the two!

Practice
You're unlikely to do well on a test if you don't understand the core concepts, but there is
no guarantee about the inverse. Some students who are quite accomplished in practice
don't manage to demonstrate that same proficiency in the exam setting. Writing code on
paper under time constraints is different than working at the computer. Most students
need practice to adapt their skills. We recommend you sit down with the problems and
write out solutions in longhand. This is much more valuable than a passive review of the
problem alongside its solution where it is too easy to conclude "ah, yes, I would have
done that" only to find yourself sad during the real exam when there is no provided
solution to agree with!
–2–

The rest of this handout is the midterm I gave last quarter in CS107 so you can consider
the questions fairly representative in terms of format, difficulty, and content. To conserve
paper, I cut back on answer space. The real exam will have much more room for your
answers and scratch work. We'll distribute solutions in the next lecture. There are also
many excellent problems in the K&R and B&O texts if you'd like further practice.

Problem 1: C-strings
The Tokenize function below is supposed to tokenize a file into words and return the
count of words and find the longest word. It make the valid assumptions that fp is a file
containing at least one token and that it can chunk words in lengths of at most 30.
Warning: this code is buggy!

// Returns count of words tokenized, stores longest word in param


int Tokenize (FILE *fp, char *longest)
{
char buffer[31], soFar[31];
int count = 0;

while (fscanf(fp, "%s", buffer) == 1) {


count++;
if (strlen(buffer) > strlen(soFar))
strcpy(soFar, buffer);
}

longest = soFar;
return count;
}

This code compiles cleanly, yet has execution errors. The code contains four flaws
concerning its use of memory and string-handling. Identify each and show the changes
necessary to fix the code so it works correctly. You can markup the code above or show
changes below, just be sure your intentions are clear.

Problem 2: Using generic interfaces


Implement the CountSame function that iterates over a map to count each entry whose
value (a string) matches its key. The argument to the function is a CMap storing string
values created using CMapAlloc(sizeof(char*), nBuckets, CorrectFree). You will
need to use CMapMap and write a callback function.

int CountSame(CMap *cm)

Problem 3: Implementing generic functions


Implement the generic function RemoveMax that finds the array element that is largest
according to a client-supplied callback function and removes it from the array. The first
four arguments to RemoveMax are in the style of qsort: the base address of the array, the
–3–

number of elements, the size of each element in bytes, and the comparison callback. The
last argument elemAddr is an address to which the max element is copied before being
removed. The order of the remaining array elements is unchanged and you do not need to
resize the array's storage. The return value is the number of elements remaining in the
array. As an example, RemoveMax on the array {3.7, 9.4, 1.1, -6.2} with an
ordinary float comparison callback changes the array contents to {3.7, 1.1, -6.2},
writes the value 9.4 to location elemAddr, and returns 3.

You may assume that the array has at least one element. If more than one value is tied for
largest, you can choose any one to remove.
int RemoveMax(void *array, int nElems, int elemSz,
int (*cmp)(const void *, const void *), void *elemAddr)

Problem 4: Bits, bytes, and numbers

a) The Mystery function takes a float value and returns a new float computed as below:

float Mystery(float f)
{
unsigned int ui = *(unsigned int *)&f;
ui &= (-1 << 23);
return *(float *)&ui;
}

Consider the possible values for input f: a denormalized float, a normalized float, or an
exceptional float. For each, describe the value of what Mystery returns on such an input.
We answered one for you.

If f is… Mystery(f) returns….


denormalized

normalized

exceptional –inf if sign bit was on in f, +inf if sign bit was off in f
–4–

b) Implement the ContainsConsecutivePair function that takes an unsigned int word


and returns whether at least one pair of consecutive bits within word are both on. We ask
you to write two versions.
Version 1: Use a loop over word and examine neighboring bits one pair at a time.
bool ContainsConsecutivePair (unsigned int word)

Version 2: Use only the bitwise operators and process the entire word at once. no loop.
bool ContainsConsecutivePair (unsigned int word)

Problem 5: IA32 assembly


The following IA32 instructions are the disassembly from the Winky function:
Winky:
push %ebp # function prolog
mov %esp,%ebp

subl $0x8,0x10(%ebp) # first line of C code

mov 0x8(%ebp),%eax # second line of C code


mov 0x10(%ebp), %edx
movl $0x0,(%eax,%edx,4)

mov 0xc(%ebp),%edx # third line of C code


add $0x4,%edx
mov 0xc(%ebp),%eax
mov %edx,(%eax)

pop %ebp # function epilog


ret

Fill in the body of the Winky function with three lines of C code that compile to the above
IA32 instructions. Do not use any typecasts.
struct binky {
int *ptr;
int num;
};

void Winky(short **a, struct binky *b, int c) {

___________________________________________________

___________________________________________________

___________________________________________________
}

Vous aimerez peut-être aussi