\begin{Soln}{2.1} The ``null'' string \LGinlinetrue\LGbegin\lgrinde\L{\LB{\S{}\"\"\SE{}}}\endlgrinde\LGend\LGinlinefalse{} contains one character: the \acro{NUL} \ASCII character. \end{Soln} \begin{Soln}{2.2} \mbox{}\\ \begin{enumerate}[(a)] \item The sizes shown here are for the 68000 system in the laboratory: \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{int}_\V{i};__________________\C{}\1\* a long word-sized integer called \CE{}\V{i}\C{}. \*\1\CE{}}} \L{\LB{\K{unsigned}_\K{char}_\V{uch};______\C{}\1\* a byte-sized variable called \CE{}\V{uch}\C{}. \*\1\CE{}}} \L{\LB{\K{short}_\V{sh};_______________\C{}\1\* a word-sized variable called \CE{}\V{sh}\C{}. \*\1\CE{}}} \L{\LB{\K{unsigned}_\K{int}_\V{ui};________\C{}\1\* a long-word sized variable called \CE{}\V{ui}\C{}. \*\1\CE{}}} \L{\LB{\K{char}_\V{ch};________________\C{}\1\* a byte-sized variable called \CE{}\V{ch}\C{}. \*\1\CE{}}} \endlgrinde\LGend Here are three more definitions, of an \LGinlinetrue\LGbegin\lgrinde\L{\LB{\K{unsigned}_\K{short}}}\endlgrinde\LGend\LGinlinefalse{}, a \LGinlinetrue\LGbegin\lgrinde\L{\LB{\K{long}_\K{int}}}\endlgrinde\LGend\LGinlinefalse{} and an \LGinlinetrue\LGbegin\lgrinde\L{\LB{\K{unsigned}_\K{long}_\K{int}}}\endlgrinde\LGend\LGinlinefalse{}: \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{unsigned}_\K{short}_\V{ush};_____\C{}\1\* a word-sized variable called \CE{}\V{ush}\C{}. \*\1\CE{}}} \L{\LB{\K{long}_\V{li};________________\C{}\1\* a long-word sized variable called \CE{}\V{li}\C{} \*\1\CE{}}} \L{\LB{\K{unsigned}_\K{long}_\V{uli};______\C{}\1\* a long-word sized variable called \CE{}\V{uli}\C{}. \*\1\CE{}}} \endlgrinde\LGend \item For the 68000 system in the laboratory: %\vspace*{0pt}% \begin{tabular}[t]{@{}lccl@{}} \toprule \bf type & \bf size (bits) & \bf size (bytes) & \bf 2's compl/magnitude \\ \midrule integer & 32 & 4 & 2's complement \\ unsigned char & 8 & 1 & magnitude only \\ short integer & 16 & 2 & 2's complement \\ unsigned integer & 32 & 4 & magnitude only \\ char & 8 & 1 & 2's complement\footnotemark \\ \hline unsigned short integer & 16 & 2 & magnitude only \\ long integer & 32 & 4 & 2's complement \\ unsigned long integer & 32 & 4 & magnitude only \\ \bottomrule \end{tabular}% \footnotetext{However the \Kf{char} type can be unsigned by default in some compilers.} \item For \MSDOS: %\vspace*{0pt}% \begin{tabular}[t]{@{}lccl@{}} \toprule \bf type & \bf size (bits) & \bf size (bytes) & \bf 2's compl/magnitude \\ \midrule integer & 16 & 2 & 2's complement \\ unsigned char & 8 & 1 & magnitude only \\ short integer & 16 & 2 & 2's complement \\ unsigned integer & 16 & 2 & magnitude only \\ char & 8 & 1 & 2's complement \\ \hline unsigned short integer & 16 & 2 & magnitude only \\ long integer & 32 & 4 & 2's complement \\ unsigned long integer & 32 & 4 & magnitude only \\ \bottomrule \end{tabular} \item See the table above. ``Magnitude only'' means that the computer uses the unsigned value for the number and so can only hold values~$\ge 0$. ``Two's complement'' means that the computer uses the signed value for that number and so can hold a negative value. \item Here is one \LGinlinetrue\LGbegin\lgrinde\L{\LB{\Proc{printf}\V{printf}()}}\endlgrinde\LGend\LGinlinefalse{} statement for each variable: \LGinlinefalse\medskip\lgrinde \L{\LB{\V{printf}(_\S{}\"The_integer_i_=_\%d\2n\"\SE{},_\V{i}_);}} \L{\LB{\C{}\1\* print the single character \CE{}\V{uch}\C{}: \*\1\CE{}}} \L{\LB{\V{printf}(_\S{}\"The_unsigned_character_uch_=_\%c\2n\"\SE{},_\V{uch}_);}} \L{\LB{\C{}\1\* print the ASCII value of \CE{}\V{uch}\C{}, in decimal: \*\1\CE{}}} \L{\LB{\V{printf}(_\S{}\"The_unsigned_character_uch_=_\%hu\2n\"\SE{},_\V{uch}_);}} \L{\LB{\C{}\1\* print the ASCII value of \CE{}\V{uch}\C{}, in hexadecimal: \*\1\CE{}}} \L{\LB{\V{printf}(_\S{}\"The_unsigned_character_uch_=_\%hx\2n\"\SE{},_\V{uch}_);}} \L{\LB{\V{printf}(_\S{}\"The_short_integer_sh_=_\%hd\2n\"\SE{},_\V{sh}_);}} \L{\LB{\C{}\1\* print the single character \CE{}\V{ch}\C{}: \*\1\CE{}}} \L{\LB{\V{printf}(_\S{}\"The_character_ch_=_\%c\2n\"\SE{},_\V{ch}_);}} \L{\LB{\C{}\1\* print the ASCII value of \CE{}\V{ch}\C{}, in decimal: \*\1\CE{}}} \L{\LB{\V{printf}(_\S{}\"The_character_ch_=_\%hd\2n\"\SE{},_\V{ch}_);}} \L{\LB{\V{printf}(_\S{}\"The_unsigned_integer_ui_=_\%u\2n\"\SE{},_\V{ui}_);}} \L{\LB{\C{}\1\* print the value of \CE{}\V{ush}\C{}, in decimal: \*\1\CE{}}} \L{\LB{\V{printf}(_\S{}\"The_unsigned_short_integer_ush_=_\%hu\2n\"\SE{},_\V{ush}_);}} \L{\LB{\C{}\1\* print the value of \CE{}\V{ush}\C{}, in hexadecimal: \*\1\CE{}}} \L{\LB{\V{printf}(_\S{}\"The_unsigned_short_integer_ush_=_\%hx\2n\"\SE{},_\V{ush}_);}} \L{\LB{\V{printf}(_\S{}\"The_long_integer_li_=_\%ld\2n\"\SE{},_\V{li}_);}} \L{\LB{\C{}\1\* print the value of \CE{}\V{uli}\C{}, in decimal: \*\1\CE{}}} \L{\LB{\V{printf}(_\S{}\"The_unsigned_long_integer_uli_=_\%lu\2n\"\SE{},_\V{uli}_);}} \L{\LB{\C{}\1\* print the value of \CE{}\V{uli}\C{}, in hexadecimal: \*\1\CE{}}} \L{\LB{\V{printf}(_\S{}\"The_unsigned_long_integer_uli_=_\%lx\2n\"\SE{},_\V{uli}_);}} \endlgrinde\relax \end{enumerate} \end{Soln} \begin{Soln}{2.3} \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\V{printf}(_\S{}\"This_is_a_message_from_Nick.\2n\"\SE{}_);}} \L{\LB{___\C{}\1\* Or you could write: \*\1\CE{}}} \L{\LB{___\V{printf}(_\S{}\"\%s\"\SE{},_\S{}\"This_is_a_message_from_Nick.\2n\"\SE{}_);}} \L{\LB{___\C{}\1\* An alternative is to use \CE{}\V{puts}()\C{}. Notice that \CE{}\V{puts}()\C{} gives a newline. \*\1\CE{}}} \L{\LB{___\V{puts}(_\S{}\"This_is_a_message_from_Nick.\"\SE{}_);}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend \index{printf@\Vf{printf}\Of{()}!examples} \index{puts@\Vf{puts}\Of{()} library function|(} \index{library function!\Vf{puts}\Of{()}|(} The library function \LGinlinetrue\LGbegin\lgrinde\L{\LB{\Proc{puts}\V{puts}()}}\endlgrinde\LGend\LGinlinefalse{} is also part of the standard library; I have implemented \LGinlinetrue\LGbegin\lgrinde\L{\LB{\Proc{puts}\V{puts}()}}\endlgrinde\LGend\LGinlinefalse{} on the 68000 system also. Of course it is also available with \qc. The \LGinlinetrue\LGbegin\lgrinde\L{\LB{\Proc{puts}\V{puts}()}}\endlgrinde\LGend\LGinlinefalse{} library function adds a newline character at the end of the string, so I have not put one there in this solution. \index{puts@\Vf{puts}\Of{()} library function|)} \index{library function!\Vf{puts}\Of{()}|)} \end{Soln} \begin{Soln}{2.4} \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{unsigned}_\K{char}_\V{ch}_=_129;}} \L{\LB{___\V{printf}(_\S{}\"The_signed_value_of_ch_is_\%hd;\"\SE{},_\V{ch}_);}} \L{\LB{___\V{printf}(_\S{}\"_the_unsigned_value_is_\%hu\2n\"\SE{},_\V{ch}_);}} \L{\LB{___\V{printf}(_\S{}\"The_hexadecimal_value_of_ch_is_\%hx;\"\SE{},_\V{ch}_);}} \L{\LB{___\V{printf}(_\S{}\"_the_character_value_is_\%c\2n\"\SE{},_\V{ch},_\V{ch}_);}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend The output of this program is: \begin{alltt} The signed value of ch is 129; the unsigned value is 129 The hexadecimal value of ch is 81; the character value is \"u \end{alltt} \end{Soln} \begin{Soln}{2.5} \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{}} \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{i}_=_27;}} \L{\LB{___\K{short}_\V{sh}_=_56;}} \L{\LB{___\K{char}_\V{ch}_=_\S{}\'C\'\SE{};}} \L{\LB{}} \L{\LB{___\V{printf}(_\S{}\"i_=_\%d,_sh_=_\%hd,_ch_=_\%hd\2n\"\SE{},_\V{i},_\V{sh},_\V{ch}_);}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{2.6} \mbox{}\\ \begin{enumerate}[(a)] \item \LGinlinetrue\LGbegin\lgrinde\L{\LB{7_==_6}}\endlgrinde\LGend\LGinlinefalse{} has the integer value 0, which is the logical value \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{false}}}\endlgrinde\LGend\LGinlinefalse{}. \item \LGinlinetrue\LGbegin\lgrinde\L{\LB{5_!=_7}}\endlgrinde\LGend\LGinlinefalse{} has the integer value 1, which is the logical value \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{true}}}\endlgrinde\LGend\LGinlinefalse{}. \item \LGinlinetrue\LGbegin\lgrinde\L{\LB{5_\<_7}}\endlgrinde\LGend\LGinlinefalse{} has the integer value 1, which is the logical value \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{true}}}\endlgrinde\LGend\LGinlinefalse{}. \item \LGinlinetrue\LGbegin\lgrinde\L{\LB{9_\>_0}}\endlgrinde\LGend\LGinlinefalse{} has the integer value 1, which is the logical value \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{true}}}\endlgrinde\LGend\LGinlinefalse{}. \item \LGinlinetrue\LGbegin\lgrinde\L{\LB{5_\<_7_\&\&_9_\<_0}}\endlgrinde\LGend\LGinlinefalse{} has the integer value 0, which is the logical value \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{false}}}\endlgrinde\LGend\LGinlinefalse{}. \item \LGinlinetrue\LGbegin\lgrinde\L{\LB{5_\<_7_\|\,\|_9_\<_0}}\endlgrinde\LGend\LGinlinefalse{} has the integer value 1, which is the logical value \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{true}}}\endlgrinde\LGend\LGinlinefalse{}. \item \LGinlinetrue\LGbegin\lgrinde\L{\LB{10_\>=_0_\&\&_30_\<=_30_\|\,\|_!_(_50_\<_0_)_\&\&_7_\-_3_\>_5}}\endlgrinde\LGend\LGinlinefalse{} \end{enumerate} \end{Soln} \begin{Soln}{2.7} Because the program uses \LGinlinetrue\LGbegin\lgrinde\L{\LB{\Proc{printf}\V{printf}()}}\endlgrinde\LGend\LGinlinefalse{}, we must \LGinlinetrue\LGbegin\lgrinde\L{\LB{\K{\#include}}}\endlgrinde\LGend\LGinlinefalse{} \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{stdio}.\V{h}}}\endlgrinde\LGend\LGinlinefalse{}. \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{}} \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{i};}} \L{\LB{___\K{for}_(_\V{i}_=_10;_\V{i}_\<=_30;_++\V{i}_)}} \L{\LB{______\V{printf}_(_\S{}\"\%d_squared_=_\%d\2n\"\SE{},_\V{i},_\V{i}_\*_\V{i}_);}} \L{\LB{}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{2.8} \LGinlinefalse\LGbegin\lgrinde \L{\LB{\Proc{for}\K{for}_(_\V{i}_=_10;_\V{i}_\<_20;_++\V{i}_)}} \L{\LB{\{}} \L{\LB{___\V{SEVEN\_SEGMENT}_=_\V{pattern}[_\V{SWITCHES}_\&_0xf_];}} \L{\LB{___\V{DelayMs}(_200_);}} \L{\LB{\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{2.9} \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{i}_=_1;}} \L{\LB{___\K{do}}} \L{\LB{___\{}} \L{\LB{______\V{printf}(_\S{}\"i_=_\%d\2n\"\SE{},_\V{i}_);}} \L{\LB{______++\V{i};}} \L{\LB{___\}_\K{while}_(_\V{i}_\<=_10_);}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{2.10} \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{\K{int}_\V{table}[\,]_=_\{_1,_2,_3,_4,_5,_6,_7,_8,_9,_10_\};_\C{}\1\* Initialise at compile time. \*\1\CE{}}} \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{i},_\V{sum}_=_0;}} \L{\LB{___\K{for}_(_\V{i}_=_0;_\V{i}_\<_10;_++\V{i}_)}} \L{\LB{______\V{sum}_=_\V{sum}_+_\V{table}[_\V{i}_];_\C{}\1\* could write: \CE{}\V{sum}_+=_\V{table}[_\V{i}_];\C{} \*\1\CE{}}} \L{\LB{}} \L{\LB{___\V{printf}(_\S{}\"The_sum_of_the_numbers_is_\%d\2n\"\SE{},_\V{sum}_);}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend Here is another solution which assigns the \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{table}[\,]}}\endlgrinde\LGend\LGinlinefalse{} values rather than initialises the array: \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{\K{int}_\V{table}[_10_];}} \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{i},_\V{sum}_=_0;}} \L{\LB{}} \L{\LB{___\K{for}_(_\V{i}_=_0;_\V{i}_\<_10;_++\V{i}_)}} \L{\LB{______\V{table}[_\V{i}_]_=_\V{i}_+_1;_\C{}\1\* Assign at run time. \*\1\CE{}}} \L{\LB{}} \L{\LB{___\K{for}_(_\V{i}_=_0;_\V{i}_\<_10;_++\V{i}_)}} \L{\LB{______\V{sum}_=_\V{sum}_+_\V{table}[_\V{i}_];}} \L{\LB{}} \L{\LB{___\V{printf}(_\S{}\"The_sum_of_the_numbers_is_\%d\2n\"\SE{},_\V{sum}_);}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{2.11} \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<_\V{stdio}.\V{h}\>}} \L{\LB{\K{\#define}_\V{LEN}_10}} \L{\LB{\K{int}_\V{array}[_\V{LEN}_];}} \L{\LB{}} \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{i};}} \L{\LB{___\K{for}_(_\V{i}_=_0;_\V{i}_\<_\V{LEN};_++\V{i}_)}} \L{\LB{______\V{array}[_\V{i}_]_=_\V{i}_+_1;}} \L{\LB{}} \L{\LB{___\K{for}_(_\V{i}_=_0;_\V{i}_\<_\V{LEN};_++\V{i}_)}} \L{\LB{______\V{printf}(_\S{}\"array[_\%d_]_=_\%d\2n\"\SE{},_\V{i},_\V{array}[_\V{i}_]_);}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{2.12} \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{i};}} \L{\LB{___\K{for}_(_\V{i}_=_0;_\V{i}_\<_10;_++\V{i}_)}} \L{\LB{______\V{printf}(_\S{}\"i_=_\%d\2n\"\SE{},_\V{i}_);}} \L{\LB{}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{2.13} The problem is that the index for \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{patterns}[\,]}}\endlgrinde\LGend\LGinlinefalse{}, which is \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{i}}}\endlgrinde\LGend\LGinlinefalse{}, takes the values 1,~2,\ldots,9,~10 instead of the values 0,~1,\ldots,8,~9. There is an array indexing error at each end of the array. By starting with the index~1 instead of~0, the first element in the array is missed. More seriously, the last index used by this program causes the computer to access memory past the end of the array. The value that this memory location holds is unknown. To solve this, change the \LGinlinetrue\LGbegin\lgrinde\L{\LB{\K{for}}}\endlgrinde\LGend\LGinlinefalse{} loop to: \LGinlinefalse\LGbegin\lgrinde \L{\LB{\Proc{for}___\K{for}_(_\V{i}_=_0;_\V{i}_\<_\V{NUMPATTERNS};_++\V{i}_)}} \endlgrinde\LGend Notice that there are \emph{two} changes to this line. There is one other thing about this program: it will display the patterns so quickly on the seven segment display that only the last pattern will be seen. To be able to see all the patterns, it will be necessary to put a delay in the loop. A call to the function \LGinlinetrue\LGbegin\lgrinde\L{\LB{\Proc{Delay}\V{Delay}()}}\endlgrinde\LGend\LGinlinefalse{} given in question~\vref{ex:delay} will do the job. Alternatively you could put a call to the library function \LGinlinetrue\LGbegin\lgrinde\L{\LB{\Proc{DelayMs}\V{DelayMs}()}}\endlgrinde\LGend\LGinlinefalse{} described in section~\vref{sec:delayMs}. The loop would then look something like this: \LGinlinefalse\LGbegin\lgrinde \L{\LB{\Proc{for}___\K{for}_(_\V{i}_=_0;_\V{i}_\<_\V{NUMPATTERNS};_++\V{i}_)}} \L{\LB{___\{}} \L{\LB{______\V{SEVEN\_SEGMENT}_=_\V{patterns}[_\V{i}_];}} \L{\LB{______\V{DelayMs}(_500_);}} \L{\LB{___\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{2.14} \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{void}_\V{Leds}(_\K{unsigned}_\K{char}_\V{pattern}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{i};}} \L{\LB{___\K{for}_(_\V{i}_=_7;_\V{i}_\>=_0;_\-\-\V{i}_)}} \L{\LB{___\{}} \L{\LB{______\K{if}_(_\V{pattern}_\&_(_1_\<\!\!\<_\V{i}_)_)}} \L{\LB{_________\V{printf}(_\S{}\"o\"\SE{}_);}} \L{\LB{______\K{else}}} \L{\LB{_________\V{printf}(_\S{}\".\"\SE{}_);}} \L{\LB{___\}}} \L{\LB{___\V{printf}(_\S{}\"\2n\"\SE{}_);}} \L{\LB{\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{2.15} Refer to figure~\vref{fig:sevenseg}. \LGinlinefalse\medskip\lgrinde \L{\LB{\K{\#define}_\V{a}_1}} \L{\LB{\K{\#define}_\V{b}_2}} \L{\LB{\K{\#define}_\V{c}_4}} \L{\LB{\K{\#define}_\V{d}_8}} \L{\LB{\K{\#define}_\V{e}_0x10}} \L{\LB{\K{\#define}_\V{f}_0x20}} \L{\LB{\K{\#define}_\V{g}_0x40}} \L{\LB{\K{\#define}_\V{DP}_0x80}} \L{\LB{}} \L{\LB{\K{void}_\V{SevenSegment}(_\K{unsigned}_\K{char}_\V{pattern}_)}} \L{\LB{\{}} \L{\LB{___\K{if}_(_\V{pattern}_\&_\V{a}_)}} \L{\LB{______\V{printf}(_\S{}\"_\__\2n\"\SE{}_);}} \L{\LB{___\K{else}}} \L{\LB{______\V{printf}(_\S{}\"____\2n\"\SE{}_);}} \L{\LB{___\K{if}_(_\V{pattern}_\&_\V{f}_)}} \L{\LB{______\V{printf}(_\S{}\"\|\"\SE{}_);}} \L{\LB{___\K{else}}} \L{\LB{______\V{printf}(_\S{}\"_\"\SE{}_);}} \L{\LB{___\K{if}_(_\V{pattern}_\&_\V{g}_)}} \L{\LB{______\V{printf}(_\S{}\"\_\"\SE{}_);}} \L{\LB{___\K{else}}} \L{\LB{______\V{printf}(_\S{}\"_\"\SE{}_);}} \L{\LB{___\K{if}_(_\V{pattern}_\&_\V{b}_)}} \L{\LB{______\V{printf}(_\S{}\"\|\2n\"\SE{}_);}} \L{\LB{___\K{else}}} \L{\LB{______\V{printf}(_\S{}\"__\2n\"\SE{}_);}} \L{\LB{___\K{if}_(_\V{pattern}_\&_\V{e}_)}} \L{\LB{______\V{printf}(_\S{}\"\|\"\SE{}_);}} \L{\LB{___\K{else}}} \L{\LB{______\V{printf}(_\S{}\"_\"\SE{}_);}} \L{\LB{___\K{if}_(_\V{pattern}_\&_\V{d}_)}} \L{\LB{______\V{printf}(_\S{}\"\_\"\SE{}_);}} \L{\LB{___\K{else}}} \L{\LB{______\V{printf}(_\S{}\"_\"\SE{}_);}} \L{\LB{___\K{if}_(_\V{pattern}_\&_\V{c}_)}} \L{\LB{______\V{printf}(_\S{}\"\|\"\SE{}_);}} \L{\LB{___\K{else}}} \L{\LB{______\V{printf}(_\S{}\"_\"\SE{}_);}} \L{\LB{___\K{if}_(_\V{pattern}_\&_\V{DP}_)}} \L{\LB{______\V{printf}(_\S{}\".\2n\"\SE{}_);}} \L{\LB{___\K{else}}} \L{\LB{______\V{printf}(_\S{}\"_\2n\"\SE{}_);}} \L{\LB{\}}} \endlgrinde\relax \end{Soln} \begin{Soln}{2.16} \index{void@\Kf{void}!return type} The first \LGinlinetrue\LGbegin\lgrinde\L{\LB{\K{void}}}\endlgrinde\LGend\LGinlinefalse{} (to the left of the name of the function \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{Delay}}}\endlgrinde\LGend\LGinlinefalse{}) means that the function \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{Delay}()}}\endlgrinde\LGend\LGinlinefalse{} returns no value at all. It also means that we cannot put a \LGinlinetrue\LGbegin\lgrinde\L{\LB{\K{return}}}\endlgrinde\LGend\LGinlinefalse{} statement in \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{Delay}()}}\endlgrinde\LGend\LGinlinefalse{} that returns a value---for example, the compiler would not allow you to put \LGinlinetrue\LGbegin\lgrinde\L{\LB{\K{return}_0;}}\endlgrinde\LGend\LGinlinefalse{} in the body of the \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{Delay}()}}\endlgrinde\LGend\LGinlinefalse{} function. \index{void@\Kf{void}!in parameter list} The second \LGinlinetrue\LGbegin\lgrinde\L{\LB{\K{void}}}\endlgrinde\LGend\LGinlinefalse{}, the one in the parentheses, means that \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{Delay}()}}\endlgrinde\LGend\LGinlinefalse{} takes no parameters. In other words, no additional data is required by the \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{Delay}()}}\endlgrinde\LGend\LGinlinefalse{} function to decide what it should do---it delays by looping a \emph{fixed} number of times. \end{Soln} \begin{Soln}{2.17} \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{}} \L{\LB{\K{int}_\V{FtoC}(_\K{int}_\V{fahrenheit}_)}} \L{\LB{\{}} \L{\LB{___\K{return}_5_\*_(_\V{fahrenheit}_\-_32_)_\1_9;}} \L{\LB{\}}} \endlgrinde\LGend \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{centigrade};}} \L{\LB{}} \L{\LB{___\V{centigrade}_=_\V{FtoC}(_75_);}} \L{\LB{___\V{printf}(_\S{}\"75_degrees_Fahrenheit_is_\%d_Centigrade.\2n\"\SE{},_\V{centigrade}_);}} \L{\LB{}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{2.18} \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \endlgrinde\LGend \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{int}_\V{Max}(_\K{int}_\V{a},_\K{int}_\V{b}_)}} \L{\LB{\{}} \L{\LB{\Proc{if}___\K{if}_(_\V{a}_\>_\V{b}_)}} \L{\LB{______\K{return}_\V{a};}} \L{\LB{___\K{else}}} \L{\LB{______\K{return}_\V{b};}} \L{\LB{\}}} \endlgrinde\LGend \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{i}_=_40;}} \L{\LB{___\K{int}_\V{k}_=_54;}} \L{\LB{___\K{int}_\V{biggest};}} \L{\LB{}} \L{\LB{___\V{biggest}_=_\V{Max}(_\V{i},_\V{k}_);}} \L{\LB{}} \L{\LB{___\V{printf}(_\S{}\"The_biggest_of_i_and_k_is_\%d.\2n\"\SE{},_\V{biggest}_);}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{2.19} Because the program uses \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{printf}()}}\endlgrinde\LGend\LGinlinefalse{}, we must \LGinlinetrue\LGbegin\lgrinde\L{\LB{\K{\#include}}}\endlgrinde\LGend\LGinlinefalse{} \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{stdio}.\V{h}}}\endlgrinde\LGend\LGinlinefalse{}. \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{}} \L{\LB{\K{int}_\V{Square}(_\K{int}_\V{n}_)}} \L{\LB{\{}} \L{\LB{___\K{return}_\V{n}_\*_\V{n};}} \L{\LB{\}}} \endlgrinde\LGend \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{i};}} \L{\LB{\Proc{for}___\K{for}_(_\V{i}_=_10;_\V{i}_\<=_30;_++\V{i}_)}} \L{\LB{\Proc{printf}______\V{printf}_(_\S{}\"\%d_squared_=_\%d\2n\"\SE{},_\V{i},_\V{Square}(_\V{i}_)_);}} \L{\LB{}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend Notice that we can put a function call anywhere inside another function (such as \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{main}()}}\endlgrinde\LGend\LGinlinefalse{} here) where we might use a variable. Compare this solution with that for exercise~\vref{ex:forLoopSquare}. \end{Soln} \begin{Soln}{2.20} Assume that the value of \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{exp}}}\endlgrinde\LGend\LGinlinefalse{} is positive or zero. \LGinlinetrue\LGbegin\lgrinde\L{\LB{\V{Power}()}}\endlgrinde\LGend\LGinlinefalse{} calculates $\Vf{base}^{\Vf{exp}}$. \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{}} \L{\LB{\K{int}_\V{Power}(_\K{int}_\V{base},_\K{int}_\V{exp}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{i},_\V{p}_=_1;}} \L{\LB{\Proc{for}___\K{for}_(_\V{i}_=_0;_\V{i}_\<_\V{exp};_++\V{i}_)}} \L{\LB{______\V{p}_=_\V{p}_\*_\V{base};}} \L{\LB{___\K{return}_\V{p};}} \L{\LB{\}}} \endlgrinde\LGend \LGinlinefalse\LGbegin\lgrinde \L{\LB{\K{int}_\V{main}(_\K{void}_)}} \L{\LB{\{}} \L{\LB{___\K{int}_\V{n}_=_\V{Power}(_2,_15_);}} \L{\LB{}} \L{\LB{___\V{printf}(_\S{}\"2_to_the_power_of_15_is_\%d\2n\"\SE{},_\V{n}_);__\C{}\1\* \CE{}\V{n}\C{} = 32768 \*\1\CE{}}} \L{\LB{___\K{return}_0;}} \L{\LB{\}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{3.1} \begin{enumerate} \item If there are three address line inputs to the address decoder, A$_{23\text{--}21}$, then a new range of addresses is selected when A$_21$ changes. So the lowest address, A00000\hex is active when A${23}$ = 1, A$_{22}$ = 0 and A$_{21}$ = 1. This three-bit number is 101\bin. When this three-bit number is incremented, then the \DUART will cease to be activated. This will happen when A${23}$ = 1, A$_{22}$ = 1 and A$_{21}$ = 0, which (when all other bits are zero) is C00000\hex. The address one less than this is BFFFFF\hex. That is the highest address at which the chip select of the \DUART will be activated. \item There are 16 address lines, A$_{20\text{--}5}$, that are connected neither directly to the \DUART nor to its address decoder. Therefore there are $2^{16} = 65536$ different addresses for each register in the \DUART. \end{enumerate} \end{Soln} \begin{Soln}{3.2} We can write the value 1000\,0000\bin or 80\hex to the port~A control register. We can do so with this statement: \LGinlinefalse\LGbegin\lgrinde \L{\LB{\V{PACR}_=_0x80;}} \endlgrinde\LGend Note that there are many other possible answers, as the `X's in table~\vref{tab:submode1x} can be either 1 or 0; they are ``don't cares.'' \end{Soln} \begin{Soln}{3.3} We can write the same value as for the previous exercise: 1000\,0000\bin or 80\hex, or any of many other possible values. We can set port~B into submode~1X with: \LGinlinefalse\LGbegin\lgrinde \L{\LB{\V{PBCR}_=_0x80;}} \endlgrinde\LGend \end{Soln} \begin{Soln}{3.4} We would write the value 1111\,0000\bin to the port~B data direction register with: \LGinlinefalse\LGbegin\lgrinde \L{\LB{\V{PBDDR}_=_0xf0;_\C{}\1\* Now bits 3.\,.0 of port B are inputs, 7.\,.4 are outputs \*\1\CE{}}} \endlgrinde\LGend \end{Soln} \begin{Soln}{3.5} If the value read from the rows is equal to \LGinlinetrue\LGbegin\lgrinde\L{\LB{0x78}}\endlgrinde\LGend\LGinlinefalse{} then we know that \emph{no} key has been pressed, since each row bit is high; none of the keys have connected a row wire to a column wire---we know this because all the columns have been made low, and if the wires contact each other, the corresponding row will be pulled low. \end{Soln} \begin{Soln}{A.1} In these solutions, the symbol `$\land$' is the mathematical symbol for AND, and the symbol `$\lor$' is the mathematical symbol for OR\e. \begin{enumerate}[(a)] \item \LGinlinetrue\LGbegin\lgrinde\L{\LB{(_3_+_(_4_\*_7_)_)_\-_2}}\endlgrinde\LGend\LGinlinefalse{}\\ This expression has the value 29. \item % 2 & 3 = 2 = 0010\bin % 4 + 7 = 11 = 1011\bin % 2 | 11 = 1011\bin = 11 \LGinlinetrue\LGbegin\lgrinde\L{\LB{(_2_\&_3_)_\|_(_4_+_7_)}}\endlgrinde\LGend\LGinlinefalse{}\\ This expression has the value \begin{align*} (2 \land 3) \lor (4 + 7) &= 2 \lor 11\\ &= 11. \end{align*} \item \LGinlinetrue\LGbegin\lgrinde\L{\LB{1_\<\!\!\<_(_4_+_3_)}}\endlgrinde\LGend\LGinlinefalse{}\\ This expression equals \LGinlinetrue\LGbegin\lgrinde\L{\LB{1_\<\!\!\<_7}}\endlgrinde\LGend\LGinlinefalse{} which is $2^7 = 128$. \item \LGinlinetrue\LGbegin\lgrinde\L{\LB{0x45_\|_(_1_\<\!\!\<_3_)}}\endlgrinde\LGend\LGinlinefalse{}\\ which is equal to $45\hex \lor 2^3 = 45\hex \lor 8 = 4\D\hex$. \item % 12 11 13 12 6 6 \LGinlinetrue\LGbegin\lgrinde\L{\LB{(_(_(_4_+_2_)_\<\!\!\<_(_(_3_\*_2_)_\-_2_)_)_\|_4_)_\|_3}}\endlgrinde\LGend\LGinlinefalse{} \begin{align*} &\phantom{= \mbox{}\,\,}(((4 + 2) <\!\!< ((3 \times 2) - 2)) \lor 4) \lor 3\\ &=((6 <\!\!< (6 - 2)) \lor 4) \lor 3\\ &=((6 <\!\!< 4) \lor 4) \lor 3\\ &=((6 <\!\!< 4) \lor 4) \lor 3\\ % 6 \times 2^4 \lor 4 \lor 3 % 6 \times 16 \lor 4 \lor 3 % 96 \lor 4 \lor 3 % 0110\bin << 4 = 60\hex &= (60\hex \lor 4) \lor 3\\ &= 64\hex \lor 3\\ &= 67\hex. \end{align*} \item \LGinlinetrue\LGbegin\lgrinde\L{\LB{++(_\V{a}[_4_]_)}}\endlgrinde\LGend\LGinlinefalse{} % \vspace{1.5ex} \item % 10 12 12 9 12 13 % @3 < 4 + 7 - 1 == 7 + 3 * 4@ \LGinlinetrue\LGbegin\lgrinde\L{\LB{(_3_\<_(_(_4_+_7_)_\-_1_)_)_==_(_7_+_(_3_\*_4_)_)}}\endlgrinde\LGend\LGinlinefalse{} \begin{align*} &\phantom{= \mbox{}\,\,}(3 < (( 4 + 7 ) - 1 )) == ( 7 + ( 3 \times 4 ) )\\ &=(3 < (11 - 1)) == ( 7 + 12 )\\ &=(3 < 10) == 19\\ &=1 == 19\\ &= 0 \end{align*} \item % 2 12 13 13 % a \%= 4 + 5 * 7 \% 8 \textsf{a \%= ( 4 + ( ( 5 * 7 ) \% 8 ) )}\\ \textsf{a \%= ( 4 + ( 35 \% 8 ) )}\\ \textsf{a \%= ( 4 + 3 )}\\ \textsf{a \%= 7}\\ which has the same value as \textsf{a = a \% 7}. \end{enumerate} \end{Soln}