By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Feb 23 '07 #1
Share this Question
Share on Google+
43 Replies


P: n/a
dev_cool wrote:
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.

--
Chris "electric hedgehog" Dollin
The shortcuts are all full of people using them.

Feb 23 '07 #2

P: n/a
dev_cool <su*******@gmail.comwrote:
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.)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Feb 23 '07 #3

P: n/a
"dev_cool" <su*******@gmail.comwrites:
>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.

--
Chris.
Feb 23 '07 #4

P: n/a
On Feb 23, 10:59 am, "dev_cool" <sujoym...@gmail.comwrote:
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;
}

Feb 23 '07 #5

P: n/a
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?

Feb 23 '07 #6

P: n/a
dev_cool wrote:
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;}

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 23 '07 #7

P: n/a
On Feb 23, 9:26 pm, Eric Sosman <esos...@acm-dot-org.invalidwrote:
dev_cool wrote:
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;}

--
Eric Sosman
esos...@acm-dot-org.invalid
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.

Feb 23 '07 #8

P: n/a
begin quoting Chris McDonald <ch***@csse.uwa.edu.au:
"dev_cool" <su*******@gmail.comwrites:
>>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.

--
Stewart Stremler st******@rohan.sdsu.edu
-----------------------------------------------------------------------------
The problem with these international affairs is that they attract foriegners.
--from _Those Magnificant Men & Their Flying Machines_
Feb 23 '07 #9

P: n/a
Chris McDonald wrote:
>"dev_cool" writes:
>>...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.
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 ]
Feb 23 '07 #10

P: n/a
dev_cool wrote:
On Feb 23, 9:26 pm, Eric Sosman <esos...@acm-dot-org.invalidwrote:
>dev_cool wrote:
>>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;}

--
Eric Sosman
esos...@acm-dot-org.invalid

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?

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 23 '07 #11

P: n/a
On Fri, 23 Feb 2007 16:06:28 +0000, Chris McDonald wrote:
"dev_cool" <su*******@gmail.comwrites:
>>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.
Weel, being pedantic one could use a conditional expression (rather than
statement) to terminate the recursion.
Feb 23 '07 #12

P: n/a
>>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.
>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;
}
--
Dunfield Development Services http://www.dunfield.com
Low cost software development tools for embedded systems
Software/firmware development services Fax:613-256-5821

Feb 23 '07 #13

P: n/a
On Feb 23, 9:59 am, "dev_cool" <sujoym...@gmail.comwrote:
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.

Feb 23 '07 #14

P: n/a
Roberto Waltman <us****@rwaltman.netwrote:
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;
}

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Feb 23 '07 #15

P: n/a
Roberto Waltman <us****@rwaltman.netwrites:
Chris McDonald wrote:
>>"dev_cool" writes:
>>>...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.

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").

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 23 '07 #16

P: n/a
dev_cool <su*******@gmail.comwrote:
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 */
}

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Feb 23 '07 #17

P: n/a
On Feb 23, 7:59 am, "dev_cool" <sujoym...@gmail.comwrote:
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.
Feb 23 '07 #18

P: n/a

"dev_cool" <su*******@gmail.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
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);
}

Feb 23 '07 #19

P: n/a
In article <er**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@otaku.freeshell.orgwrote:
>dev_cool <su*******@gmail.comwrote:
>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):
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 dj******@csclub.uwaterloo.ca
It's hard to hate configurable software.
Don't worry. We're here to help.
--Jason Diamond and Yoz Grahame on hates-software
Feb 23 '07 #20

P: n/a
user923005 wrote:
>
.... snip ...
>
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.
You forgot the file. Maybe both a coarse and fine file. :-)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 24 '07 #21

P: n/a
On Feb 24, 3:44 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
"dev_cool" <sujoym...@gmail.comwrote in message

news:11**********************@m58g2000cwm.googlegr oups.com...
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);

}
Thank you Malcolm. I actually compiled and run your code.
Unfortunately it terminates after printing 1 and then a message
"Divide error". My compiler is Turbo C++ 3.0

Feb 24 '07 #22

P: n/a
On Feb 24, 1:53 am, "user923005" <dcor...@connx.comwrote:
On Feb 23, 7:59 am, "dev_cool" <sujoym...@gmail.comwrote:
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.
First let me say thank you for your effort. Now I already told before
that this thing is between two friends, two very young
programmer(rather student) of C. Its not my assignment nor homework.
Its kind of a challenge. He although new like me in C/C++ world, I
have to admit is better than me and when he gave me like this kind of
problem, i know he has a solution. I also have one for him and I
already gave him. From the last call he did, I see he is also stuck.
Actually we both try to solve several programming problems in our
leisure time. Allthough we are not as intelligent or experienced as
you people are. But you know, in this way....what should I say...this
is for our self-development I suppose.

