How can I implement a permutation program in C?

  • Thread starter MathematicalPhysicist
  • Start date
In summary, the conversation is discussing the creation of a function that returns permutations of an integer. The person has shared their progress so far, including using a factorial function and printing the list order. They are now seeking advice on how to implement sorting in order to create a list of all permutations. Another person shares a potential approach using a permutation program they have created in the past.
  • #1
MathematicalPhysicist
Gold Member
4,699
371
i need to write functions that gives back the permutation of an integer.
i have already the factorial function in, so here what i did so far:
i can assume that MAXCOL is 7, you can assume that n<=7.
Code:
int perm(int n)
{
MAXROW=fact(MAXCOL);
int perms[MAXROW][MAXCOL], i,j;
for(j=0;j<n;j++){
for(i=0;i<fact(n);i++){
perms[i][j]=j+1;
}
}
so far as you can see I am printing the list order i.e if n=2
then
Code:
12
12
now i want to be able to unchange the first line and then go to the second line and change it, and then change the third line that it wouldn't be identical to formers, and so forth.
the problem i don't know how to implement it, obviously some sorting is in place but which?

thanks in advance.
 
Technology news on Phys.org
  • #2
You want to make a list of all permutations of n integers?

It's not very efficient, but you could use an approach of rotating the first n objects:
Code:
int * cycle (int *numbers, int n) {
   int tmp,i;

   tmp=numbers[0];
   for(i=0;i<n-1;i++) {
      numbers[i]=numbers[i+1];
   }
   numbers[i]=tmp;
   return numbers;
}

/*These are global so that they're static */
int permindex=0;
int permutations[ROWS][COLS]

void add_permutation (int *permutation) {
   int i;
   for(i=0;i<MAXCOLS;i++) {
       permutations[permindex][i]=permutation[i];
   }
   permindex++;
}

int *permute (int *numbers, int n) {
   int i;

   if(1==n) {
      add_permutation(numbers);
   } else {
      for(i=0;i<n;i++) {
         numbers=cycle(numbers,n);
         permute(numbers,n-1);
      }
   }
}

int main {
   int i;
   int numbers[MAXCOLS];

   for(i=0;i<MAXCOLS;i++) {
      numbers[i]=i;
   }
   permute(numbers,MAXCOLS);
}
 

1. What are permutations?

Permutations are the different ways in which a set of objects or elements can be arranged or ordered.

2. How do you calculate permutations?

The formula for calculating permutations is n!/(n-r)!, where n is the total number of objects and r is the number of objects being selected.

3. Can you give an example of calculating permutations?

Sure, let's say you have 6 books on a shelf and you want to arrange 3 of them. The number of permutations would be 6!/(6-3)! = 6!/3! = 6x5x4 = 120. So there are 120 different ways you can arrange the 3 books on the shelf.

4. How is calculating permutations useful in science?

Permutations are useful in many areas of science, such as genetics, statistics, and chemistry. They can help us understand different combinations and possibilities in experiments and data analysis.

5. Are there any other methods for calculating permutations?

Yes, there are other methods such as using a permutation calculator or using the factorial function on a calculator. However, it is important to understand the formula and concept behind permutations to accurately interpret the results.

Similar threads

  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
1
Views
935
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top