Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

Write a short C program that prints all the numbers from 1 to 1000 on separate lines without using any explicit if-statements, gotos, loops, or conditional expressions.

OpenStudy (anonymous):

I'm sure there are several creative ways to skin a cat.

OpenStudy (anonymous):

I sure can't think of one. Maybe recursion but even there you'd have to use an if statement to terminate it, and it would overflow the stack on most platforms.

OpenStudy (anonymous):

I hate questions like this. They're more parlor tricks than learning anything deep.

OpenStudy (anonymous):

Right.

OpenStudy (anonymous):

All I can come up with for this is either copy-pasting, some fancy editor trick, or preprocessor abuse.

OpenStudy (anonymous):

or maybe even recursively calling main

OpenStudy (anonymous):

Ugh, I hope it's not preprocessor abuse. That would be the opposite of teaching. And yeah, I was thinking about recursively calling main, or a binary tree of calls, but all of those require termination conditions.

OpenStudy (anonymous):

who gave you this problem?

OpenStudy (anonymous):

editor tricks would violate the "short c program"

OpenStudy (anonymous):

#define PRINTONE(x) fprintf(stdout, "%d\n", x++); #define TEN(x) x x x x x x x x x x #define PRINT1000(x) TEN(TEN(TEN(PRINTONE(x))))

OpenStudy (anonymous):

wow, yeah, that works but is exactly the opposite what a class should be teaching

OpenStudy (anonymous):

right. at least after this question I'm done with the C preprocessor chapter.

OpenStudy (anonymous):

yeah, i just ran it through the preprocessor and sure enough gave me 1000 fprintf lines

OpenStudy (anonymous):

#include <stdio.h> int i = 0; p() { printf("%d\n", ++i); } a() { p();p();p();p();p(); } b() { a();a();a();a();a(); } c() { b();b();b();b();b(); } main() { c();c();c();c();c();c();c();c(); return 0; } :D

OpenStudy (anonymous):

lol

OpenStudy (rsmith6559):

#include <stdio.h> void printNumber( int number ) { number ? printNumber( --number ) : printf( "%d\n", number ); } void main() { printNumber( 1000 ); return 0; } Off the top of my head, not tested at all.

OpenStudy (anonymous):

@rsmith but that uses a conditional expression :(

OpenStudy (rsmith6559):

I read the no "if", not the no conditionals. I have GOT to stop skipping!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!