Lab 5
Logical & Relational Operations and Logical Functions

CS211 Lab Policy:

Instructions:

You will create one MATLAB program file named lab5.m for this lab.  For each step below, add appropriate MATLAB code and label each step with appropriate comments. Comments should always be placed above (or to the right) of the code they are explaining. You should nicely format all of your output using fprintf. Use the %d format descriptor to display logical values. Note that the fprintf function will display 0 for false and 1 for true and this is OK.

  1. Clear the workspace screen.
     
  2. Logical Operations.  Set the variables, A, B, and C to the logic values:
     

    A = true;

    B = false;

    C = true;

    and then add code that evaluates and displays the logical values (either 0 or 1) for each of the following expressions.
     

    1. the logical AND of A and B
    2. (the logical AND of A and B) combined with the logical OR of C
    3. the logical EXCLUSIVE OR of A and B
    4. the logical NOT of A
       
  3. Relational Operations.  Add code to read in numeric values for X and Y and then determine and display answers to the following questions:
     
    1. Is Y greater than X?
    2. Is X between 10 and 20 (exclusive)?
    3. Is Y greater than or equal to X but not equal to 10?
       
  4. Logical Functions.  Add code to read in a user-entered value of any type.  Note that a user may enter a string (chararacter array) by placing single quotes around the input value.  (Do not use the 's' argument to input() which does not require the quotes but interprets all input as a string.)  Then add code to determine and display answers to the following questions:
     
    1. Is the input value a double array (a number)?
    2. Is the input value a char array (a string)?
    3. Is the input value a logical array (true or false)?
       
  5. Truth Table.  Develop a logical expression that produces true only when A = true, B = false, and C = true.  For any other combination of logical value assignments to A, B, and C, the expression should produce false.  Your expression may use any combination of logical operators (AND, OR, and NOT), but attempt to discover the simplest expression possible.  Add eight(8) set of assignment statements and fprintf statements to your code that displays your expression for every possible combination of values that can be assigned to A, B, and C. For example, if your expression was (A & B & C), your code might look like the code below. (Use copy/paste to avoid lots of typing!)
  6. A = true;

    B = true;

    C = true;

    fprintf('%d & %d & %d produces %d\n', A, B, C, (A & B & C) );

    A = true;

    B = true;

    C = false;

    fprintf('%d & %d & %d produces %d\n', A, B, C, (A & B & C) );

    etc.

  7. Counting logical arrays.  We can simulate the random roll of a 6-sided die by generating a series of random values in the range 1 to 6. The built-in MATLAB function rand generates random values in the range [0.0, 1.0). We can scale and offset these random values to get rolls of a die as follows:
     

    rand(1,100)

     

    % produces an row vector of 100 random values in the range [0.0, 1.0)

     

    rand(1,100) * 6

     

    % produces an row vector of 100 random values in the range [0.0, 6.0)

    floor(rand(1,100) * 6)

    % produces an row vector of 100 random integer values in the range [0, 5]. The floor function converts a floating point number into an integer value by removing any fractional part of the number. Therefore, floor(2.34) is equal to 2, and floor(5.95) is equal to 5.

     

    floor(rand(1,100) * 6) + 1

     

    % produces an row vector of 100 random integer values in the range [1, 6]

Using the code in the algorithm section of lesson 5 as your guide, write code that generates 1000 random rolls of a die and counts the number of times a 6 is rolled. Display your answer to the screen.

  1. Using the method of simulating die rolls from question 5, simulate the roll of two dice 1000 times and count the number of times that "snake eyes" (double 1) is rolled. Display your answer to the screen.

Turn-in:

Submit your lab5.m file.