print 1 to n without any conditional statement, loop or jump.

D

dev_cool

Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.

How is it possible? Please help me.

Thanks in advance.
 
C

Chris Dollin

dev_cool said:
Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.

How is it possible? Please help me.

If the solution is the kind I'm thinking of, it's not the sort of
exercise that a beginning programmer needs. You'd be better of
writing the code but ignoring the withouts.

Of course your "friend" might have been thinking of the "English
doesn't do quotes consistently" answer:

printf( "1 to %d\n", n );

where you have to declare and initialise `n` appropriately. Or
the even cheatier:

printf( "1 to n\n" );
or
printf( "1 to n without any conditional statement, loop or jump\n" );

You could try the exercises in K&R.
 
C

Chris McDonald

dev_cool said:
Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.

It's very unclear *why* one would want to do this, but:

printf("1 to n\n");

might be the required solution.
More seriously, they may be suggestion recursion, but that will require a
conditional test to terminate.
 
C

Christopher Benson-Manica

dev_cool said:
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.

Oh, you have a beer with your instructor now and then? Aww. That's
so sweet. Now if you'll just give us his e-mail address we'll save
you the trouble of having to hand your homework in.

(Do your own homework, thanks. Although the problem seems rather
silly.)
 
M

Manish

Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.

How is it possible? Please help me.

Thanks in advance.

If it is possible, I think this will do it.

#include <stdio.h>

int main() {

printf("print 1 to n\n");
return 0;
}
 
D

dev_cool

Chris Dollin wrote:
If the solution is the kind I'm thinking of, it's not the sort of
exercise that a beginning programmer needs. You'd be better of
writing the code but ignoring the withouts.

Of course your "friend" might have been thinking of the "English
doesn't do quotes consistently" answer:

printf( "1 to %d\n", n );

where you have to declare and initialise `n` appropriately. Or
the even cheatier:

printf( "1 to n\n" );
or
printf( "1 to n without any conditional statement, loop or jump
\n" );

You could try the exercises in K&R.

No, I'm not sure my friend wanted to ask for that kind of solution.
But thanks for your suggestions. May you please point out which
exercise in K&R would help me to solve this problem? I mean in which
chapter of the book?
 
E

Eric Sosman

dev_cool said:
Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.

How is it possible? Please help me.

#include <stdio.h>
#include <stdlib.h>
int main(int argc,char **argv){static const c\
har question[]="Homework, then?";char devcool\
[7]={'6'-sizeof(question)/3};devcool[3]=quest\
ion[5];devcool[4]=question[9];devcool[2]=ques\
tion[10];devcool[5]=question[13];devcool[1]=d\
evcool[4];puts(devcool);return EXIT_FAILURE;}
 
D

Duncan Muirhead

It's very unclear *why* one would want to do this, but:

printf("1 to n\n");

might be the required solution.
More seriously, they may be suggestion recursion, but that will require a
conditional test to terminate.
Weel, being pedantic one could use a conditional expression (rather than
statement) to terminate the recursion.
 
D

dev_cool

dev_cool said:
Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.
How is it possible? Please help me.

#include <stdio.h>
#include <stdlib.h>
int main(int argc,char **argv){static const c\
har question[]="Homework, then?";char devcool\
[7]={'6'-sizeof(question)/3};devcool[3]=quest\
ion[5];devcool[4]=question[9];devcool[2]=ques\
tion[10];devcool[5]=question[13];devcool[1]=d\
evcool[4];puts(devcool);return EXIT_FAILURE;}

Thank you Eric. But did I tell you that my friend asked me this
question and It was not a homework? Anyway, surely it puts smile on my
face. Thank you for your effort though.
 
S

stremler

begin quoting Chris McDonald said:
Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.
[snip]

More seriously, they may be suggestion recursion, but that will require a
conditional test to terminate.

I don't see "terminate" in the posing of the question, or even "1 to n
and then stop". So recursion seems quite the applicable solution. It will
*eventually* terminate anyway, hopefully reaching n before it does so.
 
E

Eric Sosman

dev_cool said:
dev_cool said:
Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.
How is it possible? Please help me.
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char **argv){static const c\
har question[]="Homework, then?";char devcool\
[7]={'6'-sizeof(question)/3};devcool[3]=quest\
ion[5];devcool[4]=question[9];devcool[2]=ques\
tion[10];devcool[5]=question[13];devcool[1]=d\
evcool[4];puts(devcool);return EXIT_FAILURE;}

