Combinations with factorial

This is a discussion on Combinations with factorial within the C++ Programming forums, part of the General Programming Boards category; Would somebody help me with this. Let's take 4! (4 times 3 times 2 times 1 time = 24). This ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    8

    Combinations with factorial

    Would somebody help me with this.

    Let's take 4! (4 times 3 times 2 times 1 time = 24). This is easy to do but I want to have all 24 combinations on the screen.

    Thanks

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Code:
    for (x=2; x>0; x--)
    { for (y=2; y>0; y--)
       { if(y==x) continue;
          cout<<x<<","<<y<<endl;
        }
    }
    There is a little code that will do it for 2!. You can expand on that for 4!.

  3. #3
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    You are creating *permutations*.
    Here is a function for creating all permutations in order (there is one in STL too called next_permutation using templates):

    Code:
    #include <iostream>
    void swap(int &n1,int &n2) {
        int temp=n1;
        n1=n2;
        n2=temp;
    }
    bool next_permutation(int *table, int length) {
        int key=length-1, i=length-1;
        while(key>0 && table[key]<=table[key-1])
            key--;
        if(key==0)return false;
        key--;
        while(table[key]>=table[i])
            i--;
        swap(table[key],table[i]);
        for(i=length-1,key++;key<i;key++,i--)
            swap(table[key],table[i]);
        return true;
    }
    int main () {
        int i, sequence[10]={1,2,3,4,5,6,7,8,9,10};
        do {
          for(i=0;i<10;i++)
    	std::cout<<sequence[i]<<" ";
          std::cout<<std::endl;
        } while(next_permutation(sequence,10));
        return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    8

    Factorial

    Thanks for the solutions. Is it possible to get a solution so I don't have to specify in advance the number of elements in the array.
    Fero

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I''m just guessing 'cuz I didn't look carefully (I'm probably wrong, but whatever), but looking at golfinguy's post, try this:

    Code:
    void showStuff(int n)
    {
    
    for (x=n; x>0; x--)
    { for (y=n; y>0; y--)
       { if(y==x) continue;
          cout<<x<<","<<y<<endl;
        }
    }
    
    }
    again, this is a wild guess based on a quick glance at golfinguy's post...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  2. Factorial
    By foxman in forum Contests Board
    Replies: 27
    Last Post: 07-11-2008, 07:59 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 01:00 AM
  4. Basic Factorial from 1-10
    By AaA in forum C Programming
    Replies: 20
    Last Post: 05-28-2005, 08:39 AM
  5. factorial output
    By maloy in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21