Radford University ITEC

ITEC120-ibarland (incl. office hrs)infolectureslabshwsjava.lang docsjava.util docs

extra credit homework:
arrays

In the kingdom of Wunhundredtwentia, the jail system is extensive, with 200 cells all in a row. Each cell's door has a key, which when turned toggles the door's lock. That is, if the door was unlocked, then turning the key locks it; if locked, then turning unlocks it.

The Royal Jailer has a curious habit: Every evening, he turns the key in every single door -- numbers 1 through 200. Then, he goes back and turns the key in every other door -- numbers 2, 4, 6, ..., 200. Then, he goes back and turns the key in every third door -- numbers 3, 6, 9, ..., 198. Then, he turns the key in every fourth door -- numbers 4, 8, 12, ..., 200. And so on, until he finishes by turning the key in every 200th door (which is just one door: cell #200). After that, he falls asleep, and anybody in an unlocked cell can escape during the night. (At the start of the evening, all cells are initially locked.)

You and a baker's dozen of your friends have been framed, and falsely convicted of Meandering with Intent to Jay-walk, and are scheduled to be executed at dawn. Fortunately, each of you can name which cell to be locked away in.

Which cells do you choose?


Hints:

For the last two of the 40 possible pts: Once your program tells you the answer, augment the answer with an explanation. Not just what the pattern is, but why that pattern occurs. (This is a nice illustration of the difference between knowing an answer, and understanding the answer.) Make sure your answer is written clearly (you may want to run it by a roommate.)

Scoring This assignment (out of 40) will replace your lowest homework score. (You can, though, get points for both this and for the grue extra credit, replacing your two lowest scores.)
Recall that extra-credit points are graded more demandingly than regular points; be sure to include javadoc, test cases, good variable names and comments, etc..


3

While this skip-a-location approach isn't usually needed for arrays, the numeric properties of the jailer's walk make it convenient in this case.

To think about: What is the extra space-overhead incurred by this approach? Is it significant?

Answer: Well, it's an inefficiency of about 1/200, or 0.5%. This is is negligible, and not worth sacrificing having your code reflect your thinking. (Trying to save that scrap of memory will probably add more to your code-length than you save in data-length.)

     back

ITEC120-ibarland (incl. office hrs)infolectureslabshwsjava.lang docsjava.util docs


©2006, Ian Barland, Radford University
Last modified 2006.Dec.07 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme