I am new to C++ and programming in general. (Only 1 years experience with python and some perl)
I wrote a program that creates all possible words using the character set (char_set) = 'abcdefghijklmnopqrstuvwxyz' from a word length of 1 to a word length of 5.
x1 = 0
x2 = 0
It first takes char_set[x1] and sets that to the variable word. Then adds 1 to x1. Then repeats. Then once x1 == the length of char_set, it adds a new letter to the word. So word now equals char_set[x1]+char_set[x2] and repeats.
Is this an efficient way to do this?
I don't get any errors but i want to know if i am doing something wrong.
Any help/suggestions/positive criticism is greatly appreciated.
Combinations is a common problem but not exactly a trivial one! Took me a bit to figure out where to start.
Instead of making if-statements 9 levels deep (that's never efficient!) I'd keep a list of what I'm still allowed to use instead. No doublethink needed if incorrect values aren't still around to grab. Also, an array of what order to grab things in which you can update as you go, and stop when it goes past the end.
Writing this with C++ strings will be much less efficient than C-strings. By treating a C-string like an array we can do, often with single instructions, what C++ would need entire function calls to do.
It run through all 7,893,600 combinations in 1.3 seconds on my 2.3GHz Athlon64, and I've proven all outputs are unique like so:
..if there were any duplicates, 'sort -u' would have removed them and reduced the count from wc -l.
Last edited by Corona688; 10-25-2010 at 02:58 PM..
Reason: more comments igor!