Apr 12, 2002 #1 mastguy Technical User Mar 13, 2002 68 US Generating all possible combination of a number entered using "for" loop only
Apr 12, 2002 #2 Zyrenthian Programmer Mar 30, 2001 1,440 US I dont quite follow. If you mean something like 123 132 213 231 312 321 then I would suggest a character array?? or maybe an integer array if you put each value into a single element. the number of combinations you will have will be <# of digits in number>! <-(factorial) which you can define a simple recursive algorithm for int factorial(int n) { assert(n>=0); if(n==0) return 1; return n * factorial(n-1); } and your for loop would be something like int control = factorial; for(int i = 0;i<control;i++) { // change the numbers arround // output numbers } Matt Upvote 0 Downvote
I dont quite follow. If you mean something like 123 132 213 231 312 321 then I would suggest a character array?? or maybe an integer array if you put each value into a single element. the number of combinations you will have will be <# of digits in number>! <-(factorial) which you can define a simple recursive algorithm for int factorial(int n) { assert(n>=0); if(n==0) return 1; return n * factorial(n-1); } and your for loop would be something like int control = factorial; for(int i = 0;i<control;i++) { // change the numbers arround // output numbers } Matt