3 Attachment(s)
if statements causing program to crash
My program crashes on runtime. It can compile, but crashes at .exe. But everything works, right up to until I run the following code:
Code:
cout<< "centroid y: "<<trialone.centroidy()<<endl;
the method centroidy is defined as follows:
Code:
double EarthCalc::centroidy()
{
double firstmoment = 0.0;
double mass1 = 0.0;
double mass2 = 0.0;
double count = 2159;
for (int i = 0; i<colNum; i++ )
{
count--;
for ( int j = 0; j<rowNum; j++ )
{
if ( array[i][j] > 0 )
{
mass1 = mass1 + (LONDISTcalc(count/12.0 - 90)) *(LATDISTcalc(count/12.0 - 90))*(array[i][j])*2.5;
}
if ( array[i][j] < 0 )
{
mass2 = mass2 + (LONDISTcalc(count/12.0 - 90) * LATDISTcalc(count/12.0 - 90) *array[i][j]);
}
firstmoment = firstmoment + (j/12.0 - 90)*LONDISTcalc(count/12.0 - 90)*LATDISTcalc(count/12.0 - 90);
}
}
return (firstmoment/(mass1 + mass2));
}
Ive narrowed the problem down to the 2 if statements. For some reason, the if statements crash the program, even though I checked the code like 50 times and saw nothing wrong. I am accessing a huge array of like 18 MB, but still, I accessed that array before in other methods of the class as well. But it didnt crash. WTF?
Anyways, I've also attached the 3 files with all the code as well. If anyone can help me, thanks.
UPDATE: I fixed the code by deleting the if statements entirely. I used some of the other methods in the class to work around using ifs and only using the fors. But it would be still nice if someone can tell me why it crashed me