Thats why when I got stuck, I thought of seeking help from you.
Because this group consists of the C gurus. This is the place I think.
Thats the story. No homework, no assignment, just a problem solving or
time pass whatever you call it between two of C lovers both of whom
are novices.

Thank you once again.

Feb 24 '07 #23

P: n/a
In article <11**********************@t69g2000cwt.googlegroups .com>,
dev_cool <su*******@gmail.comwrote:
>Thank you Malcolm. I actually compiled and run your code.
Unfortunately it terminates after printing 1 and then a message
"Divide error".
You did run it with a command-line argument, right? Like "foo 10"?
My compiler is Turbo C++ 3.0
Man after my own heart.

If you're not running MSDOS, and you want a newer free C/C++ compiler,
there are some out there (like at http://digitalmars.com/ for instance.)

-Beej

Feb 24 '07 #24

P: n/a
On 23 Feb 2007 07:59:51 -0800, "dev_cool" <su*******@gmail.comwrote:
>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 you're a beginner in C, your so-called friend is asking way too
much of you. These types of questions are best left to the C
Magicians, who perform their magic only after they've mastered the
basics of C, grasshopper.

Before the C Magicians tackle these types of problems, they become
well-schooled in the basic concepts and even intricacies of C. For
example, these Magicians know what basic concepts such as what the
return type of main() must be:

int main(void)

They know what undefined behavior is:

int i = 0;
i = i++;

And they even know what things like NDEBUG are and how not knowing
what they are can lead to the following type of error:

char *p;
assert(p = malloc(sizeof *p));
/* do something with p */

These are but a few of the things the C Magicians know; it represents
only the tip of the iceberg of what they know.

You have a lot to learn, grasshopper. Continue reading this newsgroup
for a few months, and maybe you'll get a yellow belt.

--
jay
Feb 24 '07 #25

P: n/a
dev_cool wrote:
>
.... snip ...
>
Thank you Malcolm. I actually compiled and run your code.
Unfortunately it terminates after printing 1 and then a message
"Divide error". My compiler is Turbo C++ 3.0
Did you give it a suitable argument to work with?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 24 '07 #26

P: n/a

"CBFalconer" <cb********@yahoo.comwrote in message
dev_cool wrote:
>>
... snip ...
>>
Thank you Malcolm. I actually compiled and run your code.
Unfortunately it terminates after printing 1 and then a message
"Divide error". My compiler is Turbo C++ 3.0

Did you give it a suitable argument to work with?
For extra credit, check and validate argument without using loops, jumps or
conditional statements.

Feb 24 '07 #27

P: n/a
On Feb 24, 11:34 am, Beej Jorgensen <b...@beej.uswrote:
In article <1172294904.334436.149...@t69g2000cwt.googlegroups .com>,

dev_cool <sujoym...@gmail.comwrote:
Thank you Malcolm. I actually compiled and run your code.
Unfortunately it terminates after printing 1 and then a message
"Divide error".

You did run it with a command-line argument, right? Like "foo 10"?
My compiler is Turbo C++ 3.0

Man after my own heart.

If you're not running MSDOS, and you want a newer free C/C++ compiler,
there are some out there (like athttp://digitalmars.com/for instance.)

-Beej
No I did not run it from command-line arguement, rather from ide.

Feb 24 '07 #28

P: n/a
On Feb 24, 1:22 pm, jaysome <jays...@hotmail.comwrote:
On 23 Feb 2007 07:59:51 -0800, "dev_cool" <sujoym...@gmail.comwrote:
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 you're a beginner in C, your so-called friend is asking way too
much of you. These types of questions are best left to the C
Magicians, who perform their magic only after they've mastered the
basics of C, grasshopper.

Before the C Magicians tackle these types of problems, they become
well-schooled in the basic concepts and even intricacies of C. For
example, these Magicians know what basic concepts such as what the
return type of main() must be:

int main(void)

They know what undefined behavior is:

int i = 0;
i = i++;

And they even know what things like NDEBUG are and how not knowing
what they are can lead to the following type of error:

char *p;
assert(p = malloc(sizeof *p));
/* do something with p */

These are but a few of the things the C Magicians know; it represents
only the tip of the iceberg of what they know.

You have a lot to learn, grasshopper. Continue reading this newsgroup
for a few months, and maybe you'll get a yellow belt.

--
jay
Thanks a lot jay. Surely I would read the posts of this group. Bt
first I have to find a solution of my problem.Thanks for your
suggestions though.

Feb 24 '07 #29

P: n/a
On Feb 24, 10:28 am, "dev_cool" <sujoym...@gmail.comwrote:
On Feb 24, 3:44 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
"dev_cool" <sujoym...@gmail.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
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);
}

