CS 1112 Exercise 2: Conditionals

Due date: Sunday, February 21, 11:00pm EST.

The exercise must be submitted on CMS (Exercise 2).

If you haven’t set up computer (or cloud) storage for all your CS 1112 files yet, do it now! See Announcements for our suggestions. Taking a moment now to organize the few files we have so far will make your life better later when you’ll have many, many files! When downloading a file, do a right-click (Windows) or ctrl-click (Mac) on the file name and select save link as to control exactly where your file will go!

Triangle (modified from M1.2.4 from Insignt)

This is a discussion question--no submission required--so discuss and check your answer with your classmates before the end of the class!

What are the boolean expressions that you need to complete the program fragment below to print “scalene”, “isoceles”, or “equilateral” given three angles?

% Assume variables a, b, and c are given and are positive integers that sum to 180
  
if (____________________________________)
   disp('Scalene triangle')

elseif (____________________________________)
   disp('Equilateral triangle')
else
   disp('Isoceles triangle')
end

Minimum of a quadratic

Download and review the file Eg1_2.m from Insight (our textbook).

[M1.2.5 from Insight] Save the file as myEg1_2.m. Modify myEg1_2 to first check that L is less than or equal to R. Switch the values of L and R if necessary before computing the minimum of the quadratic.

[M1.2.6 from Insight] Modify myEg1_2 some more: Reorder the three branches of the conditional statement to first check whether the critical point is inside the interval.

When do three random sticks make a triangle?

Complete the following script (complete the file sticks.m) so that it prints “Yes” if it is possible to form a triangle with three sticks having length u, v, and w. The script should print “no” if it is impossible.

u= rand()  % a random positive number between 0 and 1
v= rand()
w= rand() 
% Add an appropriate if-statement after this comment...

Which quadrant?

Write three different scripts, angle1, angle2, and angle3 to determine in which quadrant a user-input value of A degrees belongs. A skeleton angle1.m is provided for you; your angle2.m and angle3.m should start with this same provided code. Assume that the user may enter any non-negative number. For example, 725° is the same, and should be treated, as 5°. (Hint: the function rem() that you saw last week might be useful.) To avoid ambiguity, we use the following convention:

Print the result. In angle1 use four separate if statements (four separate if-end constructs—no else or elseif). In angle2, use a single if-elseif-…-else-end construction. In the third script, use nested conditional statements without elseif. Pay close attention to the differences among the three programs—are your boolean expressions as concise as they can be?

Don't lose your work! Be sure to use Save as (instead of Save) when you go from angle1.m to angle2.m and from angle2.m to angle3.

Submit your files myEg1_2.m, sticks.m, angle1.m, angle2.m, and angle3.m on CMS.