Lab 8
Repetition:
for Loops
CS211 Lab Policy:
- This lab exercise will not be graded.
- Submit as much as you have completed before the end of the lab period in
which it is assigned.
- If you do not finish this lab work, it is to your advantage to finish it
outside of class. Please re-submit your finished work to the course web
site.
- You may receive help from anyone in completing this lab.
- You may not submit another student's code as part of your
lab.
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.
- Begin your program by clearing the command window and displaying your
name and "Lab 8".
- 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');
- 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.)
- 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.
- 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.
- 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:
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.
- 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.