Thank you Malcolm. I actually compiled and run your code.
Unfortunately it terminates after printing 1 and then a message
"Divide error". My compiler is Turbo C++ 3.0
I'm very sorry Malcom. Actually your program is running smoothly and
without any problem. Actually it was my fault. Thanks a lot to you.
Thanks a lot to all of you people for bearing me for such a long time.

Feb 24 '07 #30

P: n/a
On Feb 23, 4:59 pm, "dev_cool" <sujoym...@gmail.comwrote:
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.
How about:

#include <stdio.h>

int precn(int n);

int precn(int n) {
printf("%d\n", n>1 ? precn( n - 1 ) : 1);
return n+1;
}

int main(int argc, char const **argv)
{
precn(10);
return 0;
}

I do use a conditional expression, but expression != statement.

Feb 24 '07 #31

P: n/a
dev_cool skrev:
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>

int print_numbers(int n)
{
n && print_numbers(n - 1) && printf("%d\n", n);
return 1;
}

int main(void)
{
print_numbers(10);
return 0;
}
August
Feb 24 '07 #32

P: n/a
In article <11**********************@t69g2000cwt.googlegroups .com>,
dev_cool <su*******@gmail.comwrote:
>No I did not run it from command-line arguement, rather from ide.
Well, figure out where in the IDE you specify command line arguments,
and put the number in there. The code blindly (which is ok because it's
just a demo) assumes that you'll have a positive number as a command
line argument.

When you don't specify an argument, argv[1] is probably NULL, and I'll
bet your atoi(argv[1]) succeeds and returns 0 (<ot>for some reason--IIRC
there was INT 0x20 code at address 0x0 in a com file, but I can't
remember about EXEs.</ot Or maybe NULL is just handled by Borland's
atoi().) You then % 0 which gives a divide error. That's my guess,
anyway.

The FAQ talks a bit about command line arguments and the argv array here:

http://c-faq.com/misc/argv.html

-Beej

Feb 24 '07 #33

P: n/a
On Feb 24, 4:14 pm, August Karlstrom <fusionf...@comhem.sewrote:
dev_cool skrev:
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>

int print_numbers(int n)
{
n && print_numbers(n - 1) && printf("%d\n", n);
return 1;

}

int main(void)
{
print_numbers(10);
return 0;

}

August
much better! :)

Magnus

Feb 24 '07 #34

P: n/a
CBFalconer <cb********@yahoo.comwrites:
dev_cool wrote:
... snip ...
>>
Thank you Malcolm. I actually compiled and run your code.
Unfortunately it terminates after printing 1 and then a message
"Divide error". My compiler is Turbo C++ 3.0

Did you give it a suitable argument to work with?
Did the author document how the program is supposed to be used?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 24 '07 #35

P: n/a
On Feb 25, 2:08 am, Keith Thompson <k...@mib.orgwrote:
CBFalconer <cbfalco...@yahoo.comwrites:
dev_cool wrote:
... snip ...
Thank you Malcolm. I actually compiled and run your code.
Unfortunately it terminates after printing 1 and then a message
"Divide error". My compiler is Turbo C++ 3.0
Did you give it a suitable argument to work with?

Did the author document how the program is supposed to be used?

--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Thanks Keith for providing me those valueable links. Yes, i already
wrote it was my mistake, first time I did run Malcolm's program
without the argument. But now I have figured it out. Thankz also goes
to Beej, your writing was simple and clear to clear my doubts. I must
include the names of panic and August too for their good intension in
helping me.

Feb 26 '07 #36

