Lab 8

Repetition: for Loops

CS211 Lab Policy:

Instructions:

You will create one MATLAB program file named lab8.m for this lab.  For each step below, add appropriate MATLAB code and label each step with appropriate comments.    Make sure you test your code for each step before proceeding to the next step.

  1. Begin your program by clearing the command window and displaying your name and "Lab 8".
     
  2. Add a for loop that displays all integers from 1 to 100, one per line, in the command window.  Put the following line after this loop to pause your program:

    Wait = input('Press return to continue', 's');
     
  3. Add a while loop to perform the same task as step 2 (i.e., displays all integers from 1 to 100, one per line, in the command window). (Refer back to the lesson 7 for examples of while loops.)
     
  4. Add a for loop that displays all prime numbers between 1 and 100, one per line, in the command window.  Hint: use MATLAB's isprime() function.
     
  5. Add code that gets two numbers from the user and then displays all integers between those two numbers (inclusive), one per line, in the command window.   The user-entered numbers need not be integers and do not have to be in any particular order (i.e., the larger of the two may come first or second).  For example, if the user enters the numbers 5.1 and 2.3, your program should display 3, 4 and 5.  Hints:  To calculate the correct starting and ending values, use the MATLAB ceil  and floor functions - ceil(n) calculates the nearest integer greater than or equal to n - floor(n) calculates the nearest integer less than or equal to n.
     
  6. Add code that gets from the user a number of scores from 1 to 10.  Validate the user's input (only accept an integer from 1 to 10).  Then, get from the user that number of scores, placing each score into an element in a row vector.  Finally, using MATLAB's mean() function, display the average (mean) of all user-entered scores to two decimal places.  For example, if the user enters 5 for the number of scores and then enters the scores: 90, 75, 88, 93, and 85, your program should create the row vector:
     
    90 75 88 93 85

    and then using the mean() function on that vector, display that the average score is 86.20.  Hint: You can build a vector "on the fly" by adding new elements to the vector with one repeated assignment statement indexing successive elements.
     

  7. Add code that gets from the user a number n of rows/columns from 1 to 10.  Validate the user's input.  (Hint: cut, paste and modify your code from the last problem.)  Then create an n x n matrix with a 1 in the first row and first column, a 2 in the first row and second column, ... , and n2 in the nth row and nth column.  Finally, use the disp function to display your matrix.  For example, if the user enters the number 5, your code should create and display the matrix:

     
     1   2  3  4  5
     6  7  8  9 10
    11 12 13 14 15
    16 17 18 19 20
    21 22 23 24 25


    Hint: There are several ways to solve this problem.  In general, the simpler the solution the better.

Turn-in:

Submit your lab8.m file.