Programming Homework: Robot Genetic Algorithm : <p>This program is built to run 10000 generations of 200 robots, allowing evolution to shape the digits in the robot's "DNA". The problem I'm having is that, even though there is nothing but numbers going into the 'robotGenes' array, the resulting DNA that prints is a random collection of letters, numbers and symbols. I have absolutly no idea why. I have tried switching up where I declare and assign my variables, unfortunatly its all been in vane. Plz halp. </p>

<p>P.s. I'm posting the entire program code here, as I am unsure what is causing the problem, though I am positive that I am simply being daft. Any help is greatly appreciated.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;stdlib.h&gt;
#include &lt;cmath&gt;

using namespace std;

int randBattery = 0,
randRobot = 0,
randDirect = 0,
board[144] = { },
testDirect = 0,
randTurn = 0,
randTurnDirect = 0,
genAdd = 0,
masterFit = 0,
abc = 0;

double fitness[10000] = { },
winners[5] = { };

std::string startCond;

unsigned char robotGenes[200][12] = { },
test[12] = { };

class Display {
    public:
        int results() {
            cout &lt;&lt; "\n\n\n-Evolution Winning Avg. Fitness Ratings-\n\n";
            cout &lt;&lt; "1. " &lt;&lt; winners[1];
            cout &lt;&lt; "\n DNA: ";
            for (int G = 1; G &lt;= 12; G++)
            {
              cout &lt;&lt; robotGenes[1][G];
            }
            cout &lt;&lt; "\n\n2. " &lt;&lt; winners[2];
            cout &lt;&lt; "\n DNA: ";
            for (int G = 1; G &lt;= 12; G++)
            {
              cout &lt;&lt; robotGenes[41][G];
            }
            cout &lt;&lt; "\n\n3. " &lt;&lt; winners[3];
            cout &lt;&lt; "\n DNA: ";
            for (int G = 1; G &lt;= 12; G++)
            {
              cout &lt;&lt; robotGenes[81][G];
            }
            cout &lt;&lt; "\n\n4. " &lt;&lt; winners[4];
            cout &lt;&lt; "\n DNA: ";
            for (int G = 1; G &lt;= 12; G++)
            {
              cout &lt;&lt; robotGenes[121][G];
            }
            cout &lt;&lt; "\n\n5. " &lt;&lt; winners[5];
            cout &lt;&lt; "\n DNA: ";
            for (int G = 1; G &lt;= 12; G++)
            {
              cout &lt;&lt; robotGenes[161][G];
            }
            return 0;
        };
};

class RobotSex {
    public:
        int bang() {
            int newGen, newGen2, B;
            newGen = 101;
            newGen2 = 102;
            B = 2;
            for (int C = 1; C &lt;= 100; C += 2)
            {
                robotGenes[newGen][6] = robotGenes[C][6];
                robotGenes[newGen][7] = robotGenes[C][7];
                robotGenes[newGen][8] = robotGenes[C][8];
                robotGenes[newGen][9] = robotGenes[C][9];
                robotGenes[newGen][10] = robotGenes[B][10];
                robotGenes[newGen][11] = robotGenes[B][11];

                robotGenes[newGen2][6] = robotGenes[B][6];
                robotGenes[newGen2][7] = robotGenes[B][7];
                robotGenes[newGen2][8] = robotGenes[B][8];
                robotGenes[newGen2][9] = robotGenes[B][9];
                robotGenes[newGen2][10] = robotGenes[C][10];
                robotGenes[newGen2][11] = robotGenes[C][11];

                newGen += 2;
                newGen2 += 2;
                B += 2;
            }
        };
};

class Genocide {
    public:
        int ethnicCleansing() {
            for (int x = 101; x &lt;= 200; x++)
            {
                for (int y = 6; y &lt;= 12; y++)
                {
                    robotGenes[x][y] = 0;
                }
            }
        };
};

class Sorting {
    public:
        int sortPower() {
            for (int P = 0; P &lt;= 200; P++)
            {
                for (int x = 1; x &lt;= 200; x++)
                {
                    int y;
                    y = x + 1;
                    if (robotGenes[x][12] &lt; robotGenes[y][12])
                        for (int Q = 1; Q &lt;= 12; Q++)
                            swap (robotGenes[x][Q], robotGenes[y][Q]);
                }
            }
        };

        int addGenFit() {
            for (int o = 1; o &lt;= 200; o++)
            {
                genAdd += robotGenes[o][12];
            }
            fitness[masterFit] = genAdd / 200;
            ::masterFit += 1;
        };

        int sortGenFit() {
            for (int P = 0; P &lt;= 10000; P++)
            {
                for (int x = 1; x &lt;= 9999; x++)
                {
                    int y;
                    y = x + 1;
                    if (fitness[x] &lt; fitness[y])
                        swap (fitness[x], fitness[y]);
                }
            }
            for (int o = 1; o &lt;= 2000; o++)
            {
                genAdd += fitness[o];
            }
            winners[1] = genAdd / 2000;

            for (int o = 2001; o &lt;= 4000; o++)
            {
                genAdd += fitness[o];
            }
            winners[2] = genAdd / 2000;

            for (int o = 4001; o &lt;= 6000; o++)
            {
                genAdd += fitness[o];
            }
            winners[3] = genAdd / 2000;

            for (int o = 6001; o &lt;= 8000; o++)
            {
                genAdd += fitness[o];
            }
            winners[4] = genAdd / 2000;

            for (int o = 8001; o &lt;= 10000; o++)
            {
                genAdd += fitness[o];
            }
            winners[5] = genAdd / 2000;
        };
};
class Establishing {
    public:
        int clearBoard() {
            for (int c = 0; c &lt;= 144; c++)  //Clearing board
            {
                board[c] = 0;
            }
            for (int x = 0; x &lt;= 12; x++)   //Establishing walls
            {
                board[x] = 9;
            }
            for (int x = 132; x &lt;= 144; x++)
            {
                board[x] = 9;
            }
            for (int x = 13; x &lt;= 121; x += 12)
            {
                board[x] = 9;
            }
            for (int x = 24; x &lt;= 132; x += 12)
            {
                board[x] = 9;
            }

        };
};

class Randomizer {
    public:
        int randBattery() {
            for (int t = 1; t &lt;= 4; t++)
            {
                srand(time(NULL));      //Random battery placement R1
                ::randBattery = (rand() % 14+23);
                board[::randBattery] = 1;
            }
            for (int t = 1; t &lt;= 4; t++)
            {
                srand(time(NULL));      //Random battery placement R2
                ::randBattery = (rand() % 26+35);
                board[::randBattery] = 1;
            }
            for (int t = 1; t &lt;= 4; t++)
            {
                srand(time(NULL));      //Random battery placement R3
                ::randBattery = (rand() % 38+47);
                board[::randBattery] = 1;
            }
            for (int t = 1; t &lt;= 4; t++)
            {
                srand(time(NULL));      //Random battery placement R4
                ::randBattery = (rand() % 50+59);
                board[::randBattery] = 1;
            }
            for (int t = 1; t &lt;= 4; t++)
            {
                srand(time(NULL));      //Random battery placement R5
                ::randBattery = (rand() % 62+71);
                board[::randBattery] = 1;
            }
            for (int t = 1; t &lt;= 4; t++)
            {
                srand(time(NULL));      //Random battery placement R6
                ::randBattery = (rand() % 74+82);
                board[::randBattery] = 1;
            }
            for (int t = 1; t &lt;= 4; t++)
            {
                srand(time(NULL));      //Random battery placement R7
                ::randBattery = (rand() % 85+95);
                board[::randBattery] = 1;
            }
            for (int t = 1; t &lt;= 4; t++)
            {
                srand(time(NULL));      //Random battery placement R8
                ::randBattery = (rand() % 98+107);
                board[::randBattery] = 1;
            }
            for (int t = 1; t &lt;= 4; t++)
            {
                srand(time(NULL));      //Random battery placement R9
                ::randBattery = (rand() % 110+119);
                board[::randBattery] = 1;
            }
            for (int t = 1; t &lt;= 4; t++)
            {
                srand(time(NULL));      //Random battery placement R10
                ::randBattery = (rand() % 122+131);
                board[::randBattery] = 1;
            }
        };

