Hi
I keep on having an error when I run this program. The error says:
Matrix Class Error: Array index out of bounds
Invalid index 79 was passed. Valid index range is >= 0 and < 79
which was for matrix P, although I declared the matrix to be 605x605. Any suggestions?
Code:
// standard include files
// custom include files
#include "matrix.hpp"
using namespace std;
// Templated helper function to square a value
template< class T >
T sq( const T& x )
{ return x*x; }
const double c = 299792458.0; // Speed of light
// GPS range observation
struct RangeRecord
{
// Data members
unsigned prn;
double time;
double position[3];
double pseudorange; // range measurement
double pseudorangeStdDev; // range measurement std. dev.
bool read( istream& input );
};
bool readRanges( const string& filename, vector<RangeRecord>& ranges );
int no_epochs(vector<RangeRecord>& ranges);
int main(int argc, char* argv[] )
{
int n, u;
int epochs=0;
//files are inputted correctly
// -- Perform adjustment ------------------------------------------------
n = ranges.size();
u = 3 + no_epochs(ranges);
//matrices
Matrix<double> P(n, n);
//Create weight matrix, P
create_weight(P, ranges, n);
P.print(cout, 2, 5);
return EXIT_SUCCESS;
}
void create_weight(Matrix<double>& P, vector<RangeRecord>& ranges, int n)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i==j)
P[i][i]=1/(sq(ranges[i].pseudorangeStdDev));
else
P[i][j]=0;
}
}
}
int no_epochs(vector<RangeRecord>& ranges)
{
int number=0;
for(int i=0; i < ranges.size(); i++)
{
if(ranges[i].time!=ranges[i+1].time)
number++;
}
return number;
}
// Read a range record from a file
//
// Returns:
// true - input was successful
// false - input failed
//
// Parameters:
// input - input file stream that has already been opened
bool RangeRecord::read( istream& input )
{
input >> prn
>> time
>> position[0] >> position[1] >> position[2]
>> pseudorange
>> pseudorangeStdDev;
if( !input )
return false;
return true;
}
// Read the configuration from a file
//
// Returns:
// true - input was successful
// false - input failed
//
// Parameters:
// filename - Path of file containing ranges
// ranges - Array of ranges
bool readRanges( const string& filename, vector<RangeRecord>& ranges )
{
int size;
ifstream input( filename.c_str() );
if( !input )
return false;
input >> size;
ranges.resize(size); //Changes size of vector to hold records
for(int i = 0; i < size ; i++)
{
if(!ranges[i].read(input))
return false;
}
return true;
}