getting 'nan' error when code was working fine before (no compile error)

I do not know specifically where the error is so I apologise for posting the whole code in this post.

About one month after starting to learn c++ i decided to make a genetic algorithm for curve fitting a set on points. It was working perfectly fine and as shown in this picture the mean difference of points from their function is show clearly.
(screenshot)https://gyazo.com/25332cf70258788b9cc0189b31533b17

then after declaring another integer i get this error
(screenshot)https://gyazo.com/d568320011532e20804ffdfa4cfcae7f

i am sure this is the reason because it will work fine and then when i put declare this new variable at any line in the code and i will get the same error. it can be called anything and contain any value so here i am thinking that i have reached some sort of 'cap'.

can anyone help me out?

useful info (maybe):
Windows 7
CodeBlocks
No compile error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

int NumberOfPoints= 5;
int NumberOfFunctions = 6;
int PolynomialDegree = 2;

float RNG()
{
    float x;
    do{
        x = rand()%1000;
    }while( x < 100);
    x = x/100;
    return x;
}
class Function{
public:
void SetFunction2()
{
    a =  RNG();
    b =  RNG();
    c =  RNG();
    int RandomSign = rand()%2;
    if(RandomSign==1)
        a = -a;

    RandomSign = rand()%2;
    if(RandomSign==1)
        b = -b;

    RandomSign = rand()%2;
    if(RandomSign==1)
        c = -c;
}
void PrintFunction()
{
    cout << a <<"x^2 +" <<b <<"x + " << c;
}
float OutputDegree2(float InputFloat)
{
    return (a*pow(InputFloat,2) + b*InputFloat + c);
}
void SetMeanDifference(float x)
{
    MeanDiffence = x;
}
float GetMeanDifference()
{
    return MeanDiffence;
}
void SetScore(float InputTotalScore)
{
    DifferenceFromTotalf = InputTotalScore - GetMeanDifference();
    Score = round(DifferenceFromTotalf);
}
int GetScore()
{
    return Score;
}
void SetNormalisedScore(int LowestScore)
{
    NormalisedScore = GetScore() - LowestScore;
    if(NormalisedScore == 0)
    {
        NormalisedScore = 1;
    }
}
int GetNormalisedScore()
{
    return NormalisedScore;
}

private:
    float a;
    float b;
    float c;

    float MeanDiffence;
    float DifferenceFromTotalf;
    int Score;
    int NormalisedScore;
};


int main()
{
    /// set points
float Points[NumberOfPoints][2];
    Points[0][0] = -6.24;   Points[0][1] = 3.41;
    Points[1][0] = 3.49;    Points[1][1] = 6.90;
    Points[2][0] = 2.24;    Points[2][1] = 1.62;
    Points[2][0] = 10.24;    Points[2][1] = -7.63;

    srand(time(0));
Function Parents[NumberOfFunctions]; ///create functions
for (int i =0; i<NumberOfFunctions ;i++)
{
    Parents[i].SetFunction2();
}
///print functions
for(int i = 0 ; i<NumberOfFunctions;i++)
{
    Parents[i].PrintFunction();
    cout << endl;
}
///calculate mean difference of each point of each function
for(int x =0;x < NumberOfFunctions;x++){

    float TotalDifference = 0;
    for(int i = 0; i < NumberOfPoints;i++)
    {
    TotalDifference = TotalDifference + abs(Parents[x].OutputDegree2(Points[i][0]) - Points[i][1]);
    }

    float MeanDifference = TotalDifference/NumberOfPoints;
    Parents[x].SetMeanDifference(MeanDifference);
}
cout <<"average mean differences:\n" ;
for(int i =0; i <NumberOfFunctions;i++)
{
    cout << Parents[i].GetMeanDifference() << endl;
}

///set scores
float TotalDifferencef = 0 ;

for(int i = 0 ; i < NumberOfFunctions;i++)
{
  TotalDifferencef = TotalDifferencef + Parents[i].GetMeanDifference();
}
cout << "\nTotalDifference :" <<TotalDifferencef << endl;

for(int i=0;i <NumberOfFunctions;i++)
{
    Parents[i].SetScore(TotalDifferencef);
}
cout << "\nDifferences from Total\n";

for(int i=0;i<NumberOfFunctions;i++)
{
    cout << Parents[i].GetScore();
    cout << endl;
}
int LowestScore = Parents[0].GetScore();
///get normalised score
for(int i=0;i<NumberOfFunctions;i++)
{
    if(Parents[i].GetScore() < LowestScore)
    LowestScore = Parents[i].GetScore();
}
cout << "\nLowestScore:" << LowestScore << endl;


int TotalNormalisedScore = 0;
for(int i=0;i<NumberOfFunctions;i++)
{
    Parents[i].SetNormalisedScore(LowestScore);
    cout << Parents[i].GetNormalisedScore() <<endl;
    TotalNormalisedScore = TotalNormalisedScore + Parents[i].GetNormalisedScore();
}

int RandomParentSelector = rand()%TotalNormalisedScore +1;

cout << "TotalNormalisedScore: " << TotalNormalisedScore <<endl;
cout << "RandomNumber: " << RandomParentSelector << endl;

int newtestinteger = 5;



return 0;
}
Last edited on
Although your code seems to compile without errors, it does seem to have some warnings:

1
2
3
4
5
main.cpp||In function ‘int main()’:|
main.cpp|93|warning: variable length array ‘Points’ is used [-Wvla]|
main.cpp|99|warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]|
main.cpp|100|warning: variable length array ‘Parents’ is used [-Wvla]|
main.cpp|172|warning: unused variable ‘newtestinteger’ [-Wunused-variable]|


VLA (Variable Length Arrays) are not valid standard C++ and should not be used. Arrays in C++ should be compile times constants. The following:
1
2
3
4
5
using namespace std;

int NumberOfPoints= 5;
int NumberOfFunctions = 6;
int PolynomialDegree = 2;

Should probably be const qualified values, this should solve the VLA issues, since these variables seem to be used to size the arrays.

And really you should consider using std::vector instead of the arrays.
Topic archived. No new replies allowed.