2

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;
}
8
  • Why are all your loops fixed at 20 or 21 iterations? If there is only one nonzero value, your loops act as if there are 20 nonzero values. You need to have a variable for the quantity of nonzero items. Commented Apr 9, 2013 at 21:18
  • You're output function is not displaying the indices of the nonzero elements. Commented Apr 9, 2013 at 21:19
  • @ThomasMatthews I'd be willing to bet that's what the nonzero variable was going to be used for, but it hadn't been implemented yet. Commented Apr 9, 2013 at 21:19
  • What are your trying to accomplish with this expression: valueArray == 0? Commented Apr 9, 2013 at 21:20
  • Yeah, the thing is, I can't change the function prototypes and pass the nonzero variable so I just go through the loop 20 times because that's the maximum length. Commented Apr 9, 2013 at 21:21

2 Answers 2

3

Also change this line to initialise the array, valueArray needs an subscript.

if (indexArray[i] == 0 && valueArray == 0) {

to

if (indexArray[i] == 0 && valueArray[i] == 0) {
Sign up to request clarification or add additional context in comments.

Comments

3

This loop iterates 21 times.

for (int k = 0; k < 21; k++)

And this is why your output is returning 820.

unsigned object; // This can be returned uninitialized data!  Set it to 0!

for (unsigned i = 0; i < 20; i++) {
    if (indexArray[i] == index) {
        object = valueArray[i];
    }
}

return object;

5 Comments

I guess I should have clarified more. I meant when a.output() gets called I get zeros. Also, the program should be outputting 210, not 820.
@user1681673 It's printing zeroes because the array is filled with zeroes. The reason you're getting 820 is because you never initialize your object variable. (this answer doesn't attempt to explain why your array is filled with zeroes)
@user1681673 I'll refrain from adding the reason you're getting all 0 because I think suspectus caught it.
Yeah, he did. Thank you for the help though!
@user1681673 The bug missing from my answer is causing your problem because your set takes the unusual step of checking both the index and value arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.