Thread: Permutations

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Question Permutations

    Hi All!

    This is my code for generating the permutations for a number input,it takes an array of size n!*n where n is the input no., to find out the permutation.Please help me.

    FOLLOWS MY CODE:#include<iostream>

    void permute(int arr[],int pos);
    int Factorial(int number);

    int main(){

    int num =0, fact = 0,SIZE=0 ;
    cout<<"Enter the number to be Permuted: ";
    cin>>num;
    fact = Factorial(num);
    SIZE = (fact*num);
    int * Arr = new int [SIZE];
    int pos =0;
    permute(Arr,pos);
    return 0;
    }

    void permute(int arr[],int pos){

    int n,SIZE = 0;
    int temp ,num;

    SIZE = ((Factorial(num))*(num));
    arr = new int [SIZE];
    int *narr = new int [SIZE];
    for (int i=0; i < SIZE ;i++)
    narr[i] = arr[i];

    if (pos >=SIZE -1)
    {
    for(int i=0; i<SIZE ;i++)
    {
    cout<<arr[i];
    cout<<endl;
    }
    }
    else
    {
    for (int next = pos; next < SIZE; next++)
    {
    temp = narr[next];cout<<temp;
    narr[next] = narr[pos];cout<<narr[next];
    narr[pos] = temp;
    permute(narr, pos + 1);
    }
    }
    }
    int Factorial( int num)
    {

    int temp=0;

    if(num <= 1) return 1;
    else
    temp = num * Factorial(num - 1);
    return temp;
    }
    THANK U

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Please help me.
    I'd be glad to, what's the problem? It's nice if you actually let us know what's going wrong because very few of us have time to debug your entire program to find the problem.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Permutations
    By bumfluff in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2008, 12:33 PM
  2. recursive permutations
    By wd_kendrick in forum C Programming
    Replies: 7
    Last Post: 06-02-2008, 03:11 PM
  3. permutations of a string
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 05-23-2006, 08:52 AM
  4. Permutations
    By mekaj in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2006, 09:10 AM
  5. String permutations help
    By nickk in forum C Programming
    Replies: 4
    Last Post: 05-15-2004, 01:55 PM