> 2. Write a 'C' program without using any loop (if, for, while etc) to
> print numbers from 1 to 100 and 100 to 1;
(Wherein we learn that the specification above is a bit weak :)
The following has no "if" (or arithemtic if ("?")), "for", "while",
or "goto". Note that "goto" isn't excluded by the question,
just it's use as a *looping* construct would be.)
extern int exit (int n);
int (*labels [2]) (int);
int printup (int xxx) {
(void) printf ("%4d %4d\n", xxx, 101-xxx);
(*labels [--xxx > 0]) (xxx);
return 0;
}
main () {
labels [0] = &exit;
labels [1] = &printup;
printup (100);
}
output:
100 1
99 2
98 3
97 4
96 5
95 6
..
1 100
I wrote it by asking myself "what's the equivalent of
a computed GOTO in ASL?", and translating that into C :)
--
Stan Sieler
[log in to unmask]
www.allegro.com/sieler/wanted/index.html
* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *
* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *
|