Professional Documents
Culture Documents
Inputs:
1. Evaluate the following expressions and write their results in the
blank:
a) (0 & 0) | (~0 & 0) __false (0)____________
b) ((0 & 0) & (~1 | 0) ) | ( 0 & 1) __false (0)____________
c) (1 & 1) | (~(0 & 1)) __true (1)____________
to learn more about short-circuiting and how they work. Then explain the difference
between the
following expressions:
a) 0 & 1 and 0 && 1 __The first looks at 0 and 1 then compares whether they
are both true. The second looks first at the first term to see if its true. It will also
return 0 but it doesnt bother to look at the term on the right.
b) 1 | 0 and 1 || 0 __The first looks to see whether the left or the right term is
true. The second first looks at the first term to see if its true. If it is (which it is) then
it doesnt even bother to look at the second term.
4. Exclusive OR (XOR)
Let x, y be two Matlab logic variables. Design using ~, &, and |, an expression which
will return
true if and only if only one of the variables is true, and false otherwise. (You may not
use the
build-in xor() function to do this.)
Expression: (x & ~y)|(~x & y)
5. Triangles
The three interior angles of any triangle add up to 180. Complete the program
fragment below
to print scalene (where none of the angles are equal), isosceles (where exactly two
of the angles
are equal), or equilateral (where all three angles are equal) given the three angles.
% assume x, y, z are positive integers that sum to 180
if(_((((x + y) / 2) == x) | (((x + z) / 2) == x) | (((y + z) / 2) == y)) & ((x + y +
z) ~= (3*x) ) % a)
disp('Scalene triangle')
else
if(__((x + y + z) / 3) == x___) % b)
disp('Equilateral triangle')
else
disp('Isoceles triangle')
end
end
6. Triangle Inequalities
Three sticks only form a triangle if the sum of the length of any two sides is greater
than the
length of the third side. A triangle is called a degenerate triangle if it does not
satisfy the above
property. (Click http://www.mathopenref.com/triangleinequality.html for more
information.)
Complete the following script so that it prints Normal if it is possible to form a
triangle (that is
not degenerate) with the three sticks having the length x, y, and z. The script
should print
Degenerate otherwise.
% rand function returns a positive number between 0 and 1
x = rand
y = rand
z = rand
% add your if-elseif-else statements after this comment
if ( ((x + y) > z) && ((x + z) > y) && ((y + z) > x) )
disp(Normal)
else
disp(Degenerate)
end
7. Absolute Value
The absolute value function abs() returns the distance the number is located from 0
in the number
line. This number is always positive. The mathematical definition of the absolute
value function
is
| a | = a if a 0
a, if a < 0
Write your own function called new_abs() that returns the absolute value of a
function using if
statements. (You may not use the build-in abs() to do this.)
function y = new_abs(x)
% add your if-else statement after this comment
if (x >= 0)
y = x;
else
y = (-x);
end
8. Equality?
Let x be a non-zero number. Define a = 1/ (x 1). Then x = 1/a + 1, right? Lets test
this theory in
Matlab. Enter the following in Matlab:
x = 100
a = 1/(x - 1)
y = 1/a + 1
x == y
What is the result? Explain what you think have happened. (Hint: try enter format
long and then
enter the above again)
The result is 0 (false) because when you divide by a to get y, you return 100 but in a
decimal notation (100.0000). To MatLab, the decimal notation matters, so 100.0000
isnt the same thing as 100.
function should display an error message and stop running if the user inputs a letter
that is not
one of the recognized flags.