(
Latest Revision:
Thu Oct 06, 2011
)
Directions for Lab #4
Read about while-loops in our text book before starting to work
on this lab.
Your assignment for this lab is to write a program that works
this way:
- The program asks the user to enter the number of fence posts s/he
desires to see in a drawing of a fence.
- The user enters a number.
- The program then draws a fence having the desired number of fence posts.
To help you understand the assignment, take some time now to study the script
of sample runs of my solution to the assignment:
Click here to see fence.script.
DISCUSSION:
An important part of doing this assignment is to write some C++ source code
that draws one layer of a fence of the desired length. However, the
fence is composed of two layers: an upper layer and a lower layer. Each
layer looks like this example: |---|---|, so a section of
fence looks like this:
|---|---|
|---|---|
(With this kind of fence, you can put your foot up on the lower layer and rest
your elbows on the upper.)
You will need to write a loop. The job of the loop will be to draw (most of)
one layer of a fence. You have to write the code for the loop so that the
desired number of fence posts determines how many times the loop body
executes.
This problem is an example of a fence-post problem. It is very
common to run into a fence-post problem when one is trying to write a loop.
The fence post part of the problem has to do with the fact that the
number of posts in a fence is one more than the number of horizontal
sections.
Think about this problem: If your loop writes one vertical section and one
horizonal section each time through the loop body, then the loop by itself
cannot possibly draw a whole layer of a fence correctly. It would be capable
only of drawing something that looks like:
|---|---|---
or
---|---|---|
One the other hand, if it writes one horizonal section between two vertial
sections each time through the loop body, then the loop would be capable only
of drawing something like this:
|---||---||---|
which, unfortunately, does not make the drawing look the way it is supposed
to look.
SUGGESTIONS:
Write some code whose job is to draw one layer of a fence. If you write the
code so it does these steps:
- Write "|" one time.
- Initialize a loop control variable
- Execute a loop that writes "---|" as many times as is necessary.
- Finally, after the loop is finished, output a newline - to complete the
fence layer and get ready to write whatever is supposed to go on the next
line of output.
then it will solve the fence-post problem for you.
For example, you could use a variable named "postsMade" to keep track of how
many vertical lines have been drawn so far. You could place a statement that
initializes postsMade = 1 after the statement that writes the initial
"|", and before the beginning of the loop. Each execution of the
loop body should write "---|" one time, and add 1 to postsMade. The
loop should be rigged to continue while postsMade is less than the number of
posts required to be in the fence.
The program can begin by prompting the user for the number of posts desired
and then reading in the value. Then it can execute an if-else statement. If
the number of posts is in range, the program should make the two layers of the
fence, by running the loop code twice. Otherwise, it should write an error
message like "Sorry, no can do."
GETTING IT RIGHT:
The point of this lab is to help you learn to write a loop that works
correctly. Fence post problems and runaway loops are common pitfalls.
Plan your lab program as much as possible before the
day you have to perform the lab.
You don't have to write everything out in perfect C++ before the lab
starts. However, you could write a specific step-by-step description of what
your program will do. Write this plan so that it will be easy to translate it
into C++ when the time comes. Write some of the C++ statements in advance if
you anticipate having difficulty with them. In lab, compare plans with your
partner and execute a good compromise plan.
Compile, test, and debug repeatedly until the program is working correctly.
You need to run the program on a lot of different inputs. Try numbers like
-10, -1, 0, 1, 2, 3, 5, 10, 17, 18, 19, and 20.
If you make an error in how you write the loop code, there is a possibility
that your loop will become a "runaway" or "infinite" loop. If your program
writes excessive output or takes excessive time to finish, you can force it
to abort by typing a control-c.
Be careful about letting your loop run when the number of posts "does not make
sense." Sometimes unintended inputs can cause a loop to become a runaway.
Everything should be fine if you write your loop so that it works correctly
for a number of posts between 2 and 18 (inclusive), and you write the
program so it executes the loop only if the number of posts is between
2 and 18 (inclusive).
SENDING IT IN:
Send me a listing (source file) and script by e-mail. Make the script so it
shows you running these inputs: -10, -1, 0, 1, 2, 3, 5, 10, 17, 18, 19, and
20.
Please use the following subject lines:
Source Code: "CS1500_Lab4_Source"
Script: "CS1500_Lab4_Script"
You can refer to
the Hello World! example
for details on how to make and filter a script file, and on how
to e-mail files to me.