43S-CS107-Final-Solution

43S-CS107-Final-Solution - CS107 Spring 2007 Handou t 43S...

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

View Full Document Right Arrow Icon
CS107 Handout 43S Spring 2007 June 8 th , 2007 CS107 Final Exam Solution Problem 1: Munchies (10 points) Given the following C++ class definition, generate code for the munchy::cheeto method. Assume that the parameters have already been set up for you. Be clear about what code pertains to which line. Recall that C++ references are automatically dereferenced pointers, and k-argument methods are really (k + 1)-argument functions, where the address of the receiving object is passed in as the bottommost parameter. The address of the first instruction of the cheeto method is synonymous with <munchy::cheeto> . You have this and the next page for your code. class munchy { public: char& cheeto(munchy& pretzel, char *sunchip); private: short *pringle; short bugle[2]; char dorito[12]; }; char& munchy::cheeto(munchy& pretzel, char *sunchip) { *(munchy **)sunchip += sunchip[8]; return dorito[pretzel.cheeto(*this, sunchip + 1)]; } Criteria for Line 1 is 3 points (0.5 point deductions, cap at 3) R1 = M[SP + 12]; // grab sunchip R2 =.1 M[R1 + 8]; // load sunchip[8] R3 = R2 * 20; // scale offset, because it’s pointer += against a munchy * R4 = M[R1]; // load *sunchip as if it’s a munchy * R5 = R4 + R3; // compute new value M[R1] = R5; // flush new value over old one Criteria for Line 2 is 7 points (0.5 point and 1.0 deductions, cap at 7) R1 = M[SP + 12]; // grab sunchip again R1 = R1 + 1; // get address of character one beyond sunchip R2 = M[SP + 4]; // get this (which is pointer backing *this) (wrong? 1.0 off) R3 = M[SP + 8]; // pull pointer backing pretzel reference (incorrect? -1.0) SP = SP – 12; // make space for three params M[SP] = R3; M[SP + 4] = R2; M[SP + 8] = R1; CALL <munchy::cheeto> SP = SP + 12; // clean up space for params R4 =.1 M[RV]; // pull char ref’ed by return value (not right? 1.0 off) R5 = M[SP + 4]; // reload this (totally necessary) R6 = R5 + 8; // load base address of dorito array RV = R6 + R4; // load RV with address backing reference (no? 1.0 off) RET; line 1 line 2 SP saved pc pretzel sunchip this cap deductions here at 1.0 points
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 Problem 2: Scheme (15 points) a) (4 points) A derangement is a permutation of the integers 1 through n such that no number k falls in the k th position. So, (4 1 3 5 2) is certainly a permutation, but it’s not a derangement, because the 3 sits in the 3 rd position. (3 5 1 2 6 4) is a derangement of length 6, and (2 3 7 6 1 5 4) is a derangement of length 7. (1 2 3 4 5 6 7 8) is as far as a permutation can be from a derangement. Write a function called derangement? that returns #t if and only if the supplied list of numbers, assumed to be a real permutation of all the integers 1 through the list length, is also a derangement. Use car - cdr recursion. Feel free to write helper functions. ;;; ;; Returns true if and only if the specified permutation (which
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 / 9

43S-CS107-Final-Solution - CS107 Spring 2007 Handou t 43S...

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