CS 221 Lab 4 Fall 2011: DeMorgan's Law; More MATLAB

Out: 21 September 2011
Due: 6 October 2011 (one week later than usual due to Lab Quiz next week)

Note: To save your work for later completion, copy/save the spreadsheet to removable media (flash drive) or your locker.


Part 1: More MATLAB Scripts with Conditionals

In last week's lab you created a script to test whether a given quadratic equation had real roots, and if so, to compute those roots. In this exercise you will create a script called areas to compute the areas of different shapes.

The script will prompt the user for the name of a shape (triangle or circle) and then, prompt again for the parameters needed to compute the area: base and height for the triangle, radius for the circle. (Recall that the area of a triangle with base b and height h is ½bh; the area of a circle of radius r is πr².)

  1. Start MATLAB and open the script editor window by selecting "New" -> "Script".

  2. Add a comment (a line beginning with "%") as the first line, giving the script name and purpose. Include your name also in this comment.

  3. First, use the input() function to get one of the strings "triangle" or "circle" from the user, and assign it to a variable called whichshape. The prompt (argument to input()) should say "Which shape - triangle or circle?" Tip: always include a space at the end of the prompt string — it looks better.

    Remember that to read a string from the keyboard, you need to give a second argument (the string 's') to input(). This tells it to treat whatever the user types as a string, and not to try to evaluate it.

  4. Next, depending on what string the user has input (i.e., the value of whichshape), prompt for and read in the appropriate parameter(s), compute the area using the appropriate formula, and assign it to the variable result. You will want to execute exactly one of three alternatives, depending on the value of whichshape:

    Remember to use strcmp() to compare strings!

  5. Use disp() to print "the area is ", followed by the value of result.

  6. When you have completed your script, save it as areas.m. Then test it with several different input strings, including "triangle", "circle", "square", and "123".

  7. When you have it working, demonstrate it for your instructor, then uplaod it as "Lab 4 Part 1" via the online submission page.


Part 2: Truth Tables in Excel

In this exercise you will learn how to construct a truth table for any boolean expression in Excel.

In lecture you have seen DeMorgan's Law, which says how negation (not) "distributes over" (kind of) conjunction ("and", ∧) by changing it to disjunction ("or",∨), and vice versa:

~(A ∧ B) = ~A ∨ ~B
~(A ∨ B) = ~A ∧ ~B
In this exercise you will create a truth table using Boolean operators in Excel. The truth table will show the value of each of the expressions above, along with all of its subexpressions.

  1. Create a new spreadsheet with the following headings in cells A1 through J1: A, B, (A ∧ B), (A ∨ B), ~A, ~B, ~(A ∧ B), (~A ∨ ~B), ~(A ∨ B), (~A ∧ ~B). Your spreadsheet will have 10 columns in all. Make the column headings bold and centered. Also, use the logical symbols "∧" and "∨", not "&" and "|". To get these symbols, select Insert -> Symbol, then click on the symbol you want in the pop-up table. (Make sure the font selected is "Symbol" - you may have to change it in the drop-down menu.)

  2. In A2:B5, fill in all possible combinations of boolean values for A and B: FALSE and FALSE in row 2, FALSE and TRUE in row 3, TRUE and FALSE in row 4, and TRUE and TRUE in row 5. (Notice, again, that there are 2² = 4 possible combinations of values of two variables.)

  3. Now, in C2:J2, fill in the appropriate formula to make each cell contain the value of the expression in the header of that column. For example, C2 should contain:
    =AND(A2,B2)
    
    , and I2 should contain
    =NOT(D2)
    
    In other words, translate the boolean expression in the column header into the corresponding Excel formula, in each column. Use references to other cells to the left to keep the formulas as simple as possible. For example, once you have filled in the formula for the column headed "(A ∧ B)", you can refer to that cell in the same row in the column headed "~(A ∧ B)" — you don't have to repeat the formula for that subexpression. Use the "fill handle" on the cell in row 2 to copy its contents to the three rows below it.
    Note well: make sure you understand the reasoning behind each cell's contents.

  4. Verify that the values in columns G and H are the same, and the values in columns I and J are the same. In other words, the equations given by DeMorgan's laws are true!

  5. Show your instructor the formulas in your completed spreadsheet. Then save and upload it as "Lab 4 Part 2" via the online submission page.

Summary: Things You Learned