(Latest Revision: 10/28/2001)

Directions for Lab #6

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 in a fence.
  2. The user enters a number.
  3. The program then "draws" a fence having that same number of posts.
Have a look at the file named fence.script for sample runs of my version of this program.

DISCUSSION:

The heart of this assignment is to write some code that inputs a number of fence posts and draws one layer of a fence. The fence is composed of two layers: an upper layer and a lower layer. Each layer looks like this example: |---|---|

You will need to write a loop for this program. The job of the loop will be to draw (most of) one layer of a fence. When the program executes, the number of posts input by the user will determine how many times the loop body will execute. You have to write the loop so that the loop body is guaranteed to execute the correct number of times, according to the input of the user.

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. Suppose that your loop writes one post and one horizonal section each time through the loop body. In that case, 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

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

SUGGESTIONS:

Write a function called "makeOneLayer" whose job is to draw one layer of a fence. Let the number of posts be a parameter of makeOneLayer.

Here is one way to take care of the fence post problem:

  1. Write "|" one time.
  2. Enter a loop that writes "---|" as many times as is necessary.
Use a local variable to control the loop. For example, you could use a local variable named "sections" to keep track of how many of these strings: ---| have been written. You could initialize sections = 0 before entering the loop. The loop could add 1 to sections each time through the loop body, and the loop could be rigged to stop when sections reaches some limit (determined by how many posts there are supposed to be).

In your main program, you can prompt the user for the number of posts. You can have an if-statement that causes makeOneLayer to be called twice if the number of posts given is reasonable. You can use an else-statment with the if-statement to make the program write a message like "Sorry, no can do." if the number of posts is too low or too high.

GETTING IT RIGHT:

Plan your lab program during the week. 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 very careful about letting your loop run when the number of posts "does not make sense." Sometimes this can cause a loop to become a runaway. Look at your loop and think about what it may do if the number of posts is 1, 0, or some negative number. Would it become an infinite loop? Is there something simple you can do to prevent this from happening, even if the user decides to enter a "foolish" number? Try to write the program in a way that defends against such a problem. Test your defenses by inputting numbers of posts such as -1, 0 and 40. Remember: use control-c if you have to abort.


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_Lab6_Source"

Script: "CS1500_Lab6_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.