Permutations of an N element array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 6
Reputation: merck120 is an unknown quantity at this point 
Solved Threads: 0
merck120 merck120 is offline Offline
Newbie Poster

Permutations of an N element array

 
0
  #1
Nov 9th, 2006
I am currently working on a program in C, that needs to calculate the permutations
of a n integer array. The approach that I have taken is using recursion, using a n-element array of counters incrementing the correct value each recursion,
so the subroutine calls itself x (x being the max # of permutations) number of times returning the next permutation.

For example: Information known ahead of time: n = 3(could be any value); n_array[0] = 3 (could be any value from 1 to 9), n_array[1] = 3, n_array[2] = 3:
so first permutation (does not have to be in this order):
0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
1 0 0
1 0 1
1 0 1
1 1 1 etc.
That being said, I attempted to write a loop within the recursive function that looks like this,
  1. for(i = 0; i <n; i++) {
  2. if(i == n-1) {
  3. count[i]++;
  4. }
  5. if(count[i] == n_array[i]) {
  6. count[i] = 0;
  7. count[i-1]++;
  8. }
  9. }
which gives the following:
0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
0 3 0 -> incorrect
1 0 1
1 0 2 etc.
I realize what my loop is doing, but I can't think of a way to fix
it, any fresh ideas would be appriciated. Thanks in advance.
Last edited by WaltP; Nov 10th, 2006 at 3:02 am. Reason: Use regular CODE tags, not INLINECODE tags for code blocks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Permutations of an N element array

 
0
  #2
Nov 10th, 2006
this would be in the daniweb code snippet section.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: merck120 is an unknown quantity at this point 
Solved Threads: 0
merck120 merck120 is offline Offline
Newbie Poster

Re: Permutations of an N element array

 
0
  #3
Nov 10th, 2006
Originally Posted by iamthwee View Post
this would be in the daniweb code snippet section.
Under what listing, I went through all snippets, and I did not see what I was looking for.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Permutations of an N element array

 
0
  #4
Nov 10th, 2006
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Permutations of an N element array

 
0
  #5
Nov 10th, 2006
A non recursive way, if it suits your need:

http://www.daniweb.com/code/snippet533.html
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: merck120 is an unknown quantity at this point 
Solved Threads: 0
merck120 merck120 is offline Offline
Newbie Poster

Re: Permutations of an N element array

 
0
  #6
Nov 10th, 2006
Originally Posted by ~s.o.s~ View Post
A non recursive way, if it suits your need:

http://www.daniweb.com/code/snippet533.html
Not what I am looking for, look at my first post.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Permutations of an N element array

 
0
  #7
Nov 10th, 2006
Originally Posted by merck120 View Post
Not what I am looking for, look at my first post.
So, your example don't even make sense

0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
1 0 0
1 0 1 <- Why the repeat
1 0 1 <-Why the repeat
1 1 1

In fact your example doesn't even exhibit behaviour you would expect when using the word 'permutation'
Last edited by iamthwee; Nov 10th, 2006 at 3:29 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: merck120 is an unknown quantity at this point 
Solved Threads: 0
merck120 merck120 is offline Offline
Newbie Poster

Re: Permutations of an N element array

 
0
  #8
Nov 10th, 2006
Originally Posted by iamthwee View Post
So, your example don't even make sense

0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
1 0 0
1 0 1 <- Why the repeat
1 0 1 <-Why the repeat
1 1 1

In fact your example doesn't even exhibit behaviour you would expect when using the word 'permutation'
I sorry, that line is a typo, and it should not be repeated, and yeah I know permutation is not the best word, for what I am doing, combinations, would probably be better.

it should be:
100
101
102
110
etc.
Last edited by merck120; Nov 10th, 2006 at 3:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Permutations of an N element array

 
0
  #9
Nov 10th, 2006
Ok, so do you mean the mathematical definition of the word combination.


Or are you going to change your mind again.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: merck120 is an unknown quantity at this point 
Solved Threads: 0
merck120 merck120 is offline Offline
Newbie Poster

Re: Permutations of an N element array

 
0
  #10
Nov 10th, 2006
Originally Posted by iamthwee View Post
Ok, so do you mean the mathematical definition of the word combination.


Or are you going to change your mind again.
Yes, all of the mathematical combinations, per my fixed example.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC