1. ## Array of Arrays

Title self explainitory I've been trying to create a population of individuals each of which contains a string/or array of numbers.

For example: Individual A: 110010101101

then basically * 100 to get 100 copies.

I need to be able to call each individual and then call each element of the individual so i can mutate them.

here is what i tryed out:

Code:
```void GetPopulation(int (&Output), int (&Pop)[PopSize])

{
int index;
int j;

for ( index = 0; index < PopSize; index++)
{
for ( j = 0; j < 21; j++)

{
int Out;
Out = Output[j];

Pop[index][j] = Pop[index][Out];
cout << Pop[index][j];
cin.get();
}

}

}```
Output is our Individual, and contains 21 int's of (1,0)

I was hoping the code would populate 100 arrays Pop[index]
each of which would contain a copy of the original individual
Pop[j].

Then try and print out what is in the arrays: ie

Pop[1st ind][1-21] = "01010101010101" etc

then on to the next ad infinitum.

Sadly it only prints out "0" then crashes at the end Output and Pop are both declared in Main()

int Pop[PopSize];

Popsize is defined as 100

Is there anything grossly wrong,

And i understand that there maybe better ways of doing it, ie someone suggesting Vectors etc but until i learn about them i'd rather stick with plain old arrays Unless i fundementally have to change it Thanks for any help

Regards Wolfe 2. umm, your crash might be due to a incorrect loop size like in your for loop ( if it is 1 to big or to small your program will crash sometimes ) 3. Well i found out what is wrong i think.

this part

int Out;
Out = Output[j];

Pop[index][j] = Pop[index][Out];
cout << Pop[index][j];

Having Out as what is in [j] at the time may sound fine and dandy but stupid of my to think it would go into the array like that. 4. there we go works but still crashes.

Pop[index][j] = Output[j];
cout << Pop[index][j]; 5. > void GetPopulation(int (&Output), int (&Pop)[PopSize])
1. What are those & for ?
2. How are you calling this function
3. Do you get any warnings when you compile the code. 6. The &'s are for referencing both Output and Pop which are in main.

GetPopulation(Output, Pop);

is how function is called.

It works now. 7. If you are trying to get the program to work and not make it so hard on yourself, you should really switch to strings and vectors instead of arrays.

"10001010101" is basically a string, therefore there is not much reason to treat it as an array of ints.

If you want multiple copies of this, you can put them all in a vector of strings (vector<string> Individuals) where you can easily access and manipulate each member. 8. Originally Posted by Cdrwolfe
The &'s are for referencing both Output and Pop which are in main.
You don't need them arrays are automatically passed by refference. Or by pointer, depending on how you look at it. 9. Didn't realise that, thanks.

Well here is the whole thing for anyone interested.

Code:
```#include <iostream>
#include <stdio.h>
#include <string>
#define RAND_MAX 100
#define srand 12
#include <ctime>
#define PopSize 10
#define GATarget 1111111111

using namespace std;
using std::string;
using std::cout;
using std::endl;
using std::cin;

void GetUserInput(string &Input)

{
cout << "Please enter Amino Acid Sequence \"H,P\": " << endl;
cin >> Input;

cin.get();
}

void GetSetupString(string theInput, int (Output))
// To declare an Array as a reference from Main, syntax is differant then other
// references, ie: Type (&ArryName)[Size], try finding that in any tutorial :)

{

int index;

for (index = 0; index < theInput.length(); ++index)
{
switch (theInput[index])
{
case 'H':
Output[index] = 1;
break;
case 'P':
Output[index] = 0;
break;
default:
break;

}
cout << Output[index] ;
}

cin.get();
}

void GetPopulation(int (Output), int (Pop)[PopSize])

{
int index;
int j;

for ( index = 0; index < PopSize; index++)
{
for ( j = 0; j < 10; j++)

{

Pop[index][j] = Output[j];
cout << Pop[index][j];

}
cin.get();

}

}

int GetMutation (int (Pop)[PopSize])

{
//Don't forget to seed rand() each time you run the program,

int index;
int j;
int Num = 0;

do
{

for ( index = 0; index < PopSize; index++)
{
for ( j = 0; j < 10; j++)

{

if (rand()%100 > 50)

{

if ( rand()%100 < 5)
{
Pop[index][j] = 0;
}
else
{
Pop[index][j] = 1;
}
Num++;
}

else

{
//do nothing
}

cout <<  Pop[index][j] ;

}
cout << endl;//cin.get();
}
}
while (Pop[index][j] = GATarget);

cin.get();

}

int main ()

{
string Input;
int Output;
int Pop[PopSize];
//int size = Input.size();

GetUserInput(Input);
GetSetupString(Input, Output);

GetPopulation(Output, Pop);

GetMutation(Pop);
}```
Changed the mutation function so it runs through the entire population.

