Representing algorithms

EasyMediumHard
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
Question 4
Medium

Develop an algorithm using pseudo-code or a flowchart that will move all containers from Bay 0 to Bay 1.

Your algorithm must work for any number of containers initially stacked in Bay 0. You can assume there is at least one container in Bay 0 at the start, and that Bay 1 and Bay 2 are empty.

The vertical order of the containers must be preserved (i.e. the container that was at the bottom of Bay 0 must end up at the bottom of Bay 1, and the container that was at the top of Bay 0 must end up at the top of Bay 1).

You must use the following subroutines:

  • MOVE(fromBay, toBay) — moves the top container from the fromBay to the toBay.
  • SIZE(bay) — returns the number of containers currently in that bay.

For example, if the cargo bays initially look like this:

Diagram showing three cargo bays. Bay 0 contains a vertical stack of three containers labeled from bottom to top: Container A, Container B, and Container C. Bay 1 and Bay 2 are empty.

Then the final arrangement must have the containers in Bay 1 in the exact same vertical order (Container C on top of Container B, which is on top of Container A).

[4]

Representing algorithms Questions

  1. GCSE
  2. /Computer Science
  3. /Representing algorithms