Being a newbie to C++, I thought I had better get used to using strings, so I have written a Genetic Algorithm to come up with a random string of characters which it then mutates and "breeds" from, ultimately to match a target phrase.
I have a number (#defined as num) of such offspring.
Now, when there are only 2 offspring in each iteration, the program runs perfectly. But as soon as I use a different number...
#define num 4
the program doesn't even seem to bother running through the algorithm.
I am utterly lost as to why this should be the case since i have used the macro name throughout the program to denote the loop parameters and array offsets.
If anyone can give me a hand in trying to locate the reason for this problem, then I'd be much obliged.
The program compiles in the DJGPP compiler (in which it was written).
The program doesn't crash as such, it just exits early for some weird reason.
Don't worry about the weird target phrase, its a shakespearian quote. If you have read "The Blind Watchmaker" it will make sense!
Please feel free to criticise the code to your heart's content, I need all the help I can get!
Okay, got it working...I had to change a line to this:
code:--------------------------------------------------------------------------------int i = 0, j = 0, cc = 0, best = 0, max = 0, gen = 0;--------------------------------------------------------------------------------
It works as normal with 2, and when I tried it with 3 & 4 it was okay as well.
Wow, nice one dude.
Hmm, why do you think we need to init the variables with 0 when its the default beginning value?
I had to set gen to 0 on declaration to solve a counting problem that made the generation numbers 137849 when it should have been 45!
C/C++ doesn't initialise variables. Using int x merely allocates space for that variable, what the contents of that memory location are is undefined (unless it's MSVC++'s debug mode in which case it's 0xCDCD ).
You have to initialise it yourself.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You". -- Linus Torvalds
This is not a bad thing, it allows you to make more efficient code. No point initialising a variable if you don't know what value it's going to start with.
Didn't know VC++ initialised it for you in debug mode... why 0xCDCD?
No idea. But it makes life easier because it can whinge at you if you use an uninitialised variable because if it prints 0xCDCD you know you've done something wrong.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You". -- Linus Torvalds
Wow, I didn't know that about C++ not initialising variables. I must have taken it for granted being such a VB enthusiast for all this time!
(good grief, these new-fangled Vis-yoo-all Baysic programmer people don't know they're born, being pampered...I remember when all this was fields and you could get to Aberdeen and back, buy a new suit and take a young lady to the pictures, and still have change from a tenner!)