Also added a do while loop so that it continues until it matches some kind of target. Of which i haven't qutie got to work yet .

Now to try and build a cross over function, a little bit harder. 10. There are still some errors:

Concerning seeding the random number generator - I see you have included <ctime> (that's where the time functions are) but you don't seem to be using it anywhere. The point about putting
Code:
`srand((unsigned)time(NULL))`
in the main is that it produces a unique number (current calendar time) each time you run the program, and as a result the rand() function will produce a unique series of numbers each time.

Code:
`#define srand 12`
This doesn't seed the RNG. All it does is tells the compiler to replace each case of 'srand' with '12' in the code while compiling.

Then, why are you still redefining RAND_MAX?

Code:
```using namespace std; //what's the point of this
using std::string;           //... if you use this directives?
using std::cout;
using std::endl;
using std::cin;```
Another potential problem is using literal constants (like 10) everywhere. It would be easier to define the constants once and then use the constant. This way, if you want to change any of them later (and you seem to be doing this a lot) you wouldn't have to go through the whole code to replace all 10-s. 11. Don't #define them, use global const int. 12. Here is a Half fnished single bit crossover operator i was trying out using simple coding that of which i only know It compiles but i have yet to see if it really works.

Code:
```void GetCrossover(int (Pop)[PopSize])
{
int NewPop[PopSize];
int NewPop2[PopSize];
int index;
int j;
int Inc;

for (index = 0; index < PopSize; index++)
{
for (j = 0; j < 10; j++)
{
NewPop[index][j] = Pop[index][j];
NewPop2[index][j] = Pop[index][j];
}
}

for (index = 0; index < PopSize; index++)
{
Inc = index;

char  Check = 'Y';   // Reinitialise Check to 'Y' for each loop

for (j = 0; j < 10; j++)
{
if ( Check != 'X')
{
// If Not 'X' the perform this block
if (rand()%100 > 50)
{
Inc = index;

Check = 'X';
Pop[index][j] = NewPop[++Inc][j];
}
else
{
Pop[index][j] = NewPop[index][j];
}
}

else
// If Check = 'X' the perform this block
{

Pop[index][j] = NewPop[++Inc][j];
}

cout << Pop[index][j];

}
cout << endl;
cin.get();
}

}```
Thought some people might be interested, to tired at the moment to finish it off. 13. Do While loop help if possible.

Didn't think it needed a new thread.

I was trying to get a do while loop in Main () to continously run 3 functions until it reaches a condition

Code:
```int main ()

{
string Input;
int Output;
int Pop[PopSize];
//int size = Input.size();

GetUserInput(Input);
GetSetupString(Input, Output);

do
{
GetPopulation(Output, Pop);
GetMutation(Pop);
GetCrossover(Pop);
}
while (Pop[PopSize] != {1,0,1,0,1,0,1,0,1,0}); <-- error?

}```
But i get an error which seems simple enough to figure out yet i can't seem to do it.

syntax error before `{' token

I'am thinking it must do with {1,0,1,0,1,0,1,0,1,0} but i'am not sure.

Any help is appreciated. 14. is Pop[PopSize] a 2-D array? If it is then I don't think the
Code:
`{1,0,1,0,1,0,1,0,1,0}`
thing will work. I'd say make a bool type function, and pass the array into the function, and if it is ok, return true, or else return false. Then put that function into the do while loop. Have the function have a nested for loop or something. 15. You can't compare the arrays like that.

What you could do is make another array that contains the desired results. Then there are several things you could try:

1) just loop through both arrays checking if all elements are equal

2) a somewhat easier way would be to try the equal algorithm from the <algorithm> library.

3) the easiest way (as always) to achieve your goal is to use a string instead of the int arrays, which lets you simply compare the population string with a test string using the == operator

Code:
```std::string population;
std::string test = "101010101010";
do {
GetPopulation(population);
} while (population != test);``` Popular pages Recent additions 