1

I have an array and I need all the possible permutations of this array given the following constraints:

The possible values are: 0.25,0.5,0.75,1.0,1.25,1.5,1.75,2.0.

The array has 18 elements. Each of the elements can contain one of the above values. Repeating is allowed, for example: 0.25,0.25,0.5 etc...

What type of iteration would I need to do to cover every permutation (not combination)?

for(...)
{
   std::vector<float> array;

   for(...)
   array.push_back...
}

etc

My question does not seek to find all permutations of a given array:

I just want:

for example:
0.25 0.25 0.25
0.5 0.25 0.25
1.0 0.25 0.25
...
All the way to 
2.0 2.0 2.0
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • possible duplicate of [Finding all the unique permutations of a string without generating duplicates](http://stackoverflow.com/questions/9217839/finding-all-the-unique-permutations-of-a-string-without-generating-duplicates) – MooseBoys Apr 15 '15 at 03:20
  • To my mind "every permutation" and "not all permutations" appear to be little mutually contradictory? – Cheers and hth. - Alf Apr 15 '15 at 03:44
  • Why 0.75 is missing after 0.5? – Slava Apr 15 '15 at 03:45

1 Answers1

0

Here's a program that will print all those combinations(be weary, there's a load of them) to a file called combinations.txt. Be aware though, it will take a while, and the output will be HUGE, since there are a LOT of those combinations. As for how it works, it's a quick-and-dirty solution, where i have an int array with 18 elements. I have used int since it's easier for us to work in integer arithmetic, but the output is converted to your desired format by dividing by 100. Now, the app prints each combination out in the file, and then increments the first array element by 25. If the element goes over 200 ( or 2.00 for you ), it sets that element to 25, and increments the next one in the array by 25. It works just like counting, when you reach 9, the first digit wraps around and adds one to the next higher power.

#include <cstdio>
#include <cmath>

FILE *pOut;

void PrintCombinationToFile ( int *pArray )
{
    fprintf ( pOut,  "\n" );
    for ( unsigned i = 0; i < 18; i++ )
        fprintf ( pOut, "%.2f ", pArray[i] * 1.0 / 100.0 );
}


int IncrementArray ( int *pArray )
{
    unsigned i = 0;
    pArray[0] += 25;
    while ( pArray[i] > 200 )
    {
        if ( i == 18 )
            return 0;
        pArray[i] = 25;
        pArray[i+1] += 25;
        i++;
    }
    return 1;
}

int main()
{
    int initialArray[18];
    int i;

    pOut = fopen ( "combinations.txt", "w" );
    if ( !pOut )
    {
        printf ( "\nUnable to open a new file." );
        return 1;
    }

    for ( i = 0; i < 18; i++ )
        initialArray[i] = 25;

    PrintCombinationToFile(initialArray);

    while(IncrementArray(initialArray))
        PrintCombinationToFile(initialArray);

    fflush ( pOut );
    fclose ( pOut );
    printf ( "Done!")
    return 0;
}
Nazara
  • 124
  • 4