help needed: C++ to Processing
Hi all!
I am new to Processing and I need help with the code. I am looking for a way to get all possible combinations of elements which are separated in two groups, for example for 4 elements it would look like this:
(AC) = (AD)(AE)(AF)
(AD) = (AC)(AE)(AF)
(AE) = (AC)(AD)(AF)
(AF) = (AC)(AD)(AE)
(AC)(AD) = (AE)(AF)
(AC)(AE) = (AD)(AF)
(AC)(AF) = (AD)(AE)
(AC)(AD)(AE)(AF)
Below I attach code in C++ which gives an idea of an algorithm, but I was just wondering if there is a way to create the same thing in processing. Maybe it would help.. thank you ver much in advance for any suggestions!
void
getCombination( int k, int n) //generation of combinations{
int* A=new int[k]; //array for group 1
int * B= new int [n-k]; //array for group 2char Companies[20][3]={"AB","AC","AD","AE","AF","BB","BC","BD","BE","BF","CB","CC","CD","CE","CF","DB","DC","DD","DE","DF"}; // companies names
if (k<n){
for ( int i=1;i<=k;i++) //initialis arrayA[i]=i;
// algorithm execution
int p=k;
while(p>=1 && res>0)
{
res--;
int z=1;
for(int ii=1; ii<=n;ii++)
{
bool ok=1;
for(int jj=1; jj<=k;jj++)
if (ii==A[jj])
ok=0;
if(ok)
B[z++]=ii;
}
cnt++;
for(int j=1;j<=k;j++)
{
cout << "(" << Companies[A[j]] << ")" ;
myfile << "(" << Companies[A[j]] << ")" ;
}
cout << " = ";
myfile << " = ";
for(int ii=1; ii<=(n-k);ii++)
{
cout << "(" << Companies[B[ii]] << ")";
myfile << "(" << Companies[B[ii]] << ")";
}
cout << "\n";
myfile << "\n";
if(A[k]==n)
p--;
else
p=k;
if (p>=1){
for ( int ii=k; ii>=p;ii--)A[ii]=A[p]+ii-p+1;
}
}
}
else //last combination (if all companies are in the same group){
cnt++;
for(int j=1;j<=k;j++)
{
cout << "(" << Companies[j] << ")" ; //to screen
myfile << "(" << Companies[j] << ")" ; //to file
}
cout << "\n";
myfile << "\n";
}
}
int
_tmain( int argc, _TCHAR* argv[]){
int keyCode=0;while(keyCode!=113 && keyCode!=81) //113 and 81 code for Q and q
{
cnt=0;
int maxval=0; while (maxval<2 || maxval>20) //limitations - no more than 20 and no less than 2 companies{
cout << "Number of companies[2-20]? \n";
cout << "===========================\n";
char tmp[100];
cin >> tmp;
maxval = atoi(tmp);
}res=pow(2,double(maxval-1))-1; //verification Steerling number
cout << "Summary: " << res + 1 << "\n"; // add 1 to account for the last combination when all companies are in the same group
myfile << "Summary: " << res + 1 << "\n";
myfile << "============================================\n";
for(int i=1;i<=maxval;i++)
{
getCombination(i,maxval);
}
cout << "----------\n";
cout << "Total: " << cnt << "\n";
myfile << "----------\n";
myfile << "Total: " << cnt << "\n";
cout << "Press any key to repeat or 'Q' for quit \n";
keyCode=_getch();
}
myfile.close();
return 0;
}