        int randPlacement() {
            for (int t = 1; t &lt;= 200; t++)
            {
                srand(time(NULL));      //Random robot placement &amp; resets power level
                randRobot = (rand() % 62+71);
                robotGenes[t][1] = randRobot;
                robotGenes[t][12] = 5;
            }
        };

        char randDirect() {
            for (int d = 1; d &lt;= 200; d++)
            {
                srand(time(NULL));      //Random robot direction
                ::randDirect = (rand() % 62+71);
                robotGenes[d][5] = ::randDirect;
            }
        };
};

class Reader {
    public:
        int readDNA() { 
            do {
                if (board[test[1]] == 1)    //Pickup battery
                    test[12] += 5;
                if (test[9] == 1)       //Check for wall
                {
                    if (board[test[2]] == 9)
                    {
                        if (test[5] == 1)
                            test[5] = 4;
                        if (test[5] == 2)
                            test[5] = 1;
                        if (test[5] == 3)
                            test[5] = 2;
                        if (test[5] == 4)
                            test[5] = 3;
                    }
                    if (board[test[2]] == 9 &amp;&amp; board[test[3]] == 9)
                    {
                        if (test[5] == 1)
                            test[5] = 2;
                        if (test[5] == 2)
                            test[5] = 3;
                        if (test[5] == 3)
                            test[5] = 4;
                        if (test[5] == 4)
                        test[5] = 1;
                    }
                }
                if (test[7] == test[6])     //Changing direction after # of moves
                    if (test[6] % 2 == 0)       //Turning left
                    {
                        if (test[5] == 1)
                            test[5] = 4;
                        if (test[5] == 2)
                            test[5] = 1;
                        if (test[5] == 3)
                            test[5] = 2;
                        if (test[5] == 4)
                            test[5] = 3;
                    }
                    else                //Turning right
                    {
                        if (test[5] == 1)
                            test[5] = 2;
                        if (test[5] == 2)
                        test[5] = 3;
                        if (test[5] == 3)
                        test[5] = 4;
                        if (test[5] == 4)
                            test[5] = 1;
                    }
                if (test[10] == 1)      //Left sensor checking for battery, TURN LEFT
                    if (test[5] == 1)
                        test[5] = 4;
                    if (test[5] == 2)
                        test[5] = 1;
                    if (test[5] == 3)
                        test[5] = 2;
                    if (test[5] == 4)
                        test[5] = 3;
                if (test[11] == 1)      //Right sensor checking for battery, TURN RIGHT
                    if (test[5] == 1)
                        test[5] = 2;
                    if (test[5] == 2)
                        test[5] = 3;
                    if (test[5] == 3)
                        test[5] = 4;
                    if (test[5] == 4)
                        test[5] = 1;
                if (test[5] == 1)           //Face north 
                {
                    test[2] = test[1] + 12;
                    ::test[3] = test[1] - 1;
                    test[4] = test[1] + 1;

                    test[1] += 12;          //Move north
                    test[2] += 12;
                    ::test[3] += 12;
                    test[4] += 12;
                }
                if (test[5] == 2)           //Face east 
                {
                    test[2] = test[1] + 1;
                    ::test[3] = test[1] + 12;
                    test[4] = test[1] - 12;

                    test[1] += 1;           //Move east
                    test[2] += 1;
                    ::test[3] += 1;
                    test[4] += 1;
                }
                if (test[5] == 3)           //Face south
                {
                    test[2] = test[1] - 12;
                    ::test[3] = test[1] + 1;
                    test[4] = test[1] - 1;

                    test[1] -= 12;          //Move south
                    test[2] -= 12;
                    ::test[3] -= 12;
                    test[4] -= 12;
                }
                if (test[5] == 4)           //Face west
                {
                    test[2] = test[1] - 1;
                    ::test[3] = test[1] - 12;
                    test[4] = test[1] + 12;

                    test[1] -= 1;           //Move west
                    test[2] -= 1;
                    ::test[3] -= 1;
                    test[4] -= 1;
                }
                test[7] += 1;
                test[12] -= 1;
            } while (test[12] &gt; 1 &amp;&amp; test[7] &lt; 24); 
        };
};

