1

I have a program that is supposed to read in values from user into a vector. My function is then supposed to keep a running sum and start from element 1 and compare element 2 to the sum(this point it is just element 1). Move to the next element, add element 2 to the sum and see if element 3 is greater than the sum of elements 1 and 2. I am supposed to print only the elements that are greater than the sum. I'm having trouble getting it to print out any values. Could someone please let me know what I might be doing wrong? Thanks

int main()
{
    vector <int> theData;
    int i;

    cout<< "Enter in the list of integers ending with a -1" << endl;

    do
    {
        cin >> i;
        if (i==-1)
        {
          break;
        }
        theData.push_back(i);


    }while(i!=-1);

    int index = 1;
    int runningSum = unsortedData[i];
    largeValue(unsortedData, index, runningSum);

    system("PAUSE");
    return 0;
}

void largeValue(vector<int> myVector, int index, int runningSum)
{
    int size = myVector.size();

    if (index == size)
    {
        return;
    }
    if (myVector[index] > runningSum)
    {
        cout << myVector[index] << " ";
        runningSum += myVector[index];
        index = index +1;
        largeValue(myVector, index, runningSum);
    }
    else if (myVector[index] < runningSum)
    {
        runningSum += myVector[index];
        index = index + 1;
        largeValue(myVector, index, runningSum);
    }
}
4
  • Please format the source in your question using spaces, not tabs, and include all of the source in the formatting. Commented Nov 6, 2009 at 21:50
  • I think this isn't the place to ask for homework! Commented Nov 6, 2009 at 21:52
  • 1
    Are you supposed to use recursion? Commented Nov 6, 2009 at 21:52
  • 2
    @Nathan: this is a place to ask for help! This isn’t the best worded question but it’s definitely not a “plz-send-the-homeworkz” question. Commented Nov 6, 2009 at 21:53

3 Answers 3

2

There are several errors in your code:

int runningSum = unsortedData[i];

You probably meant index, not i. Both are wrong, though: the first index in the array is 0, not 1 (which is the value of index).

Also, your recursive function contains at least one error: you don’t consider that the current element equals the sum.

Another thing: you pass the vector to your function by value – not a good idea: for each call of the function, the whole vector is copied, which may take considerable time for medium-sized vectors. In “real” code, large data types should always be passed by (const) reference. Just change the function signature slightly:

void largeValue(vector<int> const& myVector, int index, int runningSum)

This way, you pass an unmodifiable reference of your vector to the function instead of copying it. Notice that this makes it impossible to modify the data of the vector inside the function.

Sign up to request clarification or add additional context in comments.

Comments

1

Firstly, your function fails to meaningfully process the case when myVector[index] == runningSum.

Secondly, the initial value of runningSum is taken from unsortedData[i] which doesn't make any sense, since i is -1 at that time. You probably meant unsortedData[0].

Comments

0

Early in main you use theData and later you use unsortedData. I'm not sure why the compiler hasn't complained about unsortedData not being defined.

Comments

Your Answer

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