You are on page 1of 6

Lab 4 If Statements + Character Array and User

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)____________

2. Suppose x is a logical variable. For each expression below, find an


equivalent expression that uses only one x.
a) x & ~x __0____________
b) x | ~x ___1___________
c) ( x & y ) | ( x & ~y ) where y is another logical variable______~x______

3. Short Circuit Operators


Type: >> help precedence into Matlab. You should see the following output in
Matlab:
[image not copyable]
This is a list of precedence for all of the operators in Matlab. The operators higher
on the list get
evaluated before the lower ones. If you look at the list, multiplication (*) and
division (/) comes
before addition (+) and subtraction (-). There, in the expression 2 + 3 * 4, 3 * 4 is
evaluated
before 2 + 3. Notice the last two items in the list,
9. short-circuit logical AND (&&)
10. short-circuit logical OR (||)

Read this page


(http://www.mathworks.com/help/techdoc/ref/logicaloperatorsshortcircuit.html)

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.

9. Write a function y = middle3(x, y z) that returns the middle of the


three values x, y, z.
function a = middle3(x,y,z)
a = (x + y + z) / 3;

10. Metrics Systems


Although the accepted standard of measurement is the metric system, many
scientists still use
non-standardized units in their experiments. Thus it is very useful to have a function
that can
convert all the measurements into the standardized metric system.
Create a function english_to_metric() that can convert gallons to liters,
pounds to kilograms,
Fahrenheit to Celsius, and yards to meters. Your function should take two inputs x
and y. x will
be a flag that tells the function what you are trying to convert to (let L = convert to
Liters, Kg
= convert to kilograms, C = convert to Celsius, and M = convert to meters) and y
is the value
that needs to be converted. Your function should have one output, the converted
value. Your

function should display an error message and stop running if the user inputs a letter
that is not
one of the recognized flags.

Your function should give identical output as below:


>> z = english_to_metric('L', 64)
z=
242.2664
>> z = english_to_metric('Kg', 100)
z=
45.3593
>> z = english_to_metric('C', 32)
z=
0
>> z = english_to_metric('M', 1)
z=
0.9144
>> z = english_to_metric('D', 50)
Please input correct flag.
z=
[]
See .zip file for function.

You might also like