GENERATE PERMUTATIONS PROGRAM

# Include <conio.h>
# Include <stdlib.h>
# Include <stdio.h>

using namespace std;

int result;
int major;
int permutation;
int aj;
/ / Factorial function:
         int factorial (int value)
{
         result = value;
         while (value> 1)
         {
               result = result * (value-1);
               value = value – 1;
               }
               return result;
               }
               

int main (int argc, char * argv [])
{
    int p, nq, max, x, i, j, n, k, r, s, tr, no;
    int a [100];
    div_t xx;
    
    for (i = 0; i <100; i + +)
    {
        a [i] = 0;
        }
        / / Display the opening
        printf (“Generate PERMUTATIONS PROGRAM ———- ———— \ n”);
        printf (“———— By RAHMADIKA Kurniawan ———— \ n”);
        printf (“——————- 11,018,028 ——————— \ n”);
               
        / / Input value of n (the number of data <maksimal 100>)
        printf (“Enter the value of n:”);
        scanf (“% d”, & n);
        / / Input data into the array
        for (i = 1; i <= n; i + +) / / repeat for all the data to the data to the nth
        {
            printf (“Enter the value set of a [% d]:”, i);
            scanf (“% d”, & a [i]);
            }
            / / Input value of r
            printf (“r:”) scanf (“% d”, & tr);
            
            / / Compute the permutation
            p = factorial (n);
            nq = factorial (n-tr);
            if (nq == 0) nq = 1;
            max = p / nq;
            printf (“Permutation Value:% d \ n \
            
            Press Enter to See Results Generate Permutations … \ n “, max);
            
            getche (); / / function reads the character of keyboard
            no = 1; / / variable to display the number
            
            / / To generate permutations with
            / / Generate the next-permutation algorithm
            
            / / Generate as much as the permutation
            for (x = 1; x <= max; x + +)
            {
                printf (“% 3d.”, no);
                for (i = 1; i <= tr; i + +)
                    printf (“% d”, a [i]);
                printf (“\ n”, a [i]);
                no + +;
                
                j = n – 1;
                while (a [j]> a [j +1])
                      j = j – 1; / / j is the largest subcript with aj <aj +1
                k = n;
                while (a [j]> a [k])
                           k = k – 1; / / ak is the smallest integer and more
                big> aj;
                / / Exchange aj and ak
                i = a [k];
                a [k] = a [j];
                a [j] = i;
                
                r = n;
                s = j + 1;
                while (r> s)
                {
                      / / Exchange and as ar
                i = a [r];
                a [r] = a [s];
                a [s] = i;
                
                r = r – 1;
                s = s + 1;
                }
            }
            getch ();
            }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s