CS106A handout2

  • Upload
    lowtec

  • View
    231

  • Download
    0

Embed Size (px)

Citation preview

  • 7/25/2019 CS106A handout2

    1/5

    CS107 Handout #2

    J Zelenski Oct 20, 2014

    Midterm practice

    Midterm Exam: Friday Oct 31st 12:50-2:05pmNVidia Aud (last name begins with A-O)

    200-002 (last name begins with P-Z)

    Please note the exam will be given in two separate locations. You must attend the exam at thelocation assigned to the first letter of your last name please make a note of where to go!

    SCPD students: Local SCPD students attend the regular on-campus exam. Those outside the

    Bay Area should arrange for your SCPD site coordinator to administer the exam on-site to youon Friday the 31st. These remote students must send e-mail to our Head TA Michael

    ([email protected]) by Friday Oct 24to initiate arrangements for an on-site exam. Local

    students should bring a completed routing form to attach to your exam, remote students shouldreturn the completed exam with a routing form. .

    (http://scpd.stanford.edu/generalInformation/pdf/SCPD_HomeworkRouteForm.pdf)

    One page of notes

    The midterm is a closed-book exam. The printed exam will include a list of relevant function

    prototypes and you may bring a single sheet of paper (double-sided, size 8.5" x 11") prepared

    with your own printed and/or hand-written notes. No additional written materials and no

    electronic devices/resources are be used during the exam.

    Material

    Our exams focus on evaluating your mastery of the material that figured prominently in the labsand assignments. For the midterm, this means questions that delve into strings, pointers, arrays,function pointers, low-level memory manipulation, and data representation (e.g. bits, ints, floats).

    The midterm will nottest IA32 assembly.

    The rest of this handout is the midterm I gave most recently in CS107 so you can consider thequestions fairly representative in terms of format, difficulty, and content. To conserve paper, I

    cut back on answer space. The real exam will have more room for your answers and scratch

    work. We'll distribute solutions in the next class meeting. There are also many excellent

    problems in the K&R and B&O texts if you want further practice.

    Further adviceMy manifesto of advice and encouragement on how to prepare for and crush a CS107 exam is onour web site at http://cs107.stanford.edu/advice_exams.html !

  • 7/25/2019 CS106A handout2

    2/5

    2

    Problem 1: C-strings

    a) Write the !"#$#% function that takes a string &"!' and an integer ( and returns a new stringthat moves the first (characters from the front of &"!'to the end. Here are some examples:

    !"#$#%)*+#!$,(*- /0 returns"#!$,(+"!"#$#%)*1!%$2"3#*- 40 returns""3#1!%$2"

    You can assume that (is greater than 0 and less than the word length.

    56$! 7!"#$#%)5"(+# 56$! 7&"!'- ,(# (0

    8

    b) In one sentence, explain how your code above properly null-terminates the result string.

    Problem 2: C generics

    This problem concerns an alternative CMap implementation that uses a single CVector to hold

    all entries instead of a hashtable of buckets. Each CVector element is a 9",'7pointer to a singlekey-value entry. An entry is a heap-allocated "blob" that stores the key characters contiguously

    with the value. Consider a CMap created to associate prices of type '"31:%with grocery items.

    After adding a few entries, the CVector being used by the CMap stores pointers to the blobs isshown below.

    The ;$=?5!%$#%function are shown below. Your code must work with

    this code unmodified.

    #@=%'%A +#!35# 8

    ;B%5#"! 7%(#!,%+C

    +,D%?# 9$:3%+DC

    E ;$::"5)+,D%"A);JF%(#!,%+ I 59%5?5!%$#%)+,D%"A)9",'70- 5$=$5,#@?6,(#- KLMM0C5>JF9$:3%+D I 9$:3%+DC

    !%#3!( 5>C

    E

    e g g s \0 2.75

    b r e a d \0 3.15

    t e a \0 5.75

    0th

    m i l k \0 3.45

  • 7/25/2019 CS106A handout2

    3/5

    3

    A few important notes:

    Each element in the CVector is a pointerto a single blob.

    A blob stores the characters of the key contiguously with the value. If storing values of type

    56$!, 9$:3%+Dwill be 1 byte and the blob for the key "apple" will occupy 7 total bytes.

    The CVector is maintained in alphabetical order by key. Ignore implementing a value cleanup function.

    You are the implementorof CMap, but acting in the role of clientof the CVector. You may

    use only the public features of the CVector.

    The CMap keeps its CVector alphabetically sorted by key to enable efficient lookup. This

    requires an appropriate callback to pass to 59%5?+%$!56and 59%5?+"!#. Implement the necessary

    5>=?%(#!@callback.

    NN 5">=$!$#"! !%#3!(+ (%O$#,9%ND%!"N="+,#,9% #" ,(',5$#% "!'%!,(O "A #&" $!O3>%(#+

    ,(# 5>=?%(#!@)5"(+# 9",' 7$- 5"(+# 9",' 710

    Write the 5>$=_!%=:$5% function to overwrite an existing value for a key. You must use59%5?+%$!56to search for an existing entry and may assume the CVector is sorted according to

    the callback above. You must not allocate or deallocate any memory when replacing a value.

    You may use any of the CVector public functions but you must not call 5>$=?O%#or 5>$=?=3#.

    N7 P3(5#,"(Q 5>$=?!%=:$5%

    7 JJJJJJJJJJJJJJJJJJJJJJ

    7 RA ;%(# &$+ >$'%C A$:+% "#6%!&,+%T

    7N1"": 5>$=?!%=:$5%);- 5"(+# 56$! 72%@- 5"(+# 9",' 7$''!0

    Problem 3: Bits, bytes, and numbers

    a) The >@+#%!@function takes two unsigned values and returns a boolean computed as below.

    1"": >@+#%!@)3(+,O(%' ,(# $- 3(+,O(%' ,(# 10

    8

    !%#3!( )$ V 10 W )$ H 10C

    E

    For which values of1does >@+#%!@)GSX5- 10return #!3%?

    b) Consider all negative ,(#s whose absolute value is an exact power of two (JY, JZ[and so on).What do the bit patterns of these numbers have in common that distinguishes them from all other,(#values?

  • 7/25/2019 CS106A handout2

    4/5

    4

    c) Consider the two following functions to average an array of integers.

    A:"$# $9%!$O%\),(# $!!]^- ,(# (0

    8

    ,(# +3> I GC

    A"! ),(# , I GC , W (C ,__0

    +3> _I $!!],^C

    !%#3!( )A:"$#0+3>N(C

    E

    A:"$# $9%!$O%`),(# $!!]^- ,(# (0

    8

    A:"$# +3> I GTGC

    A"! ),(# , I GC , W (C ,__0

    +3> _I $!!],^C

    !%#3!( +3>N(C

    E

    ,(# (3>+][^ I 8 a - b EC

    =!,(#A)*\ I cA- ` I cAd(*- $9%!$O%\)(3>+- [0- $9%!$O%`)(3>+- [00C

    The underlined expressions above initialize (3>+ to an array for which both $9%!$O%\ and

    $9%!$O%` produce a correct result. Replace the initialization of (3>+with a 2-member array of

    valid integer expressions to produce the following results instead.

    Intialize (3>+to an array for which $9%!$O%\ produces a correct result and $9%!$O%`does not.

    ,(# (3>+][^ I" - #$

    Intialize (3>+to an array for which $9%!$O%` produces a correct result and $9%!$O%\does not.

    ,(# (3>+][^ I" - #$

  • 7/25/2019 CS106A handout2

    5/5

    5

    Library functionsReminder of the prototypes for potentially useful functions. You won't use every function listed. You may also use

    standard C library functions not on this list.

    %&'()* +#!:%(+,-.%* ,/01 2%*13$

    ,/01 2+#!5=@+,/01 24%*5 ,-.%* ,/01 2%1,3$

    ,/01 2+#!(5=@+,/01 24%*5 ,-.%* ,/01 2%1,5 %&'()* .3$,/01 2+#!5$#+,/01 24%*5 ,-.%* ,/01 2%1,3$

    ,/01 2+#!(5$#+,/01 24%*5 ,-.%* ,/01 2%1,5 %&'()* .3$

    ,/01 2+#!56!+,-.%* ,/01 2%5 &.* ,/3$

    ,/01 2+#!!56!+,-.%* ,/01 2%5 &.* ,/3$

    ,/01 2+#!+#!+,-.%* ,/01 2/06%*0,75 ,-.%* ,/01 2.((48(3$

    &.* +#!5>=+,-.%* ,/01 2%95 ,-.%* ,/01 2%:3$

    &.* +#!(5>=+,-.%* ,/01 2%95 ,-.%* ,/01 2%:5 %&'()* .3$

    ,/01 2+#!'3=+,-.%* ,/01 2%3$

    ;-&4 2>$::"5+%&'()* %'3$

    ;-&4 2!%$::"5+;-&4 25=@+;-&4 24%*5 ,-.%* ;-&4 2%1,5 %&'()* .3$

    ;-&4 2>%>>"9%+;-&4 24%*5 ,-.%* ;-&4 2%1,5 %&'()* .3$

    ;-&4 2>%>+%#+;-&4 24%*5 &.* ,/5 %&'()* .3$

    *6?(,*-1 2,;3$

    ;-&4 259%5?(#6+,-.%* >?(,*-1 2,;5 &.* &.4(D3$

    ;-&4 59%5?$==%('+>?(,*-1 2,;5 ,-.%* ;-&4 204413$

    ;-&4 59%5?!%=:$5%+>?(,*-1 2,;5 ,-.%* ;-&4 204415 &.* &.4(D3$

    ;-&4 59%5?+"!#+>?(,*-1 2,;5 >-@-@