Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Calculating permutaions.

  1. Jul 6, 2007 #1
    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 (Text):

    int perm(int n)
    int perms[MAXROW][MAXCOL], i,j;
    so far as you can see im printing the list order i.e if n=2
    Code (Text):

    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.
  2. jcsd
  3. Jul 6, 2007 #2


    User Avatar
    Science Advisor
    Homework Helper

    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 (Text):

    int * cycle (int *numbers, int n) {
       int tmp,i;

       for(i=0;i<n-1;i++) {
       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++) {

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

       if(1==n) {
       } else {
          for(i=0;i<n;i++) {

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

       for(i=0;i<MAXCOLS;i++) {
  4. Jul 6, 2007 #3


    User Avatar
    Gold Member

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Calculating permutaions.
  1. Pocket calculators (Replies: 3)

  2. Calculation in c++ (Replies: 3)