Combinatoric Permutations Algorithm

//By Alex McAlpine

//CSC-134 Charles Chen

//02-04-2003

//This program gives every possible integer combo for any number of given integers.

//Permutations.exe ver1.0.2

//PreProcessor Directives

#include <iostream.h>

#include <stdlib.h>

#include <conio.c>

//Function Prototypes

void Aquire(void);

int Mixer(int,int,int[],bool[],int[]);

//structures

struct numlist

{

int value;

struct numlist *next;

};

//Global Variables

int counter = 0;

//List Pointers

numlist *head;

numlist *prev;

numlist *now;

//MAIN FUNCTION

int main()

{

Aquire();

//Assembling Boolian dummyArray for permutation tests assuring no repeats

bool *dummyB = new bool[counter];

//Assembling integer Array for results

int *temp = new int[counter];

for(int index = 0;index < counter;index++)

{

dummyB[index] = 1;

}

//Definition of Ascending Sort Algorithm

cout<<endl<<"Sorting into Ascending order for neatness:"<<endl;

//Creating a temporary integer array for the structure allocations

int *numArray = new int[counter];

//Setting now to the first of the allocation list

now = head;

//Charging the Array

for(int chargTest = 0;chargTest < counter;chargTest++)

{

numArray[chargTest] = now->value;

now = now->next;

}

// Freeing the Memory

struct numlist *buffer;

now = head;

while(now)

{

buffer = now->next;

delete now;

now = buffer;

}

//Organizing from smallest to greatest with additive index sort

for(int index = 0;index < counter - 1; index++)

{

for(int index2 = index + 1; index2 < counter;index2++)

{

if(numArray[index] > numArray[index2])

{

numArray[index] = numArray[index] + numArray[index2];

numArray[index2] = numArray[index] - numArray[index2];

numArray[index] = numArray[index] - numArray[index2];

}

}

}

for(int index = 0;index < counter; index++)

{

cout<<numArray[index]<<endl;

}

cout<<endl<<"The sequences are as follows:"<<endl;

//end of asending order sequence -> this is not in a function because of scope problums

//with numArray.

Mixer(0,counter,temp,dummyB,numArray);

//cleaning up the rest of the dynamicly allocated variables.

delete dummyB;

delete numArray;

delete temp;

system("PAUSE");

return 0;

}

//Function definitions

//Definition of Aquire()

inline void Aquire(void)

{

int Input = 0 ;

cout<<"Please enter any given number of integers.\n";

cout<<"After each integer press enter, when you are\n";

cout<<"done enter 0\n";

//Loop until user enters 0.

do

{

cin>>Input;

if(Input)

{

counter++;

now = new numlist;

if(now == 0)

{

cout<<"Cannot Allocate Memory\nProgram Variables Will Be Lost";

exit(0);

}

now->value = Input;

if(prev == 0)

{

head = now;

}

else

{

prev->next = now;

}

now->next = 0;

prev = now;

}

}

while(Input);

//Calculating the number of possible combinations

unsigned short ComboView = 1;

for(unsigned short step = counter;step > 0;step--)

{

ComboView*= step;

}

cout<<"There are "<<ComboView<<" possible combinations for "<<counter<<" different\n";

cout<<"integers."<<endl;

}

//Definition of Combinatoric Permutation Algorithm, using Recursion.

inline int Mixer(int list,int max,int tempArray[],bool dummy[],int ARRAY[])

{

if(list == max)

{

for(int index = 0; index < max;index++)

{

cout<<tempArray[index]<<" ";

}

cout<<endl;

}

else

{

for(int mloop = 0;mloop < max;mloop++)

if(dummy[mloop] == 1)

{

dummy[mloop] = 0;

tempArray[list] = ARRAY[mloop];

Mixer(list+ 1,max,tempArray,dummy,ARRAY);

dummy[mloop] = 1;

}

//list = 0;

}

return 1;

}

Click Button To Download Console Application

Click Button To Download Code

Click Button To View Visual Basic Version