\magnification1200
\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\frac#1#2{{{\cc{#1}}\over{\cc{#2}}}}
\newcount\pn\pn=1

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

\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 Midterm}
\centerline{\bf March 23, 2000}
\centerline{Professor L.~Taylor}
\medskip
\noindent{\bf Name:\ }$\underline{\hskip4in}$
\bigskip

\newp
Consider the following bit of code
\medskip
\hskip40pt\vbox{{\hsize=\hba\cc\obeylines
if( $test_1$ ) \lb $code_1$\rb
else if( $test_2$ ) \lb $code_2$\rb
else if( $test_3$ ) \lb $code_3$\rb
else \lb $code_4$\rb
$code_{after}$
}}
\medskip
If $test_1$ and $test_3$ are false, discuss the two ways the 
program can behave at this point.
What happens if $test_1$ remains false, but $test_3$ is true?

\hide
The two possible cases are that  $test_2$ is true or that it is
false.
If the {\bf program} determines that $test_2$ is true, then the program does $code_2$,
followed by $code_{after}$ {\bf unless} $code_2$ contains a {\cc return} or a {\cc break}
or  other such statement, in which case you cannot really tell what happens without
a more detailed examination of the code.
If $test_2$ is false then we do $code_4$ followed by $code_{after}$ 
{\bf unless $code_4$ ...}.

We have the same two cases for the second part of the question and if
$test_2$ is true, we again do $code_2$, followed by $code_{after}$
{\bf unless $code_2$ ...}.
But if $test_2$ is false, then we do  $code_3$, followed by
$code_{after}${\bf unless $code_3$ ...}.

Notice that it is the running program that determines what happens and if
we make several passes through this bit of code during the course of the program,
the truth value of the test's may change.
The ability to talk (or write) your way through bits of code depending on assumptions
is an important skill.
It comes up in writing the code and especially in debugging.
\endhide

\newp
Define a function {\cc S} with the following code.
Assume {\cc S} has been declared correctly.

\medskip
\hskip40pt\vbox{\hsize=\hba{\cc\obeylines
short S(short x, short y) \lb
return(x-2*y);\rb }}

\vskip 10pt\noindent

\medskip
Discuss the possible values in the {\cc x} and {\cc i}
boxes after the following two statements have executed:

\centerline{\cc i=3; x=S(++i,i--);}

\hide
Several people brought up {\bf precedence} with {\cc x-2*y}.
Precedence can be a source of errors, but the language specifies the precedence
so it will work the same on all compilers. 
YOU (or I) may not remember the precise rules (which is why parentheses are a good
idea) but the compiler does. 
For arithmetic expressions, all of us usually get the precedence right even if we'd
rather not have to write out what the rules are exactly.
Here precedence is a red herring.

There are also possible {\bf scope} issues which are again irrelevant in this example.
The {\cc x} in {\cc x=S(++i,i--);} has nothing to do with the {\cc x}
in the function {\cc x} {\bf because} we start the code for the function with
{\cc short S(short x, short y)} so inside the function {\cc S}, {\cc x} is the name
for the first variable and {\cc y} is the name for the second.

The real issue here is that the language does not specify the order in which
the variables are evaluated.
This is extremely insidious.
As an example, nearly everyone in the class behaved as though evaluation {\bf must}
go from left to right, but it {\bf need} not.
C does however guarantee that once the program begins to evaluate a variable it will
finish that evaluation before moving onto the evaluation of the next.

Once we see the problem, there are two cases, either we evaluate the first variable first
(and then the second) or else we evaluate the second variable first (and then the first).
C does not even guarantee left-to-right OR right-to-left, so when the function has three
variables, there are 6 possibilities.
Question: How many cases are there when the function has 5 variables?

Suppose we evaluate the first variable first.
Then we first do {\cc ++i} which puts a {\cc 4} into the {\cc i} box and sets the
first variable of the function to {\cc 4}.
Then we evaluate the second variable.
There is a {\cc 4} in the {\cc i} box, so the second variable is also {\cc 4}
and then we put a {\cc 3} into the {\cc i} box.
Hence  the {\cc x} box
contains {\cc S(4,4)} which is {\cc -4} and the {\cc i} box contains {\cc 3}.

