( Latest Revision: Tue Oct 08, 2013 )

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:

  1. The program asks the user to enter the number of fence posts s/he desires to see in a drawing of a fence.
  2. The user enters a number.
  3. 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.

There is a common class of problems that programmers encounter when they try to design loops. They are called fence-post problems. This lab is an example of a fence-post problem. We're actually using fence construction to illustrate the idea - the number of posts in a fence is one more than the number of stringers (horizontal sections).

Think about this problem: If your loop writes one vertical line and one horizonal section in each iteration of 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 this:

|---|---|---

or like this:

---|---|---|

On 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.

This illustrates that when a programmer designs code containing a loop, s/he usually has to think about
  1. what the code just prior to the loop should do,
  2. what actions the loop will repeat while iterating,
  3. what will make the iteration stop at the right time, and
  4. what the code immediately after the loop should do.
Often, most or all of these considerations are required in order to solve the problem at hand.

SUGGESTIONS:

Write some code whose job is to draw one layer of a fence. If you write the code so it does these steps:
  1. Write "|" one time (only).
  2. Initialize a loop control variable
  3. Execute a loop that writes "---|" as many times as is necessary.
  4. 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 "vertsMade" to keep track of how many vertical lines have been drawn so far. You could place a statement that initializes vertsMade = 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 vertsMade. The loop should be rigged to continue while vertsMade 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. You must use the following subject lines:

Source Code: "CS1500Lab04Source"

Script: "CS1500Lab04Script"

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.