简体   繁体   English

递归函数错误

[英]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). 然后,我的函数应该保持运行中的总和,并从元素1开始,然后将元素2与该总和进行比较(此时,它只是元素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. 移至下一个元素,将元素2添加到总和中,然后查看元素3是否大于元素1和2的总和。我应该仅打印大于该总和的元素。 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 . 您可能是指index ,而不是i Both are wrong, though: the first index in the array is 0 , not 1 (which is the value of index ). 但是,两者都是错误的:数组中的第一个索引是0 ,而不是1 (这是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. 在“实际”代码中,应始终通过(const)引用传递大型数据类型。 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 . 首先,当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. 其次, runningSum的初始值是从unsortedData[i] ,这没有任何意义,因为当时i-1 You probably meant unsortedData[0] . 您可能是指unsortedData[0]

Early in main you use theData and later you use unsortedData. 首先,主要使用theData,后来使用unsortedData。 I'm not sure why the compiler hasn't complained about unsortedData not being defined. 我不确定为什么编译器没有抱怨unsortedData没有被定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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