[programming] add solutions for parts of exercise, add question
This commit is contained in:
parent
a518043a02
commit
571de4b7f5
@ -14,7 +14,7 @@
|
||||
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
|
||||
\pagestyle{headandfoot} \header{{\bfseries\large Exercise 4
|
||||
}}{{\bfseries\large Boolean Expressions and logical indexing}}{{\bfseries\large 23. Oktober, 2018}}
|
||||
}}{{\bfseries\large Boolean expressions and logical indexing}}{{\bfseries\large 29. Oktober, 2019}}
|
||||
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
|
||||
jan.grewe@uni-tuebingen.de} \runningfooter{}{\thepage}{}
|
||||
|
||||
@ -50,24 +50,75 @@ following pattern: ``variables\_datatypes\_\{lastname\}.m''
|
||||
\verb+y = [5 2 2 6 0 0 2]+. Execute the following commands and explain.
|
||||
\begin{parts}
|
||||
\part \verb+x > y+
|
||||
\begin{solution}
|
||||
Returns a vector of zeros and ones. The vector is one for each
|
||||
position at which the value of x is larger than the
|
||||
respective counterpart in y.
|
||||
\end{solution}
|
||||
\part \verb+y < x+
|
||||
\begin{solution}
|
||||
same result, because we are inverting the expression.
|
||||
\end{solution}
|
||||
\part \verb+x == y+
|
||||
\begin{solution}
|
||||
only 1 for same values in x and y
|
||||
\end{solution}
|
||||
\part \verb+x ~= y+
|
||||
\begin{solution}
|
||||
1 for all indices where x and y values are not the same.
|
||||
\end{solution}
|
||||
\part \verb+x & ~y+
|
||||
\begin{solution}
|
||||
1 only for the indices in which x is ~= 0 AND y is 0.
|
||||
Implicit conversion of the values in x and y to logicals.
|
||||
\end{solution}
|
||||
\part \verb+x | y+
|
||||
\begin{solution}
|
||||
1 at the indices where x or y are ~= 0.
|
||||
\end{solution}
|
||||
\end{parts}
|
||||
|
||||
\question What is the difference between the logical operators
|
||||
\verb+&+ and \verb+&&+ and \verb+|+ and \verb+||+ and why are they
|
||||
useful?
|
||||
\begin{solution}
|
||||
\verb+&&+ and \verb+||+ are called short-ciruit AND and OR. In
|
||||
case of an AND expression, the right-hand side is only evaluated
|
||||
if the left-hand side yields \verb+true+. Similarly for OR, the
|
||||
right-hand side is only evaluated if the left-hand side yields
|
||||
\verb+false+.
|
||||
|
||||
Saves processing time.
|
||||
\end{solution}
|
||||
|
||||
\question Find out what the functions \verb+bitand+ and \verb+bitor+ do.
|
||||
\begin{parts}
|
||||
\part Execute and explain: \verb+bitand(10, 8)+
|
||||
\begin{solution}
|
||||
compares the binary patterns of both numbers with a logican AND
|
||||
and returns the decimal value of the resulting binary pattern.
|
||||
The only bit that is ``on'' for both numbers is the '8', the
|
||||
result is thus '8'.
|
||||
\end{solution}
|
||||
\part Execute and explain: \verb+bitor(10, 8)+
|
||||
\begin{solution}
|
||||
Comparison with a logical OR. 8 and 2 bits are ``on'' in at
|
||||
least one of the two binary patterns, the result will be 10.
|
||||
\end{solution}
|
||||
\end{parts}
|
||||
\item Implement the following Boolean expressions. Test your
|
||||
implementations using random integer
|
||||
numbers for \verb+x+ and \verb+y+ (\verb+randi+).
|
||||
\begin{parts}
|
||||
\part The result should be \verb+true+ if \verb+x+ is greater than \verb+y+ and the sum of \verb+x+ and \verb+y+ is not less than 100.
|
||||
\begin{solution}
|
||||
\code{x > y \& (x+y) >= 100}
|
||||
\end{solution}
|
||||
\part The result should be \verb+true+ if \verb+x+ and \verb+y+ are not equal zero or \verb+x+ and \verb+y+ are equal.
|
||||
\begin{solution}
|
||||
\code{(x ~= 0 \& y ~= 0) | x == y}
|
||||
\end{solution}
|
||||
|
||||
\end{parts}
|
||||
\end{questions}
|
||||
|
||||
|
Reference in New Issue
Block a user