( Latest Revision: Wed Oct 13, 2010 )

Directions for Lab #5

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.
To help you understand the assignment, take some time now to study this script of sample runs of my solution to the assignment:

fence.script

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. However, 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 that was 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 construct the function so it takes care of the fence post problem:
  1. Write "|" one time.
  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.
For example, you could use a local variable named "postsMade" to keep track of how many posts 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 parameter - the number of posts required to be in the fence.

The main program can prompt the user for the number of posts and read 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 calling MakeOneLayer two times. 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 commond 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 this can cause a loop to become a runaway. Everything should be fine, if you write your 'makeOneLayer' function so that it works correctly for a number of posts between 2 and 18 (inclusive), and you program the main function so it calls makeOneLayer 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_Lab5_Source"

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