954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Generate 1 to n mumbers.

Hi every body , I got one problem that how can we generate 1 to n numbers without using any loop and without using recursion function ?

san_sarangkar
Newbie Poster
16 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

There's a generate_n function in the header.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

But there is for loop in it but program should not contain any loop.

san_sarangkar
Newbie Poster
16 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 
But there is for loop in it but program should not contain any loop.


Let me tell you a secret, every thing deep inside of the machine, are loops and go to.
Use a search engine, that question is all over, even when I don't understand the profit of it.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

>But there is for loop in it
How do you know? It might be a while loop, or it might even be magic. Coming up with a ridiculous excuse to call a library function is just as reasonable as coming up with a ridiculous requirement of doing something an unknown number of times without any kind of iteration at all.

The way I see it, you only have one good option: Slap the bejeezus out of the person who gave you this assignment and find another teacher. You won't learn anything useful from this assignment that couldn't be learned more easily with something realistic.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

lol .. I am always amazed by such questions .. there is complex stuff and then there are just idiotic unreasonable expectations and this falls in the latter category.

how on earth do you expect to generate n numbers without using a loop ? Now you could write n assignment statements one below the other ... but really that would not make any sense.

Well to add to this discussion, someone has apparently come up with a mechanism to do something similar .. here . Of course I still think its a convoluted way to trying to get you to do something, which in my opinion, you should never do in practice, but hey, I am not an authority on this stuff.

stilllearning
Posting Whiz
309 posts since Oct 2007
Reputation Points: 161
Solved Threads: 43
 

>Now you could write n assignment statements one below the other
Presumably the requirement is that n is an unknown quantity until runtime, in which case hard coding the statements is not possible in a compiled language. You could write a second program that builds the hard coded statements into a final program, but then you would be back to square one of not using a loop or recursion.

>someone has apparently come up with a mechanism to do something similar .. here
That would be recursion, which isn't allowed. Technically, any solution would involve some kind of loop or some kind of recursion, so unless you word the restrictions in such a way to allow the expected solution, it's quite literally an impossible problem.

A good trick is to use a third party program so that your program technically doesn't contain a loop or recursion. This relies on a common loophole of the program requirements.

Other tricky solutions are hiding the loop with goto, or hiding recursion with process spawning. Yet another is this evil little combination of tricks direct to Daniweb via my sleep deprived brain:

// Do not try this at home, kids
#include <stdio.h>
#include <stdlib.h>

int main ( int argc, char *argv[] )
{
  if ( argc > 1 ) {
    int n = atoi ( argv[1] );

    if ( n > 0 ) {
      char buf[BUFSIZ];

      printf ( "%d\n", n );
      sprintf ( buf, "\"%s\" %d", argv[0], n - 1 );
      system ( buf );
    }
  }

  return 0;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>Yet another is this evil little combination of tricks direct to Daniweb via my sleep deprived brain
Could that be the reason that you even entertained the post?

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You