Use a debugger!
Code:
$ g++ -g DTM.cpp DTM-driver.cpp
$ gdb ./a.out
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /tmp/a.out...done.
(gdb) run
Starting program: /tmp/a.out
Water percentage :-nan
Water volume:0
Highest elevation:0
Lattitude highest: 2.96439e-323
Longitutde highest: 3.11455e-317
Deepest elevation: 0
Lattitude lowest: 2.07378e-317
Longitutde lowest: 0
Average elev: 0
Average above sea elev: -nan
Program received signal SIGSEGV, Segmentation fault.
0x000000000040195a in EarthCalc::centroidy (this=0x7fffffffe100) at DTM.cpp:188
188 if ( array[i][j] > 0 )
(gdb) print i
$1 = 2176
(gdb) print j
$2 = 0
Woah - 2176!?
But you have
static const int rowNum = 2160;
A large number of your for loops are whacked.
Code:
$ grep 'for' DTM.cpp
for( int i=0; i < columns; i++)
for( int j=0; j < rows; j++ )
for (int i = 0; i<rowNum; i++ )
for(int j = 0; j<colNum; j++ )
for(int i = 0; i<rowNum; i++ )
for(int j = 0; j<colNum; j++ )
for(int i = 0; i<rowNum; i++ )
for(int j = 0; j<colNum; j++ )
for(int i = 0; i<rowNum; i++ )
for(int j = 0; j<colNum; j++ )
for( int i = 0; i<rowNum; i++ )
for( int j = 0; j<rowNum; j++ )
for ( int i = 0; i<rowNum; i++ )
for( int j = 0; j<rowNum; j++ )
for (int i = 0; i<colNum; i++ )
for ( int j = 0; j<rowNum; j++ )
for (int i = 0; i<colNum; i++ )
for ( int j = 0; j<rowNum; j++ )
It's supposed to be row by col, not
row by row
or
col by row