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

Solution is locked

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 called vertical(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) and vertical(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) and vertical(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.

  1. Sequence

  2. Selection

  3. Iteration

  4. Subroutine

Solution

Solution is locked