In the other case, we begin by evaluating the second variable.
This sets the second variable to {\cc 3} and puts a {\cc 2} into the {\cc i} box.
Then we evaluate the first variable.
This first puts a {\cc 3=2+1} into the {\cc i} box and then sets 
the first variable to {\cc 3}.
Hence the {\cc x}
box contains {\cc S(3,3)} which is {\cc -3} and the {\cc i} box contains {\cc 3}.

This order issue is not limited to functions.
With {\cc i=3} still, think about what
{\cc x=(++i)+(i--);} might be.

\endhide

\newp
Declare {\cc xx} and {\cc dd} as {\cc char *xx, dd;}.
After some code executes, {\cc xx} points to the 
box with the {\cc 'A'} in it, with the next few memory locations
filled as indicated.

\medskip
\noindent
\hskip40pt\hbox{\boxit{{\cc 'A'}}\boxit{{\cc 'a'}}\boxit{{\cc
'r'}}\boxit{{\cc 'd'}}%%
\boxit{{\cc 'v'}}\boxit{{\cc 'a'}}\boxit{{\cc 'r'}}\boxit{{\cc 'k'}}\lboxit{{\cc '\\
0'}}}
\medskip
\noindent
Discuss what is in the {\cc dd} box after each of the statements
\itemitem{a.}
{\cc dd=*(++xx);}
\itemitem{b.} {\cc dd=++(*xx);}
\itemitem{c.} {\cc dd=*(xx++);}


What is in the box pointed to by {\cc xx} after each of these
statements?

\hide
In addition to the nine boxes shown, there are two more relevant boxes.
Somewhere there is a box called {\cc xx} which holds the machine address of
the box containing the {\cc 'A'}.
This box is usually the size of four {\cc char} boxes, but its size is irrelevant 
to our discussion.
The second box is a {\cc char} box named {\cc dd}.
(If I want {\cc dd} to also be a {\cc char} pointer, then I have to write
{\cc char *xx, *dd;}.)
The question concerns the values in these two boxes after each of the 3 statements
above executes.

One point you need to bear in mind is that every time you do a {\cc ++} or {\cc --}
the value in some box gets changed - you need to figure out which box and when the
change is made.
A second point deals with precedence and parentheses.
Since we have parentheses, we work from the inside out.

Let's begin with a.
Because of the parentheses, we begin by incrementing {\cc xx},
so the value in the {\cc xx} box is incremented by {\cc 1}. 
Since it is a {\cc char} pointer, it now points to the next box in memory 
which is the first box containing the character {\cc 'a'}. 
Then we take that value and put it in the {\cc dd} box so it now contains {\cc 'a'}. 
This answers both questions for case a.

For b, we first take the character in the box pointed to by {\cc xx}, which is
{\cc 'A'}, and then we increment the value and put it back in the box pointed to
by {\cc xx}.
This time it is now the box pointed to by {\cc xx} that has its value changed.
Our bit of memory now reads 
\medskip
\noindent
\hskip40pt\hbox{\boxit{{\cc 'B'}}\boxit{{\cc 'a'}}\boxit{{\cc
'r'}}\boxit{{\cc 'd'}}%%
\boxit{{\cc 'v'}}\boxit{{\cc 'a'}}\boxit{{\cc 'r'}}\boxit{{\cc 'k'}}\lboxit{{\cc '\\
0'}}}
\medskip
\noindent
The {\cc xx} box points to the {\cc 'B'} and we put that value into the {\cc dd} box, \
so {\cc dd} is now {\cc 'B'}.

Finally, we examine c.
First we take the value pointed to by {\cc xx} and put that into the {\cc dd} box.
Hence {\cc dd} is {\cc 'A'}.
After we do this, we increment {\cc xx} by {\cc 1} so {\cc xx} points to the first
box containing the character {\cc 'a'}.

\endhide



\newp
Consider the following bit of code:
\bigskip
\hskip40pt\vbox{\hsize=\hba\cc\obeylines 
xx=2;
switch(cc)\lb
case 'a': xx+=3;
case 'b': xx\%=3; break;
case 'c': xx-=4;
default: xx=0;
\rb
}
\bigskip

\noindent
What value will {\cc xx} have if {\cc cc='a'}?
How about if {\cc cc='c'}?

\hide
No real issues here, just being careful.
If {\cc cc} is {\cc 'a'} then we execute the code after the {\cc case 'a':}.
In other words, we increment the value in {\cc xx} by {\cc 3}.
Since the value in {\cc xx} was {\cc 2} to begin with, it is now {\cc 5}.
Then, because there is no {\cc break;} we drop through and do the code after the
{\cc case 'b':}.
This sets {\cc xx} to the remainder of what is in there now, after division by {\cc 3}.
Since there is a {\cc 5} now, after this statement, {\cc xx=2}.
Then we continue execution immediately after the {\cc\rb} closing the {\cc switch}
since there is a {\cc break;} after the {\cc xx\%=3;}.

If {\cc cc='c'} then we begin with the code immediately after the {\cc case 'c':}
which does {\cc xx-=4;} so {\cc xx} now has the value {\cc 2-4} or {\cc -2}.
But again there is no {\cc break;} so we fall through to the {\cc default:} code
and this just puts a {\cc 0} into the {\cc xx} box.

\endhide


\newp
Consider the sequence defined recursively by
 $a_n=a_{n-1}+a_{n-2}+a_{n-3}$;
$a_1=1$, $a_2=2$ and $a_3=3$.
Write the code for a function with declaration
{\cc short  A(short n);} which returns the {\cc n}th term
in the sequence, $n\geq 1$, and returns {\cc -1} otherwise.

\hide
There are lots of ways to do this.
They all use {\bf recursion} but there are lots of ways to write the non{--}recursive
part.
Here are some possibilities
\bigskip
\noindent1.\hskip40pt
\vtop{\hsize=\hba{\cc\obeylines
short  A(short n)\lb
if( n>=4) \lb return( A(n-1)+A(n-2)+A(n-3) );\rb
else if( n==3) \lb return (3);\rb
else if( n==2) \lb return (2);\rb
else if( n==1) \lb return (1);\rb
else  \lb return (-1);\rb
\rb}}

\bigskip
\noindent2.\hskip40pt
\vtop{\hsize=\hba{\cc\obeylines
short  A(short n)\lb
if( n==3) \lb return (3);\rb
else if( n==2) \lb return (2);\rb
else if( n==1) \lb return (1);\rb
else if( n>=4) \lb return( A(n-1)+A(n-2)+A(n-3) );\rb
else  \lb return (-1);\rb
\rb}}

\bigskip
\noindent3.\hskip40pt
\vtop{\hsize=\hba{\cc\obeylines
short  A(short n)\lb
if( n>=4) \lb return( A(n-1)+A(n-2)+A(n-3) );\rb
else if( n>=1) \lb return (n);\rb
else  \lb return (-1);\rb
\rb}}

\bigskip
\noindent4.\hskip40pt
\vtop{\hsize=\hba{\cc\obeylines
short  A(short n)\lb
if( n>=4) \lb return( A(n-1)+A(n-2)+A(n-3) );\rb
else if( n==1 || n==2 || n==3 ) \lb return (n);\rb
else  \lb return (-1);\rb
\rb}}

\bigskip
\noindent5.\hskip40pt
\vtop{\hsize=\hba{\cc\obeylines
short  A(short n)\lb
if( n>=4) \lb return( A(n-1)+A(n-2)+A(n-3) );\rb
else \lb switch(n) \lb
\hskip40pt case '3': return 3;break;
\hskip40pt case '2': return 2;break;
\hskip40pt case '1': return 1;break;
\hskip40pt default: return -1;break;
\hskip40pt \rb
\hskip20pt \rb
\rb}}

\bigskip
\noindent6.\hskip40pt
\vtop{\hsize=\hba{\cc\obeylines
short  A(short n)\lb
if( n>=4) \lb return( A(n-1)+A(n-2)+A(n-3) );\rb
else \lb switch(n) \lb
\hskip40pt case '3': case '2': case '1': return n;break;
\hskip40pt default: return -1;break;
\hskip40pt \rb
\hskip20pt \rb
\rb}}

Any of the {\cc n>=4} tests can be replaced by {\cc n> 3} with the same results.
The {\cc n>=4} test in example 2 can be replaced by {\cc n>=3}, {\cc n>=2} or
{\cc n>=1}, or even {\cc n> 0}.

There are also versions that can be done without recursion.
\endhide


\end