简体   繁体   中英

recursive function error

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);
    }
}

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.

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] .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM