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

C

CBFalconer

user923005 said:
.... 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. :)
 
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.

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

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.
 
B

Beej Jorgensen

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
 
J

jaysome

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.
 
C

CBFalconer

dev_cool said:
.... 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?
 
M

Malcolm McLean

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

dev_cool

You did run it with a command-line argument, right? Like "foo 10"?


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.
 
D

dev_cool

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.

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.
 
D

dev_cool

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.
 
P

panic

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.
 
A

August Karlstrom

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
 
B

Beej Jorgensen

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
 
P

panic

dev_cool skrev:



#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
 
K

Keith Thompson

CBFalconer said:
dev_cool wrote:
... snip ...

Did you give it a suitable argument to work with?

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

dev_cool

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

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.
 
D

dev_cool

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;
}
 
I

Ian Collins

dev_cool said:
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.
 
C

Chris Dollin

dev_cool wrote:

(Something funny happened to your >'s. I've fixed it here.)
Chris Dollin wrote:

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.
 
P

Peter Shaggy Haywood

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"?
 

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,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top