CS 221 Lab 6 Fall 2011: while loops, fprintf, and for loops in MATLAB
Out: 12 October 2011
Due: 20 October 2011
The purpose of lab exercises is for you to learn how the tools
work. The purpose is not for you to type
something into MATLAB. As you go through the
steps of each exercise, think about what you are doing and what the
program is doing, and
try to predict what you will see. If you don't understand
something, ask your instructor -- that's what he's there for.
Part 1: Printing a Vector with a while-loop and fprintf
In this part you will implement a function rangecheck()
that counts and prints values
in a vector that are within a given range, and prints other
symbols for values that are outside the range.
Suppose L is a vector:
10.45 9.99 -1.1 100.39 21.34 0.01 89.77 101
The expression rangecheck(L, 0, 100) should return 5, and produce
10.45 9.99 -- ++ 21.34 0.01 89.77 ++
Each printed value should have exactly two decimal places, and each
value should be in a field 6 characters wide.
- Download and save the file
rangecheck.m to your desktop; it contains
the skeleton of the function to be implemented.
Start MATLAB, and open the file in the MATLAB editor.
This function examines each element of the vector, using a while-loop
(as described in Tuesday's lecture).
Replace the word "REPLACEME" with a valid condition for the
while; the loop
should continue as long as index is less than or equal to the
length of the vector V. (Use the length() function.)
The first part of the body of the loop is an if-elseif-else statement.
You will need to replace "TEST1"
and "TEST2" with the correct boolean expressions.
More precisely: if the element is less than lower,
we want to print two dashes.
If it is greater than upper, we want to
print two plus signs. Otherwise (it is at least lower and at most
upper) we want to print the value itself, and increment count.
(Hint: You don't need any logical operators in either test.)
You will need to fill in the arguments to the fprintf()
function on lines 9, 11 and 13. The comments tell what you
need to print there. You will also need to replace the question mark
"?" in line 13 with an appropriate index expression.
The last line of the body of the while loop is missing; replace the comment
"MISSING LINE HERE" with the assignment statement needed to ensure that the
loop terminates (i.e., eventually the condition becomes false).
The final fprintf() call should print a carriage return so that
the command prompt appears on a new line.
The string '\n' is interpreted by fprintf as a carriage return.
Save the file in a directory on your path.
Now create an array L, that looks like the one in the example above.
Test your program by first typing "L" at the command prompt. Then
type "rangecheck(L,0,100)". If the output doesn't look like it should
(the numbers should line up, with the dashes and plus signs centered
under the numbers they correspond to), adjust the format strings
(first arguments) for fprintf() until the output is correctly formatted.
Test your function on a few other cases by creating different arrays
(or modifying L) and passing them to your rangecheck() function. Show
your TA your completed code, and upload it via the
CS Portal as
as "Lab 6 Part 1".
Part 2: Tutorial on for-loops
Read and perform the steps of the tutorial in Section 4.2.1 beginning
on page 109 of your textbook; stop after the bold-face paragraph on
page 111. Make sure you understand what is happening with the
variables m and a, and can predict what will happen on
the next iteration. (Execute the script multiple times if you need
[Nothing to turn in for this part.]
Part 3: Processing Vectors with for-loops
As described in lecture, the for-loop is a convenient
"shorthand" for the while-loop pattern used to process the elements of
a (one-D) array. For this part you will modify your rangecheck()
function to use a for-loop instead of a while-loop.
Change the name of the function to rangecheck2, and save it to a
Modify the function so it uses the for statement instead
We will use the same variable ("index") for the index as we
walk through the array.
The only lines we need to change are 6, 7 and 16.
Change the word while to for.
Replace the boolean expression of the while with the assignment
statement with index on the left-hand side. The right-hand
side should make the value of index start at 1 and go up
Save the file with these changes, and test it. What happens?
Recall that in a for-loop, MATLAB automatically
increments the index variable, so we don't need to do it ourselves.
Delete line 16. Also, line 6 is redundant, and can be removed.
Make these changes, save the modifications, and re-test the function.
Demonstrate your modified function for the instructor and
upload it via the
CS Portal as
as "Lab 6 Part 3".
Summary: Things You Learned
- How to use a while-loop to iterate a computation over the
elements of an array.
- How to use the built-in function fprintf() to print
How a for-loop works.
- How to use a for-loop to iterate over the elements of an array.