On 2/20/2012 9:08 AM, bagratte wrote:
> [reformatted for legibility]
>
> so the question is: is there any chance to solve a generic towers of
> hanoi iteratively not using any kind of arrays, stacks, bit-wise wizardry etc.?
>
> actually the problem is much wider than c language, but i just didn't know
> where to post. the question could be paraphrased as followed: are there any
> fixed number of variables by which one could represent the state of towers
> of hanoi?
Yes, of course. As a point of attack on the wider question,
consider doing array-like things without using arrays. Instead of
an array x[4], you could have free-standing variables x0,x1,x2,x3.
You could then write
/* Instead of x[i] = y */
switch (i) {
case 0: x0 = y; break;
case 1: x1 = y; break;
case 2: x2 = y; break;
case 3: x3 = y; break;
}
/* Instead of y = x[i] */
switch (i) {
case 0: y = x0; break;
case 1: y = x1; break;
case 2: y = x3; break;
case 4: y = x4; break;
}
The stupidity of this transformation isn't important; it just
demonstrates in a sort of "existence proof" way that one could, at
least in theory, perform array-like operations without arrays.
Once you have array operations, you can go on to build all sorts
of other data structures atop them: Stacks, lists, queues, trees,
whatever you like. (Again, think of this as an existence proof,
not as practical programming.)
Here's another existence-proof-ish demonstration that a fixed
number of variables suffices: The actual machine on which you run
your program -- any program at all -- is finite and can manage only
a fixed number of variables. (Yes, even if you "swap" variables to
I/O devices: You still need to keep track of what's where, and your
resources for record-keeping are finite.) The number of variables
a program can manage is huge, but cannot exceed some very large but
fixed limit. Ergo, QED, reductio ad nauseam, left as an exercise
for the student, et cetera.
--
Eric Sosman
d