orage04
April 18th, 2004, 05:13 PM
Hi,
I have some code which produces all anagrams of the command line argument give to the program. This is fine but with slightly longer words this takes hours. I have recently been looking at ways to speed it up and multi-threading seemed a good idea.
Here is my code so far:
# include <iostream.h>
# include <string.h>
# include <conio.h>
# include <fstream.h>
// Few Prototypes
void rotate_array (char*,int) ;
void print_permutations(char*,int) ;
// Create A FileStream
////ifstream openfile("D:\\Documents and Settings\\Administrator\\My Documents\\wordlists\\enable1.txt");
// Max Number Of Elements
const int MAX_LEN = 20;
// The Counter
int counter = 0;
// Prints the different permutations of the array of elements
// recursively.
void print_permutations(char *array,int pos)
{ char store[MAX_LEN]; //
strcpy(store,array); // Save the array
for(int i=pos;i<strlen(array);++i)
{ rotate_array(array,pos); // Rotate the array
if(pos==strlen(array)-1)
{ cout<<array<<endl;
counter++; // Inc the counter
}
// call print_perm.. recursively
if((pos+1)<strlen(array))
print_permutations(array,pos+1);
}
strcpy(array,store); // Restore the array
}
int main(int argc,char *argv[])
{ if(argc>1)
{ print_permutations(argv[1],0);
cout<<endl<<"Number of permutations = "<<counter<<endl;
}
else
cout<<argv[0]<<" <elements for eg 1234.. or abcd...> ";
}
//
// This function rotates a part of the array
//
void rotate_array(char *array, int pos)
{ char temp = array[pos];
for(int i=pos;i<strlen(array);++i)
{ char temp2 = array[i+1];
array[i+1] = temp;
temp = temp2 ;
}
array[pos] = array[strlen(array)-1];
array[strlen(array)-1] = 0;
}
Is there a simple way of multi threading this, this seemed very complicated to do but i have never done any multi-threading before.
Thanks in advance.
I have some code which produces all anagrams of the command line argument give to the program. This is fine but with slightly longer words this takes hours. I have recently been looking at ways to speed it up and multi-threading seemed a good idea.
Here is my code so far:
# include <iostream.h>
# include <string.h>
# include <conio.h>
# include <fstream.h>
// Few Prototypes
void rotate_array (char*,int) ;
void print_permutations(char*,int) ;
// Create A FileStream
////ifstream openfile("D:\\Documents and Settings\\Administrator\\My Documents\\wordlists\\enable1.txt");
// Max Number Of Elements
const int MAX_LEN = 20;
// The Counter
int counter = 0;
// Prints the different permutations of the array of elements
// recursively.
void print_permutations(char *array,int pos)
{ char store[MAX_LEN]; //
strcpy(store,array); // Save the array
for(int i=pos;i<strlen(array);++i)
{ rotate_array(array,pos); // Rotate the array
if(pos==strlen(array)-1)
{ cout<<array<<endl;
counter++; // Inc the counter
}
// call print_perm.. recursively
if((pos+1)<strlen(array))
print_permutations(array,pos+1);
}
strcpy(array,store); // Restore the array
}
int main(int argc,char *argv[])
{ if(argc>1)
{ print_permutations(argv[1],0);
cout<<endl<<"Number of permutations = "<<counter<<endl;
}
else
cout<<argv[0]<<" <elements for eg 1234.. or abcd...> ";
}
//
// This function rotates a part of the array
//
void rotate_array(char *array, int pos)
{ char temp = array[pos];
for(int i=pos;i<strlen(array);++i)
{ char temp2 = array[i+1];
array[i+1] = temp;
temp = temp2 ;
}
array[pos] = array[strlen(array)-1];
array[strlen(array)-1] = 0;
}
Is there a simple way of multi threading this, this seemed very complicated to do but i have never done any multi-threading before.
Thanks in advance.