HP3000-L Archives

March 2003, Week 4

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Stan Sieler <[log in to unmask]>
Reply To:
Stan Sieler <[log in to unmask]>
Date:
Sat, 22 Mar 2003 17:12:01 -0800
Content-Type:
text/plain
Parts/Attachments:
text/plain (50 lines)
> 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 *

ATOM RSS1 RSS2