Thank you Eric. But did I tell you that my friend asked me this
question and It was not a homework? Anyway, surely it puts smile on my
face. Thank you for your effort though.

Yes, you wrote that this was not homework, but just a
challenge between friends. I didn't believe you.

But perhaps I am too cynical, too quick to judge, too
eager to see the mote in another's eye. Perhaps you and
your friend pass the time by setting programming problems
for each other. You've described the problem he set for
you; what challenge did you give to him? Did he meet it?
 
R

Roberto Waltman

Chris said:
It's very unclear *why* one would want to do this, but:

printf("1 to n\n");

might be the required solution.
More seriously, they may be suggestion recursion, but that will require a
conditional test to terminate.

This thing puzzled me enough to violate the holy principle of not
doing somebody else's homework.

The following code would apparently do it, (for small values of 'n',
limited by the number of cases in a switch statement.)

Of course this is only pushing the conditionalt statements, loops or
jumps from the visible spectrum to the infrassembler.


#define CASE(x) case x: printf("%d ", cnt++)

/* print from 1 to n, for 1 <= n <= 5 */

void print_from_1_to_n(int n)
{
int cnt = 1;
switch (n)
{
CASE(5);
CASE(4);
CASE(3);
CASE(2);
CASE(1);
}
printf("\n");
}


Roberto Waltman

[ Please reply to the group,
return address is invalid ]
 
D

Dave Dunfield

I'm a beginner in C programming. One of my friends asked me to write a
More seriously, they may be suggestion recursion, but that will require a
conditional test to terminate.

Actually, the original statement is: without any CONDITONAL STATEMENT,
loop or jump ...

So .. tecnnically, this does meet the requrements:

--------------------------------------------------------------------------------------------------------
#include <stdio.h>

/*
* Prints numbers from 1 to argument 'n' without using
* a conditional statement, loop or jump (However it does
* make use of recursion and the short-circuit operation of
* logical AND && :)
*/
int print1ton(unsigned n)
{
--n && print1ton(n);
printf("%u\n", n+1);
return 0;
}

int main(int argc, char *argv[])
{
/*
* Should really check that (argc == 2) && (atoi(argv[1]) != 0)
* because calling print1ton(0) results in uncontrolled recursion
* & stack explosion on most architectures - but lets keep it
* "unconditional"... (be sure to specify a valid argument)
*/
print1ton(atoi(argv[1]));

return 0;
}
 
J

John Bode

Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.

How is it possible? Please help me.

Thanks in advance.

Recursion + short-circuit evaluation are your friends here.
 
C

Christopher Benson-Manica

Roberto Waltman said:
The following code would apparently do it, (for small values of 'n',
limited by the number of cases in a switch statement.)

If switch is permitted, the recursive solution is trivial and always
correct:

#include <stdio.h>

void do_print( int n ) {
switch( n ) {
case 0:
return;
default:
do_print( n-1 );
}
printf( "%d ", n );
}

int main( int argc, char *argv[] ) {
do_print( 5 );
fputc( '\n', stdout );
return 0;
}
 
K

Keith Thompson

Roberto Waltman said:
This thing puzzled me enough to violate the holy principle of not
doing somebody else's homework.

The following code would apparently do it, (for small values of 'n',
limited by the number of cases in a switch statement.)
[snip]

"if" and "switch" are just two different kinds of conditional
statements (the standard calls them "selection statements").
 
C

Christopher Benson-Manica

dev_cool said:
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.

I'm only posting this solution because I'd like the gurus to tell me
what's wrong with it (it seems to work):

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

void handleZero( int ignore ) {
}

void handleOne( int ignore ) {
printf( "\n" );
exit( 0 );
}

