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 code fragment that uses an if statement nested inside a while loop to search a data vector for a target value and then displays the index of the target if it is found.

    If the target value is not found, it displays the message "target value not found".

    clear;
    data_vector = [ 0 10 9 34 57 8 32 7 8];  % the vector to search
    target = 8;                              % the target value
    
    index = 1; % index into data_vector, start at the beginning
    while index <= length(data_vector)   % while we haven't reached end of vector
        if data_vector(index) == target  % if we find the target value
            break                        % stop looking
        end
        index = index + 1;               % otherwise, update index and keep looking
    end
    
    % at this point, one of two things has happened:
    % 1) the target value was found and we broke out of the loop with the correct index
    % 2) the target value was not found and we stopped looping when index = length(data_vector) + 1
    
    if index <= length(data_vector)
        disp(['target value ', num2str(target), ' found at index ', num2str(index)])
    else
        disp(['target value ', num2str(target), ' not found'])
    end
    
  2. Suppose Chris' hours for the week are stored in a data file named hours.m. For example, hours.m could contain the values:

    7.25
    9.5
    12.75
    6.5
    8.25

    Write a script that loads the hours and computes Chris' gross pay for the week (using the hours matrix). Chris gets overtime for any time that exceeds 8 hours in one day's shift. Use $7.53 as the hourly pay rate and 1.5 times that for the overtime pay rate.

    clear;
    load hours.m;   % loads hours for the week into the hours variable
    hourly = 7.53;
    overtime = hourly * 1.5;
    gross_pay = 0;
    for i = 1 : length(hours)  % for each day
        
        % if hours worked is 8 or fewer, then use hourly pay rate
        if hours(i) <= 8
            gross_pay = gross_pay + hours(i) * hourly;
        
        % otherwise, the hours worked is more than 8, 
        % and must also compute overtime pay
        else
            gross_pay = gross_pay + 8*hourly + (hours(i) - 8)*overtime;
        end
    end
    disp(['Weekly gross pay is $', num2str(gross_pay)])
    
  3. Write a script that prints an L by H rectangle of asterisks, where L is the length and H is the height and these numbers are defined at the beginning of your script. For example if L is 7 and H is 4, your script should print out:

        *******
        *******
        *******
        ******* 
    H = 4;
    L = 7;
    for r = 1 : H
        stars = [];
        for c = 1 : L
            stars = [stars  '*'];
        end
        disp(stars);
    end
  4. Modify your script from exercise 3 so that it prints out an L by H box of asterisks. For example if L is 7 and H is 4, your script should print out:

    *******
    *     *
    *     *
    ******* 
    H = 4;
    L = 7;
    for r = 1 : H
        stars = [];
        for c = 1 : L
            if r==1 | r==H | c==1 | c==L
                stars = [stars  '*'];
            else
                stars = [stars  ' '];
            end
        end
        disp(stars);
    end
  5. Write a script that prints a L by L right triangle of asterisks, where L is defined at the beginning of your script. For example, if L is 4, your script should print out:

    *
    **
    ***
    **** 
    clear;
    L = 7;  % the length (and height) of the triangle
    
    for r = 1 : L                   % for each row in the triangle
        
        stars = [];
    
        % the number of *'s to display is the same as the row index
        % i.e. row 1 has 1 *, row 2 has 2 *'s, etc.
        for n = 1 : r
            stars = [stars '*'];
        end
        disp(stars);
    end