class Running {
    public:
        void runRobot() {
            for (int q = 1; q &lt;= 200; q++)
            {
                for (int z = 1; z &lt;= 12; z++)
                {
                    ::test[z] = robotGenes[q][z];
                }
                Reader objectDNA;
                objectDNA.readDNA();
                for (int z = 1; z &lt;= 12; z++)
                {
                    ::robotGenes[q][z] = test[z];
                }
            }
        };
};

int main()
{
    //System Greeting
    cout &lt;&lt; "Enter anything to start simulation.\n\n";
    cin &gt;&gt; startCond;

    masterFit = 1;

    //Setting starting genes
    for (int t = 1; t &lt;= 200; t++)
    {
        srand(time(NULL));
        randTurn = (rand() % 10+1);
        robotGenes[t][6] = randTurn;
    }
    for (int o = 1; o &lt;= 40; o++)
    {
        robotGenes[o][8] = 1;
        robotGenes[o][9] = 0;
        robotGenes[o][10] = 0;
        robotGenes[o][11] = 0;
    }
    for (int o = 41; o &lt;= 80; o++)
    {
        robotGenes[o][8] = 0;
        robotGenes[o][9] = 1;
        robotGenes[o][10] = 1;
        robotGenes[o][11] = 1;
    }
    for (int o = 81; o &lt;= 120; o++)
    {
        robotGenes[o][8] = 1;
        robotGenes[o][9] = 1;
        robotGenes[o][10] = 0;
        robotGenes[o][11] = 0;
    }
    for (int o = 121; o &lt;= 160; o++)
    {
        robotGenes[o][8] = 0;
        robotGenes[o][9] = 0;
        robotGenes[o][10] = 0;
        robotGenes[o][11] = 0;
    }
    for (int o = 161; o &lt;= 200; o++)
    {
        robotGenes[o][8] = 1;
        robotGenes[o][9] = 0;
        robotGenes[o][10] = 1;
        robotGenes[o][11] = 1;
    }

    //Running sims (not the game)

    Randomizer objectRaDirect;
    objectRaDirect.randDirect();

    Randomizer objectRaPlace;
    objectRaPlace.randPlacement();

    for (int sims = 1; sims &lt;= 10000; sims++)
    {
        Establishing objectClear;
        objectClear.clearBoard();

        Randomizer objectRaBattery;
        objectRaBattery.randBattery();

        Running objectRun;
        objectRun.runRobot();

        abc += 1;
        cout &lt;&lt; "\n\nGeneration " &lt;&lt; abc &lt;&lt; " is complete.\n";

        Sorting objectPower;
        objectPower.sortPower();

        Sorting objectGenFit;
        objectGenFit.addGenFit();

        Genocide objectHitler;
        objectHitler.ethnicCleansing();

        RobotSex objectSex;
        objectSex.bang();
    }

    Sorting objectWin;
    objectWin.sortGenFit();

        for (int B = 1; B &lt;= 5; B++)
    {
      winners[B] = abs (winners[B]);
    }

    Display objectEnd;
    objectEnd.results();
};
</code></pre>
