6.2. Draw a Box: Algorithm#
6.2.1. Problem Description#
Write a program that asks the user for an integer \(n\) and then prints a square box where the edges of the box are of length \(n\) (as shown in the examples below).
Example 1: \(n=1\)
n: 1
+ - +
| |
+ - +
Example 2: \(n=2\)
n: 2
+ - - +
| |
| |
+ - - +
Example 3: \(n=5\)
n: 5
+ - - - - - +
| |
| |
| |
| |
| |
+ - - - - - +
Note:
There is a space between each
-in the top and bottom edge. This means there are \(n\) dashes and \(n+1\) spaces between each+on the top and bottom edges, i.e. \(2n+1\) characters.There are \(2n+1\) spaces between each
|for the vertical edges.
6.2.2. Class/Homework Exercises#
What you need:
Blank A4 or A3 paper - a couple of sheets per group
Pens - one for each student
Using pen and paper complete the following questions. If this is being completed as a class activity it is recommended that students work in groups.
Question 1
First let’s focus on drawing the top and bottom edges of the squares. Think about how you would create a subroutine called
horizontal(n)which will display a horizontal edge. This subroutine should be a procedure, which means it’s a function that doesn’t have a return value.Example: \(n=1\)
+ - +Example: \(n=5\)
+ - - - - - +Draw a flowchart to represent this subroutine. Your answer must use iteration.
Solution
![]()
There may be variations in solutions, but the key things to check are:
Terminal nodes are ovals
Terminal nodes contain
horizontal(n)The display node is a parallelogram (the most common answer should only have one display command)
Remaining process are rectangles
The flow chart makes use of iteration
The iteration condition is in a diamond
The arrows leaving the diamond are appropriately labeled either True/False or Yes/No
All lines have arrows pointing in the appropriate direction
The procedure produces the correct result
The procedure does not have a return statement
Question 2
Write the pseudocode that corresponds to the flowchart you drew for Question 1.
Solution
Solution is locked
Question 3
Now let’s focus on drawing the side edges of the square. For each row in the square there are \(2n+1\) spaces between each
|. Think about how you would create a subroutine calledvertical(n)which will display one row for the vertical edges. This subroutine should be a procedure, which means it’s a function that doesn’t have a return value.Example: \(n = 1\), 3 spaces between each
|| |Example: \(n = 5\), 11 spaces between each
|| |Draw a flowchart to represent this subroutine. Your answer must use iteration.
Solution
Solution is locked
Question 4
Write the pseudocode that corresponds to the flowchart you drew for Question 3.
Solution
Solution is locked
Question 5
Now let’s put it all together. Think about how you would write the main program, which uses
horizontal(n)andvertical(n)to read in a number \(n\) from the user and then display a box of the appropriate size.Example 1: \(n=1\)
n: 1 + - + | | + - +Example 2: \(n=2\)
n: 2 + - - + | | | | + - - +Example 3: \(n=5\)
n: 5 + - - - - - + | | | | | | | | | | + - - - - - +Draw a flowchart to represent your main program. Your answer must call
horizontal(n)andvertical(n).Solution
Solution is locked
Question 6
Write the pseudocode that corresponds to the flowchart you drew for Question 5.
Solution
Solution is locked
Question 7
Consider the entire algorithm you have put together to to solve the Draw a Box problem. Which of the following are used in the algorithm? Select all that apply.
Sequence
Selection
Iteration
Subroutine
Solution
Solution is locked
