Download Handout - Cornell Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Matrix multiplication wikipedia , lookup

Gaussian elimination wikipedia , lookup

Transcript
Lab 7
CS 1109 Summer 2011
July 19
In this lab, you will develop programs that use single and nested loops.
General Lab Instructions
This lab is designed to be completed within the scheduled class meeting time.
There are three deliverables for this lab — a Word document named lab7.doc
(or lab7.docx ) containing your responses to the various questions, as well as two
Matlab script files named matrixPositiveSum.m and oddInts.m (and optionally,
prettyPrint.m). These are to be submitted via CMS1 . If you are unable to complete all of the specified tasks within the allotted time, you are expected to
complete the work on your own and submit the documents by the end of the
day. You are permitted to work with a partner on the lab — however, each
partner must submit his/her own version of the files via CMS. Please answer
the questions posed in the lab document in a clear fashion. Use the section
headers and question numbers (when available) to organize your responses. Remember to include your name, NetID and the name of your collaborator in your
documents.
Nested Loops
A nested loop is a logical structure where one loop (the “inner” loop) is placed
within a second loop (the “outer” loop), as shown in the figure below.
for i = 1 : 20
for j = 1 : 10
% Do something
end
end
Figure 1: Nested for-loops
Thus, each iteration of the outer loop causes one complete execution of the inner loop. Once the inner loop finishes running as many times as specified, the
outer loop starts on the second iteration, triggering another complete execution
of the inner loop and so on. In general, loops may be nested any number of
levels deep. You can also mix-and-match while and for-loops to create very
rich behaviors.
Task: In the example above, how many times is the body of the nested loop
executed? In general, if the bounds on the outer loop were from 1 : m and the
bounds on the inner loop were from 1 : n, how many times would the loop body
be executed? Records your answers in lab7.doc.
1 http://cms.csuglab.cornell.edu
1
CS 1109 Summer 2011
Lab 7
July 19
Walking Through Matrices
One of the most common usage patterns of nested loops is for performing operations on every element of a matrix (two-dimensional array). In this part, you
will design a program that computes the sum of every number greater than 0.5
in a randomly generated 10 × 10 matrix.
You can generate a random 10 × 10 matrix (call it m) by using the rand(10)
statement. We can decompose the task of summing up all the numbers greater
than 0.5 in m as follows:
Sum the numbers greater than 0.5 in the first row
To the previous sum, add the sum of the numbers greater than 0.5 in the second
row
..
.
To the previous sum, add the sum of the numbers greater than 0.5 in the tenth
row
This can be refined as follows:
i=1
Sum the numbers greater than 0.5 in row i
i=2
To the previous sum, add the sum of the numbers greater than 0.5 in row i
..
.
i = 10
To the previous sum, add the sum of the numbers greater than 0.5 in row i
Note that as per this solution sketch, we need to repeat the act of summing up
all the numbers greater than 0.5 in a given row of a matrix, which in itself is
an iterative process (“visit each element in the row and add it to the sum if it
is greater than 0.5”). This is a classic scenario demanding a nested loop.
Task: Using the above solution roadmap, write a script named matrixPositiveSum.m that generates a random 10 × 10 matrix and computes the sum of
all the numbers greater than 0.5 in it.
Printing Odd Integers
Task: Consider the task of printing all the odd integers from 1 up to and
including some number n, where the value n is user supplied. This can be accomplished using a single loop. Begin by writing a program that solves this task
in a script named oddInts.m.
Next, modify oddInts.m such that it prompts the user repeatedly to enter a value
2
CS 1109 Summer 2011
Lab 7
July 19
for n. After each user input, your script should print all the odd numbers up to
(and including) n and prompt the user again. Your script should terminate if
the user enters a non-positive value for n. You only need to submit the modified
version of the oddInts.m script for this task.
Challenge Problem (optional, but highly recommended)
For a given positive integer n >= 3, we define the “Pretty Print” pattern of
size n as an asterisk (‘*’) square of side n, with its left-diagonal filled in. For
example, if n = 6, the corresponding Pretty Print pattern is:
******
**
*
* * *
* * *
*
**
******
Task: Write a script named prettyPrint.m that prompts the user to enter a
number n and prints out the corresponding Pretty Print pattern. (Hint: The
most compact and elegant solution uses a nested loop. Think of each asterisk as
living in its own box in an n × n matrix. Look at the indices of the boxes that
have asterisks in them — can you spot any patterns?
Submission
Please submit the files lab7.doc, matrixPositiveSum.m and oddInts.m (and optionally, prettyPrint.m) via CMS. Note that CIT machines delete any new files
that are created by users when they are rebooted. To keep a copy of your
submission for your records, always remember to email yourself a copy of your
submission files.
Total task count for this lab: 3(4)
3