I noticed that in another of my class member functions I increment length so I changed to using a temp value for my valueAt function.
float MatrixType::valueAt(int i, int j) const
{ // code to return the float value at
// significant location row i col j
int temp = 0; // temp value holder
for(; temp < MAXSIZE; temp++){
if(i == matrix[temp].row && j == matrix[temp].col)
{
temp++;
return matrix[temp-1].value;
} // checks to match up row and col to find correct value
else
return 0.0;
} // end for loop
} // end accessor valueAt
The function I had incrementing member variable length is my storeItem function:
void MatrixType::storeItem(int i, int j, float v)
{
if(v != 0)
{ // if element is significant store members into array of structs
matrix[length].row = i; // row index stored
matrix[length].col = j; // col index stored
matrix[length].value = v; // value index stored
// now element length in the matrix has the value v and
// can be identified by members row and col
length++; // increment for next significant value
} // end if: the value has been stored at row and col
// else
// value is insignificant (ie is zero)
// move on to next element
} // end mutator storeItem
I figured that by incrementing length in this function length's value was always the latest member in the array, so using it for comparison in my valueAt function would never yield the right value.
By changing my valueAt function to where it is now my output will only show the first significant value in the matrix correctly. All other values show zero.
i.e.
Input Matrix
0.0 3.1 0.0 0.0 0.0 0.0 4.2
5.8 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 1.7 2.3 0.0 0.0 0.0
0.0 0.9 9.4 0.0 0.0 0.0 0.1
0.0 0.0 0.0 0.0 4.2 0.0 0.0
Output Matrix
0.0 3.1 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
If I change my input, say make the 3.1 a 0.0 and leave the rest as is, the Output Matrix will show the 4.2 from the Input Matrix in the correct place (row 0 col 6) but all other values will return 0.
For further clarity: This is the function in main using the valueAt member function of class MatrixType.
void print_matrix(ostream& outfile, const MatrixType& matrix)
{ // exports sparse matrix to outfile in dense matrix form
int sigRows, sigCols; // significant number of rows and columns
int i, j; // row and col index of sparse matrix; significant rows and columns
float v; // holds temp value for output
sigRows = matrix.numRows(); // get numRows
sigCols = matrix.numCols(); // get numCols
outfile << sigRows << " " << sigCols << endl;// output numRows and numCols to file
cout << "Output Matrix\n";
for(i = 0; i < sigRows; i++) // each row
{
for(j = 0; j < sigCols; j++) // each col
{
v = matrix.valueAt(i, j); // get value at location i,j
cout << v << " ";
outfile << v << " "; // output value at location i,j
} // end col for loop
cout << endl;
outfile << endl; // output file gets new line after end of col reached
} // end row for loop
}
Any help getting to point me in the right direction for getting this output to function correctly would be greatly appreciated.