I'm writing a program to represent an array with a large number of elements with most being zero and no more than 20 being non zero. Theoretically the array can have a trillion elements, but two separate arrays will represent the arrays indexes and values of the two non zero elements. The problem I'm having right now is that I can't output the value array for debugging purposes. When I attempt to output the value array, all I'm getting is zeros, but when I output the total I'm getting 820. Also, this is a homework assignment and I can't change any of the public class declarations, except the output function when I'm writing on my own to debug. Thanks for the help.
class SparseArray{
public:
SparseArray(); // construct an SparseArray with no nonzero elements
unsigned get( unsigned long long index ); // return an element from the array
SparseArray & set( unsigned long long index, unsigned value ); // set array element to value
SparseArray & output ();
private:
unsigned long long indexArray[21];
unsigned valueArray[21];
unsigned nonzero;
}; // SparseArray
int main(int argc, const char * argv[])
{
SparseArray a;
unsigned long long index = 1;
for( unsigned i = 0; i <= 20; i++, index *= 4 )
a.set( index, i );
a.output();
unsigned total = 0;
index = 1;
for( unsigned i = 0; i <= 40; i++, index *= 2 ){
total += a.get(index);
a.output();
}
cout <<total;
return 0;
}
SparseArray::SparseArray():
indexArray(),valueArray()
{
for (int i = 0; i < 20; i++) {
indexArray[i] = 0;
valueArray[i] = 0;
}
}
unsigned SparseArray::get( unsigned long long index ){
unsigned object;
for (unsigned i = 0; i < 20; i++) {
if (indexArray[i] == index) {
object = valueArray[i];
}
}
return object;
}
SparseArray & SparseArray::set( unsigned long long index, unsigned value ){
for (int i = 0; i < 20; i++) {
if (indexArray[i] == 0 && valueArray == 0) {
indexArray[i] = index;
valueArray[i] = value;
break;
}
}
return *this;
}
SparseArray & SparseArray::output (){
for (int k = 0; k < 21; k++) {
cout << valueArray[k] << "," << endl;
}
return *this;
}
nonzerovariable was going to be used for, but it hadn't been implemented yet.valueArray == 0?