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 ?

Recommended Answers

All 7 Replies

There's a generate_n function in the <algorithm> header.

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

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.

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

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.