943,569 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 715
  • C RSS
Oct 3rd, 2008
0

Generate 1 to n mumbers.

Expand Post »
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 ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
san_sarangkar is offline Offline
16 posts
since Sep 2008
Oct 3rd, 2008
1

Re: Generate 1 to n mumbers.

There's a generate_n function in the <algorithm> header.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 3rd, 2008
0

Re: Generate 1 to n mumbers.

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

Re: Generate 1 to n mumbers.

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
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 3rd, 2008
1

Re: Generate 1 to n mumbers.

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 3rd, 2008
0

Re: Generate 1 to n mumbers.

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.
Last edited by stilllearning; Oct 3rd, 2008 at 6:12 pm.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 4th, 2008
1

Re: Generate 1 to n mumbers.

>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:
  1. // Do not try this at home, kids
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main ( int argc, char *argv[] )
  6. {
  7. if ( argc > 1 ) {
  8. int n = atoi ( argv[1] );
  9.  
  10. if ( n > 0 ) {
  11. char buf[BUFSIZ];
  12.  
  13. printf ( "%d\n", n );
  14. sprintf ( buf, "\"%s\" %d", argv[0], n - 1 );
  15. system ( buf );
  16. }
  17. }
  18.  
  19. return 0;
  20. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 4th, 2008
0

Re: Generate 1 to n mumbers.

>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
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC