Back to Challenge Set 14

Code Challenge: Reverse a List

Difficulty  

divider

The Brief

What they asked for
Here are the stops on my bus route. Print them backwards, for
the trip home. Then, separately, actually reverse the array
itself, so it is stored in the other order and not just
printed that way, and print it again to prove it.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • Start with the stops in an array: Depot, Library, Park, School, Market.
  • Printing backwards is a loop that counts down, Activity 1.19's i--. Work out where it starts and where it stops.
  • Reversing in place is not. Use only what you know: Activity 1.23's three-line swap, done on pairs that mirror each other — the first with the last, the second with the second-to-last.
  • Work out how many swaps that takes. Too many is worse than too few, and running it will show you why.
  • Test an even-length list too: add a sixth stop.

Sample Output

Sample Output
Backwards: Market School Park Library Depot
Reversed: Market School Park Library Depot

Verified in a real run, and again with a sixth stop added. Both lines match because the second one prints the reversed array from front to back.


Submit

Your requirements list and your program file, named cc-reverse-list.

Commence Challenge