A couple of comments.
1. Learn how to indent code. Yours is mostly on the left hand side, with a few random lines (nothing to do with the flow of the program) indented one level.
2. Closing braces should be on a line by themselves, so
computer_sequence[4] = temp_sequence;}
Would be
Code:
computer_sequence[4] = temp_sequence;
}
If you're ever in the unfortunate position of trying to deal with a mis-placed brace, then having them hiding at the ends of lines will just make it that much harder.
3. Don't be afraid to put in some blank lines, say between the variables and the first statement, or between separate functional blocks within a function.
4. All your _first, _second etc variables are screaming to be an array.
5. For something supposed to be implementing a GA, I would have expected to see a couple of functions called say 'evolve', where two parent 'genes' are combined to produce the next generation, and a 'mutate' which occasionally makes a random change to the gene. I would also like to have seen a 'survives' function which tests the fitness of any single gene to make it into the next generation.
Your code, c/o indent -kr -nut -ts2 foo.cpp
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
srand(time(0));
int user_sequence;
int user_first;
int user_second;
int user_third;
int user_fourth;
int user_fifth;
int user_sixth;
int computer_sequence[5];
int computer_first[5];
int computer_second[5];
int computer_third[5];
int computer_fourth[5];
int computer_fifth[5];
int computer_sixth[5];
int fitness[5];
int iteration;
int temp_fitness;
int temp_sequence;
int random;
double split;
int post_split_beginning;
int post_split_end;
int post_split_fitness;
int post_split_first;
int post_split_second;
int post_split_third;
int post_split_fourth;
int post_split_fifth;
int post_split_sixth;
int post_split_sequence;
cout << "Instructions:" << endl <<
"The program will prompt for a sequence of six numbers (0 or 1) and then"
<< endl <<
"the program will use the genetic algorithm to determine the sequence."
<< endl << endl;
cout << "Enter a sequence: ";
cin >> user_sequence;
user_first = user_sequence / 100000;
user_second = user_sequence / 10000 % 10;
user_third = user_sequence / 1000 % 10;
user_fourth = user_sequence / 100 % 10;
user_fifth = user_sequence / 10 % 10;
user_sixth = user_sequence % 10;
iteration = 0;
do {
fitness[iteration] = 0;
computer_first[iteration] = rand() % 2;
computer_second[iteration] = rand() % 2;
computer_third[iteration] = rand() % 2;
computer_fourth[iteration] = rand() % 2;
computer_fifth[iteration] = rand() % 2;
computer_sixth[iteration] = rand() % 2;
if (user_first == computer_first[iteration])
fitness[iteration] = fitness[iteration] + 1;
if (user_second == computer_second[iteration])
fitness[iteration] = fitness[iteration] + 1;
if (user_third == computer_third[iteration])
fitness[iteration] = fitness[iteration] + 1;
if (user_fourth == computer_fourth[iteration])
fitness[iteration] = fitness[iteration] + 1;
if (user_fifth == computer_fifth[iteration])
fitness[iteration] = fitness[iteration] + 1;
if (user_sixth == computer_sixth[iteration])
fitness[iteration] = fitness[iteration] + 1;
computer_sequence[iteration] =
computer_first[iteration] * 100000 +
computer_second[iteration] * 10000 +
computer_third[iteration] * 1000 +
computer_fourth[iteration] * 100 +
computer_fifth[iteration] * 10 + computer_sixth[iteration];
cout << "Computer sequence: " << setfill('0') << setw(6) <<
computer_sequence[iteration] << endl;
cout << "Fitness score: " << fitness[iteration] << endl;
if (fitness[iteration] == 6) {
system("pause");
return 0;
}
iteration = iteration + 1;
} while (iteration < 5);
if (fitness[0] > fitness[1])
{
temp_fitness = fitness[0];
fitness[0] = fitness[1];
fitness[1] = temp_fitness;
temp_sequence = computer_sequence[0];
computer_sequence[0] = computer_sequence[1];
computer_sequence[1] = temp_sequence;
}
if (fitness[1] > fitness[2])
{
temp_fitness = fitness[1];
fitness[1] = fitness[2];
fitness[2] = temp_fitness;
temp_sequence = computer_sequence[1];
computer_sequence[1] = computer_sequence[2];
computer_sequence[2] = temp_sequence;
}
if (fitness[2] > fitness[3])
{
temp_fitness = fitness[2];
fitness[2] = fitness[3];
fitness[3] = temp_fitness;
temp_sequence = computer_sequence[2];
computer_sequence[2] = computer_sequence[3];
computer_sequence[3] = temp_sequence;
}
if (fitness[3] > fitness[4])
{
temp_fitness = fitness[3];
fitness[3] = fitness[4];
fitness[4] = temp_fitness;
temp_sequence = computer_sequence[3];
computer_sequence[3] = computer_sequence[4];
computer_sequence[4] = temp_sequence;
}
cout << setw(6) << computer_sequence[3] << " " << setw(6) <<
computer_sequence[4] << endl;
random = rand() % 5 + 1;
split = pow(10.0, random);
cout << random << " " << split << endl;
post_split_beginning = 0;
post_split_end = 0;
post_split_first = 0;
post_split_second = 0;
post_split_third = 0;
post_split_fourth = 0;
post_split_fifth = 0;
post_split_sixth = 0;
post_split_fitness = 0;
post_split_beginning = computer_sequence[3] / int (split);
post_split_end = computer_sequence[4] % int (split);
post_split_sequence =
post_split_beginning * int (split) + post_split_end;
post_split_first = post_split_sequence / 100000 % 10;
post_split_second = post_split_sequence / 10000 % 10;
post_split_third = post_split_sequence / 1000 % 10;
post_split_fourth = post_split_sequence / 100 % 10;
post_split_fifth = post_split_sequence / 10 % 10;
post_split_sixth = post_split_sequence % 10;
if (user_first == post_split_first)
post_split_fitness = post_split_fitness + 1;
if (user_second == post_split_second)
post_split_fitness = post_split_fitness + 1;
if (user_third == post_split_third)
post_split_fitness = post_split_fitness + 1;
if (user_fourth == post_split_fourth)
post_split_fitness = post_split_fitness + 1;
if (user_fifth == post_split_fifth)
post_split_fitness = post_split_fitness + 1;
if (user_sixth == post_split_sixth)
post_split_fitness = post_split_fitness + 1;
cout << "Computer sequence: " << setw(6) << post_split_sequence <<
endl << "Fitness score: " << post_split_fitness << endl;
if (post_split_fitness == 6) {
system("pause");
return 0;
};
if (post_split_fitness != 6) {
cout << "Still work to do.";
};
system("pause");
return 0;
}