Summer 2002: HW7 7.18
Tracing, star patterns
Due
date: 07.22
1. Objectives
Note that
this homework is due a day earlier than usual because of next weeks exam. There is no challenge problem.
Completing all tasks in this
assignment will help you:
·
trace code by hand
·
practice writing nested for loops to
demonstrate various output patterns
First skim,
and then read the entire assignment before starting.
2. Tracing
Trace
the sequence of assignments to the variables made by the program segment given
below, using a new column for each assignment made. The first three assignments
have been done for you. You may not need all the columns provided.
n = 57;
x = 0;
y = 1;
while n ~= 0
if
mod( n, 2 ) ~= 0
n
= floor(n/3);
else
n
= n + 1;
x
= x*9;
y
= y + 10;
end
end
|
n: |
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x: |
|
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
y: |
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
Write your answers in the table supplied (or a reasonable
facsimile) and include it with your other documents.
3. Star Patterns
In the
program segments below, you may only use loops, branches, and the following
types of fprintf statements:
·
fprintf(\n)
·
fprintf(*)
or
fprintf(\\) or fprintf(/)
·
fprintf( ) % use two spaces instead of one: things wont
line up properly otherwise
A) Write a program
segment that prompts
the user for a non-negative integer n then prints an
upside-down
triangle on the screen. For an input of 4, the segment should print
*
* * *
*
* *
*
*
*
B) Write
a segment that prints a square. For an
input of 4, the segment should print
* * * *
* *
* *
*
* * *
C) Write
a segment that prints a square with a diagonal line across its middle. For
input
5, it should print:
* * * * *
*
* *
* *
*
* * *
*
* * * *
D) Finally, write a segment that prints an equilateral triangle. For
an input of 6, the segment
should
print
*
* *
*
* * *
* *
* * * *
* * *
* *
* * * * * * *
* * * * * * *
* * * *
E)* See if you
can write a segment that prints a diamond. For an input of 5, the segment
should
print
/\
/ \
/ \
/ \
/ \
\ /
\ /
\ /
\ /
\/
*Optional
Copy your program segments into a text file called StarPatterns.doc (or one with another file extension if you are not using Word)
and clearly indicate which segment belongs to which part.
4. Submitting your work
Type your
name, student ID, and the date at the top of each required document. Print and
sign each file. Hand the signed
documents to the instructor at the beginning of lecture on Monday 22 July 2002.