\magnification1200
\newif\ifprelim
\prelimtrue
\prelimfalse

\input epsf.def
\def\leaderfill{\leaders\hbox to .5em{\hss.\hss}\hfil}

\def\ap{\par\noindent\hbox to \hsize{\leaderfill}
\par\medskip}

\def\qc#1{`#1'}
\def\\{\char92}
\font\cc=courier at 10pt

\newdimen\hba\hba=\hsize\advance\hba by -60pt

\long\def\hide{\par\setbox0=\vtop\bgroup}
\def\endhide{\egroup\relax%
\par\vrule width 80pt depth .1pt height0pt\par\vskip4pt%
\noindent\unvbox0}

%\long\def\hide#1\endhide{}

\def\lb{\char123 }
\def\rb{ \char125}
\def\vr{\char124}
\def\ci{% C indent
\hskip20pt}

\def\frac#1#2{{{\cc{#1}}\over{\cc{#2}}}}
\newcount\pn\pn=1

\def\newp{%
\ifnum\pn>1 \ifprelim\vskip20pt\else\vfill\eject\fi\fi%
\noindent{\bf \number\pn.}\ \global\advance\pn by1}

\def\code{\vtop\bgroup\bgroup\obeylines\cc\ccode}
\def\ccode#1{#1\egroup\egroup}

\newdimen\AA\AA=9pt
\def\boxit#1{\vbox{\hrule\hbox{\vrule\kern3pt
	\vbox{\kern3pt\hbox{\hskip\AA\hbox to 0pt{\hss#1\hss}\hskip\AA%
\vrule width 0pt height10ptdepth4pt}\kern3pt}\kern3pt}\hrule}}

\def\lboxit#1{\vbox{\hrule\hbox{\vrule\kern3pt
	\vbox{\kern3pt\hbox{\hskip\AA\hbox to 0pt{\hss#1\hss}\hskip\AA%
\vrule width 0pt height10ptdepth4pt}\kern3pt}\kern3pt\vrule}\hrule}}

\centerline{\bf Math 211 Final}
\centerline{\bf May 6, 2002}
\centerline{Professor L.~Taylor}
\vskip20pt
\noindent{\bf Name:\ }$\underline{\hskip4in}$
\bigskip

\newp
Consider the binary tree below whose nodes are labelled.
On the line provided for your answer, write the node{--}labels 
in the order given by the tree order.
The first letter you write should be the 
letter of the smallest node, followed by the next smallest and so on.
The last letter on the line should be the largest node.

\vskip10pt\noindent
Answer\hskip8pt \vrule width .5\hsize height .1pt depth 0pt

\null\hskip1.25in \epsfscale=900\epsfbox{btreeA.eps}%

The binary tree has seven nodes as does the binary tree below
whose nodes are unlabelled.
Label the nodes in this tree so that the tree order for this
tree is the same as your order on the answer line above.
A second blank tree is provided for your scratch work so your
final answer should be legible.

\null\hskip1.25in \epsfscale=900\epsfbox{btree_blank.eps}%
\bigskip
\noindent\vrule width \hsize height .1pt depth 0pt\par
\null\hskip1.25in \epsfscale=900\epsfbox{btree_blank.eps}%

\ap
Recall that the rule is ``everything to the left, then me, then everything to 
the right''.
Let's work from the \qc A.
Since \qc C, \qc D and \qc E are all to the left, they come before \qc A.
Since \qc D and \qc E are to the left of \qc C, \qc C immediately preceeds \qc A.
Since \qc E is to the left of \qc D, \qc E comes after \qc D, so the first part of the
sting is \qc D, \qc E, \qc C, \qc A.

Since \qc B, \qc F and \qc G are all to the right of \qc A, they all come after \qc A.
Both \qc F and \qc G are to the left of \qc B so they both come before \qc B.Since
\qc G is to the right of \qc F, \qc G comes after \qc F, so the whole string is
\medskip
\null\hskip 1in \qc D\quad \qc E\quad \qc C\quad \qc A\quad \qc F\quad \qc G\quad \qc B
\bigskip
For the second part, note there are three nodes to the left of the root and there are
three letters that occur on the line before \qc A.
There are also three letters occurring after \qc A and three nodes to the right of the
root, so \qc A goes at the root. Working left, we have to put \qc D, \qc E and \qc C
at the three nodes to the left of the root. The level-one node has a left node and
a right node, so if we put \qc E at the level-one node and then \qc D at the left hand
level-two node and then \qc C at the level-two right node we get the first part of the
correctly ordered.
The right hand branch is done similarly. The middle of the dtring \qc F, \qc G, \qc B
goes at the level-one node and then \qc F goes to the left at level-two and
\qc B goes to the right.

\newp 
Consider the following short program
\def\tab{\hskip 10pt}

\code{
main(int argc, char *argv[]) \lb
char\tab c;
c=*((*(argv+2))+3);
putchar(c);
\rb
}