P: n/a
On Feb 26, 11:08 am, "dev_cool" <sujoym...@gmail.comwrote:
On Feb 25, 2:08 am, Keith Thompson <k...@mib.orgwrote:
CBFalconer <cbfalco...@yahoo.comwrites:
dev_cool wrote:
... snip ...
>Thank you Malcolm. I actually compiled and run your code.
>Unfortunately it terminates after printing 1 and then a message
>"Divide error". My compiler is Turbo C++ 3.0
Did you give it a suitable argument to work with?
Did the author document how the program is supposed to be used?
--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Thanks Keith for providing me those valueable links. Yes, i already
wrote it was my mistake, first time I did run Malcolm's program
without the argument. But now I have figured it out. Thankz also goes
to Beej, your writing was simple and clear to clear my doubts. I must
include the names of panic and August too for their good intension in
helping me.
My friend also has a solution of his own. Although you people already
provide me solutions I think I can post my friend's one here too. You
might want to check this out.Although he used C++ instead of C.

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

#include<iostream.h>

class abc{
public:
static int i;
abc()
{
cout<<i++<<endl;
}
};

int abc :: i = 1;

int main() {
int n = 100;
abc *pt;

pt = new abc[n];

return 0;
}

Feb 26 '07 #37

P: n/a
dev_cool wrote:
>
My friend also has a solution of his own. Although you people already
provide me solutions I think I can post my friend's one here too. You
might want to check this out.Although he used C++ instead of C.
#include<iostream.h>
That's C++, this is comp.lang.c.

--
Ian Collins.
Feb 26 '07 #38

P: n/a
dev_cool wrote:

(Something funny happened to your >'s. I've fixed it here.)
Chris Dollin wrote:
>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?
I meant for you to try all of them, actually, and not with particular
reference to your problem, as a way of learning C.

Begin at the beginning, and go on till you come to the end: then stop.

--
Chris "king hedgehog" Dollin
"- born in the lab under strict supervision -", - Magenta, /Genetesis/

Feb 26 '07 #39

P: n/a
Groovy hepcat dev_cool was jivin' on 23 Feb 2007 07:59:51 -0800 in
comp.lang.c.
print 1 to n without any conditional statement, loop or jump.'s a cool
scene! Dig it!
>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.
It's possible by recursion, but not a very good application of
recursion.
Are you permitted to use the ternary (conditional) operator?

#include <stdio.h>

int putnums(unsigned n)
{
(0 < n) ? putnums(n - 1), printf("%u\n", n) : 0;
return 0;
}

Even if you're not allowed to use that, it still isn't very hard to
come up with something that works. Others have hinted the way.

#include <stdio.h>

int putnums(unsigned n)
{
return 0 < n && (putnums(n - 1), printf("%u\n", n));
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Feb 26 '07 #40

P: n/a
On Mon, 26 Feb 2007 19:33:00 +1300, Ian Collins <ia******@hotmail.com>
wrote:
>dev_cool wrote:
>>
My friend also has a solution of his own. Although you people already
provide me solutions I think I can post my friend's one here too. You
might want to check this out.Although he used C++ instead of C.
#include<iostream.h>
That's C++, this is comp.lang.c.
That's scarcely surprising since he said it was C++ in the text you
quoted.
Feb 26 '07 #41

P: n/a

"dev_cool" <su*******@gmail.comwrote in message
>
#include<iostream.h>

class abc{
public:
static int i;
abc()
{
cout<<i++<<endl;
}
};

int abc :: i = 1;

int main() {
int n = 100;
abc *pt;

pt = new abc[n];

return 0;
}
The question is whether disguised loops and conditions count.
For instance a compound conditional

x = a && b();

will execute an if statement in disguise, because of the intelligent
guarding rule.

Similarly your friend's example, which is C++, is just executing a hidden
for() loop as C++ calls constructors on an array.

Feb 26 '07 #42

P: n/a
Malcolm McLean <re*******@btinternet.comwrote:
The question is whether disguised loops and conditions count.
If those are unacceptable, Dave Vandervies' correction elsethread of my
version relying on recursive calls to raise() is one possible
alternative.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Feb 27 '07 #43

P: n/a
On Feb 23, 1:01 pm, Christopher Benson-Manica
<a...@otaku.freeshell.orgwrote:
dev_cool <sujoym...@gmail.comwrote:
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" );
You really, *really* don't want to call a library function from an
interrupt handler (other than signal), since library functions may
raise signals themselves, and they are not guaranteed to be
reentrant. I've fixed one bug that was exactly this situation; we had
an Access database that kept getting hosed beyond repair because a
printf() in an interrupt handler would occasionally write the string
to the .mdb file instead of stdout.
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 */

}

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.

Feb 27 '07 #44

This discussion thread is closed

Replies have been disabled for this discussion.