void do_print( int this, int n ) {
raise( this/(n+1) );
printf( "%d ", this );
do_print( this+1, n );
}

int main( void ) {
signal( 0, handleZero );
signal( 1, handleOne );
do_print( 1, 5 );
return 0; /* Never reached */
}
 
U

user923005

Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.

How is it possible? Please help me.

Thanks in advance.

There is no way to know if it can be accomplished.
I guess that printf() has conditional statements, loops and jumps.

But besides the recursive solutions and the short circuit evaluation
hints, you could also write a program that writes the program.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc > 1) {
const int count = atoi(argv[1]);
int i;
puts("/* solution: */");
puts("#include <stdio.h>");
puts("int main(void)");
puts("{");
for (i = 1; i <= count; i++)
printf("puts(\"%d\");\n", i);
puts("return 0;");
puts("}");
}
return 0;
}

Now, one might object that the program that writes the program uses
conditional statements, jumps and loops. But then again, probably
printf() does as well so the answer might well be "no" in the case
that no tool using those tools can be used.

At any rate, I think your instructor is an idiot to give you this kind
of assignment. It's like teaching someone with a giant toolbox of
tools to overhaul an engine using a pipe wrench and a ball peen
hammer.

There are some restrictions that make sense. For instance, I taught C
to students for which many were long time COBOL programmers. So I
expressly forbade the use of goto in programming assignments with the
caveat that in the real world they could use all the gotos that they
want -- just not in my class. I wanted them to think about how to
program without using it. And I wanted to be able to understand the
programs that they turned in more easily also.

If he wants you to use recursion, he should have given you that hint
(in which case it would not have been an idiotic requirement). But to
make a beginning student guess that they are going to have to use
recursion is foolishness. For someone who has been programming C for
a decade, recursion will instantly be recognized as a solution. For
someone who has never programmed, it is doubtful that they would think
of using recursion without outside help. For that reason, I think it
is an approach that encourages plaugiarism.
 
M

Malcolm McLean

dev_cool said:
Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.

How is it possible? Please help me.

Thanks in advance.

This seems to work.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>

typedef void (*printitf)(int x, int N);

void donowt(int x, int );
void printit(int x, int N);
int iszero(int x);

printitf fptr[2] = {donowt, printit};

int main(int argc, char **argv)
{
printit(0, atoi(argv[1]));

return 0;
}

void donowt(int x, int N )
{
return;
}
void printit(int x, int N)
{
printf("%d\n", x+1);
x++;
x = x % N;
(*fptr[ iszero(x) ])(x, N);
}

int iszero(int x)
{
return (int) ceil(x/ (double) INT_MAX);
}
 
D

Dave Vandervies

I'm only posting this solution because I'd like the gurus to tell me
what's wrong with it (it seems to work):
raise( this/(n+1) );
signal( 0, handleZero );
signal( 1, handleOne );

If I'm reading n869 correctly, the values that the macros in 7.14#3
expand to are the only valid ones to pass to signal() and raise() for
the sig argument.

It looks recoverable, though:

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

/*handleZero is unnecessary (we can use SIG_IGN)*/

#if 0
void handleOne( int ignore ) {
#else
void done(int ignore) {
#endif
printf( "\n" );
exit( 0 );
}

void do_print( int this, int n ) { #if 0
raise( this/(n+1) );
#else
/*SIGFOO are distinct positive integer values, so
SIGTERM-SIGINT
can't overflow when INT_MIN <= -INT_MAX
*/
raise( (this/(n+1))*(SIGTERM-SIGINT) + SIGINT);
#endif
printf( "%d ", this );
do_print( this+1, n );
}

int main( void ) { #if 0
signal( 0, handleZero );
signal( 1, handleOne ); #else
signal(SIGINT,SIG_IGN);
signal(SIGTERM,done);
#endif

do_print( 1, 5 );
return 0; /* Never reached */
}


--
Dave Vandervies (e-mail address removed)
It's hard to hate configurable software.
Don't worry. We're here to help.
--Jason Diamond and Yoz Grahame on hates-software
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top