### Help with Array

I'm trying to build a population for my Genetic Algorithm Project using an array. The number of the population is 10 and it consists of 28 binary bit. Here is my code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include #include #include using namespace std; int main(){ float binary [10][28]; float population_bin [10][29]; float population_dec [10][29]; int row, column; // Binary convertor// for (row = 1; row <= 10; row ++){ for (column = 0; column <= 27; column ++){ binary [row][column] = pow (2, column); } } //Binary Population// for (row = 1; row <= 10; row ++){ for (column = 0; column <= 27; column ++){ population_bin [row][column] = rand ()% 2; } } //Binary Conversion into Decimal// for (row = 1; row <= 10; row ++){ for (column = 0; column <= 27; column ++){ population_dec [row][column] = binary [row][column] * population_bin [row][column]; } } //Population Decoder// float A, B; for (row = 1; row <= 10; row ++){ A = 0; B = 0; for (column = 0; column <= 27; column ++){ A += population_dec [row][column]; B = 20 + (A*0.0000744313004405472); } cout << "Population Number " << row << " is " << B << endl; } getch (); return 0; }``````

And the results are as follows:

 ``` Population Number 1 is 5746.61 Population Number 2 is 17959.6 Population Number 3 is 4246.73 Population Number 4 is 16603.9 Population Number 5 is 18836.4 Population Number 6 is 18333 Population Number 7 is 18365 Population Number 8 is 14957 Population Number 9 is 3034.82 Population Number 10 is nan ```

Why is Population Number 10 resulted in "nan"??
Array indices starts at 0, so row should go from 0 to 9 instead of 1 to 10.
So my code should be like this?

 ``1234567891011121314`` `````` float binary [10][28]; float population_bin [10][28]; float population_dec [10][28]; int row, column; for (row = 0; row <= 10; row ++){ A = 0; B = 0; for (column = 0; column <= 27; column ++){ A += population_dec [row][column]; B = 20 + (A*0.0000744313004405472); } cout << "Population Number " << (row+1) << " is " << B << endl; }``````
As was said for an array of size N subscripts shall be in the range [ 0, N - 1]. So this code is invalid

 ``1234567`` `````` float population_dec [10][28]; int row, column; for (row = 0; row <= 10; row ++){ A = 0; B = 0; for (column = 0; column <= 27; column ++){``````
Okey dokey!! Thanks everyone!
Topic archived. No new replies allowed.