\vskip30pt
If you compile this program and run it as
\medskip\noindent
{\cc a.out\tab The\tab quick\tab bown\tab fox\tab jumped}
\medskip\noindent 
what character appears on the screen?
What is the value of {\cc argc}?
\ap
First recall that  {\cc argc} is the number of words on the command line
so in this example  {\cc argc}=6: the {\cc a.out} counts.
The value in {\cc c} takes a bit of work to unravel.
We work through the parentheses working form the inside out:
{\cc argv} is a pointer to an array of pointers, so {\cc argv+2}
points to the third pointer in the array.
This is a pointer to the C-string {\cc quick}.
The {\cc *} just before the opening parenthesis says take the value of that
pointer.
Then we add 3 to that, so it now points to the fourth character of {\cc quick}.
This is an \qc c so the {\cc putchar} puts an \qc c on the screen.
\vskip1in
If instead you ran
\medskip\noindent
{\cc a.out\tab All\tab for\tab one\tab and\tab one\tab for\tab all}
\medskip\noindent 
what is the value in {\cc c} at the end of the program?
\ap
This time, {\cc *(argv+2)} points to the start of the C-string {\cc for};
{\cc (*(argv+2))+3} points to the fourth character in this C-string.
There are only three characters there, but C-strings end with a $0$, so
the value in {\cc c} is $0$.

\newp
Consider the following two functions in C++.
Assume they have been properly declared.
\medskip
\code{
short rr(short v) \lb
return(v*v);
\rb
}
\bigskip
\code{
long rr(long v) \lb
return(2*v);
\rb
}
\bigskip
Discuss what happens when the following two lines execute.
Be sure to include what value is returned by the call to {\cc rr}.
\medskip\noindent
{\cc short ss=5;
\par\noindent long LL=rr(ss);}
\ap
This is an example of C++ function overloading. The program determines
which version of the function is used by the type of the variable passed
to it.
Specifically, {\cc ss} is a short so {\cc rr(ss)} uses the first version of the
function and so {\cc rr} returns $25$. It returns a short which is to be stored in
{\cc LL}, which is a long, but this is an example of the sort of automatic
promotion C and C++ handle for you.
Hence $25$ ends up in {\cc LL}.
\vskip1.5in
Discuss what happens when the following two lines execute.
Be sure to include what value is returned by the call to {\cc rr}.
\medskip\noindent
{\cc long ss=5;
\par\noindent long LL=rr(ss);}
\ap
Now {\cc ss} is a long, so the second version of the function is used and so a
$10$ gets stored in {\cc LL}.

\newp
Finally, a multiple choice question!
Given the declarations
\medskip\noindent
{\cc short i, j, k;}
\medskip\noindent
consider the values in these three variables after the following
statements execute:

\medskip\noindent
{\cc i=k=7; j=2; j*=++k;j\%=i--;i-=k; k/=8;}
\medskip
\settabs 5\columns

\noindent
Which value is the value of {\cc i}?
\+(a) -2 &(b) -1 &(c) 0 &(d) 1 &(e) 2\cr

\medskip
\noindent
Which value is the value of {\cc j}?
\+(a) -2 &(b) -1 &(c) 0 &(d) 1 &(e) 2\cr

\medskip\noindent
Which value is the value of {\cc k}?

\+(a) 0 &(b) 1 &(c) 2 &(d) 3 &(e) 4\cr

\ap
We work through the statements from left to right. 
\medskip\noindent After the first statement
\smallskip\noindent
{\cc i} is $7$; {\cc j} is undefined; and {\cc k} is $7$
\medskip\noindent After the scond statement
\smallskip\noindent
{\cc i} is $7$; {\cc j} is $2$; and {\cc k} is $7$
\medskip\noindent To evaluate the third statement we first increment {\cc k}
so it is now $8$ and then we multiply the value of {\cc j} by $8$:
\smallskip\noindent
{\cc i} is $7$; {\cc j} is $16$; and {\cc k} is $8$
\medskip\noindent To evaluate the fourth statement we calculate the remainder
of {\cc j} when divided by {\cc i}, so {\cc j} is now $2$ and then we decrement {\cc i}
so it is now $8$ and then we multiply the value of {\cc j} by $8$:
\smallskip\noindent
{\cc i} is $6$; {\cc j} is $2$; and {\cc k} is $8$
\medskip\noindent To evaluate the fifth statement we subtract {\cc k} from {\cc i}
\smallskip\noindent
{\cc i} is $-2$; {\cc j} is $2$; and {\cc k} is $8$
\medskip\noindent To evaluate the sixth statement we divide {\cc k} by $8$
\smallskip\noindent
{\cc i} is $-2$; {\cc j} is $2$; and {\cc k} is $1$
\bigskip
Now we read off the answers:
(a), (e) and (b).


\end