Exercises

Please complete each exercise or answer the question before reviewing the posted solution comments.

Use the clear command before starting your work on a new problem to ensure that any existing variable name assignments don't interfere with your work on the new problem.

  1. Write a program that asks the user for the distance (in miles) to school and whether it is raining or not and then displays a message according to the table below.

    distancerainingmessage
    less than or equal to 1 milenowalk
    yeswalk
    more than 1 mile and less than 10 milesnobike
    yesbus
    more than 10 milesnobus
    yesbus

    Here's one program that will work. There are many others that will work also.

    % Transportation.m 
    % Author: Deb Deppeler, Oct 30, 2006
    
    % Get the user input
    distance = input('How many miles from school do you live? ');
    raining  = input('Is is raining (1-yes and 0-no) ? ');
    
    % Determine the mode of transportation to get to school
    if distance > 10
        disp('take the bus');
    elseif distance > 1
        if raining == 1
            disp('take the bus');
        else
            disp('ride your bike');
        end
    else
        disp('walk to school');
    end
    

    How would you change this program so that it didn't ask the user if it was raining unless they answered that the distance was greater than 1 mile and less than 10 miles?

  2. Write a program that allows the user to play the game rock, paper, scissors against the computer.

    Here's some pseudo-code to help you get started:

    1. Assign the values 1 to ROCK, 2 to PAPER and 3 to SCISSORS.
    2. Randomly generate a value between 1 and 3.
    3. Ask the user for ROCK, PAPER, or SCISSORS. Hint: have them answer as an integer 1-rock, 2-paper, and 3-scissors.
    4. Check that their answer was between 1 and 3 inclusive.
      • If it was not, display an appropriate error message and exit the program.
    5. Determine winner as indicated by this table.
    6. Display a message that includes what the computer's choice was and wheter the user won or not.
    Computer's
    Choice
    User's
    Choice
    Output Message to Display
    ROCKROCKIt's a tie
    ROCKPAPERYou win, PAPER lays on top of ROCK
    ROCKSCISSORSI win, ROCK breaks SCISSORS
    PAPERROCKI win, PAPERlays on top of ROCK
    PAPERPAPERIt's a tie
    PAPERSCISSORSYou win, SCISSORS cuts PAPER
    SCISSORSROCKYou win, ROCK breaks SCISSORS
    SCISSORSPAPERI win, SCISSORS cuts PAPER
    SCISSORSSCISSORSIt's a tie

    Here's one program that will work. There are many others that will work also.

    % RPS.m
    % Author: Deb Deppeler, Oct 30, 2006
    clear; clc;
    
    % Assign constant values to make program easier to read
    ROCK = 1;
    PAPER = 2;
    SCISSORS = 3; 
    
    % Display intro message to user
    disp( 'Let''s play ROCK-PAPER-SCISSORS ');
    disp( 'Each of us picks ROCK, PAPER, or SCISSORS and I''ll determine who wins.');
    
    % Randomly choose for the computer
    r = randperm(3); % randomly generate values from 1-3
    c = r(1);        % pick the first one for the computer
    disp( 'I have picked my object, now it''s your turn ');
    
    % Get the user's choice
    u = input('Choose 1-ROCK, 2-PAPER, or 3-SCISSORS: ');
    
    % Check for valid choice
    if u < 1 | u > 3
    
        disp('ERROR: Your choice was not 1,2 or 3. You lose.');
    
    else   % If it was valid choice, determine the output message
    
        if c == u
            disp( 'It''s a tie!' );
        elseif c == ROCK & u == PAPER
            disp( 'You win. PAPER lays on top of ROCK.' );
        elseif c == ROCK & u == SCISSORS
            disp( 'I win. ROCK breaks SCISSORS.' );
        elseif c == PAPER & u == ROCK
            disp( 'I win. PAPER lays on top of ROCK.' );
        elseif c == PAPER & u == SCISSORS
            disp( 'You win. SCISSORS cuts PAPER.' );
        elseif c == SCISSORS & u == ROCK
            disp( 'You win. ROCK breaks SCISSORS.' );
        elseif c == SCISSORS & u == PAPER
            disp( 'I win. SCISSORS cuts PAPER.' );
        else
            disp( 'I''m confused.  I didn''t think this could happen.  CHECK CODE' );
        end
    
    end
    

    Note the use of two single quote characters ('') in an output string. Do this when you want to output a single quote character (') as part of a string of characters.

  3. Write a code fragment that displays whether or not the value in a variable named x is a prime number or not. If is not a prime number, also display whether it is even or not.

    Hint: A code fragment is not a complete program, it assumes that other code has been executed first. In this case, it assumes that the variable x has already been assigned a value.

    if prime(x)
       disp( [num2str(x) , ' is a prime number' ] );
    else
       disp( [num2str(x) , ' not a prime number' ] );
       if rem(x,2) == 0
           disp( [num2str(x) , ' is an even number' ] );
       end
    end
    

    The rem() function returns the remainder of dividing the first argument by the second. In this case, it returns the integer remainder of dividing the value in x by 2. This works because if rem(x,2) equals 0 than we know that x is even.