42S-CS107-Practice-Key

42S-CS107-Practice-Key - CS107 Spring 2007 Handout 42S June...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 42S Spring 2007 June 4, 2007 CS107 Final Exam Practice Problem Solutions Solution 1: Matchmaking /** * The primary assumption is that both boys and girls * are C vectors of dynamically allocated C strings, each * initialized as follows. * * vector boys, girls; * VectorNew(&boys, sizeof(char *), StringFree, 0); * VectorNew(&girls, sizeof(char *), StringFree, 0); * * generateAllCouples creates a new C vector of couples * and inserts one such record on behalf of every possible * mapping of boy to girl. The couples own their own strings, * so that none of the three vectors share any memory whatsoever. * Assume that CoupleFree is the VectorFreeFunction that disposes * of couple records embedded in a vector, and assume it just works. */ typedef struct { char *girl; char *boy; } couple; vector generateAllCouples(vector *boys, vector *girls) { vector couples; VectorNew(&couples, sizeof(couple), CoupleFree, 0); int i, j; couple item; for (int i = 0; i < VectorLength(boys); i++) { for (int j = 0; j < VectorLength(girls); j++) { item.boy = strdup(*(char **) VectorNth(boys, i)); item.girl = strdup(*(char **) VectorNth(girls, j)); VectorAppend(&couples, &item); } } return couples; } Solution 2: Extending the vector typedef struct { void *elems; // pointer to elemsize * alloclength bytes of memory int elemsize; // number of bytes dedicated to each client element int loglength; // number of elements the client is storing int alloclength; // number of elements we have space for VectorFreeFunction free; // applied to elements as they are removed } vector; typedef bool (*VectorSplitFunction)(const void *elemAddr); void VectorSplit(vector *original, vector *thoseThatPass, vector *thoseThatFail, VectorSplitFunction test)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 { int i; void *elem; VectorNew(thoseThatPass, original->elemsize, original->free, 0); VectorNew(thoseThatFail, original->elemsize, original->free, 0); for (i = 0; i < original->loglength; i++) { elem = VectorNth(original, i); VectorAppend((test(elem) ? thoseThatPass : thoseThatFail), elem); } original->loglength = 0; // leave memory there… } Solution 3: Spice Rack class spice { spice *& saffron(spice& salt); short sage(int cumin, spice rosemary) { cumin *= thyme[cumin - *(char *)thyme]; return ((spice *) &parsley)->saffron(rosemary)->parsley - &rosemary; } short thyme[4]; spice *parsley; }; // line 1 R1 = M[SP + 8]; // load old cumin R2 = M[SP + 4]; R3 =.1 M[R2]; // load first byte of this->thyme[0] R4 = R1 – R3; // compute index R5 = R4 * 2; // scale by sizeof(short) R6 = R2 + R5; // compute address of rhs R7 =.2 M[R6]; // compute rhs R8 = R1 * R7; // compute new cumin value M[SP + 8] = R8; // flush to space for cumin // line 2 R10 = M[SP + 4]; // load this (again) R11 = R10 + 8; R12 = SP + 12; // prepare address of rosemary (which gets passed by ref) SP = SP – 8; // make space for params M[SP] = R11; // set down address of receiving object
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 10

42S-CS107-Practice-Key - CS107 Spring 2007 Handout 42S June...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online