Recursion: using values passed in parameters

Posted by Tom Lilletveit on Stack Overflow See other posts from Stack Overflow or by Tom Lilletveit
Published on 2012-12-11T05:02:07Z Indexed on 2012/12/11 5:03 UTC
Read the original article Hit count: 144

Filed under:
|

I got this line of code that pops a int of the array saves it to int element then removes it from array. then in the return statement return CountCriticalVotes(rest, blockIndex + element); it ads it to the blockIndex variable and if it reaches 10 before the array is empty it returns 1. But my problem is this, I do not want it to add up all the values in the array in the parameter, but only add one then revert the parameter value back to it´s original state, then add a new, revert etc... How would i do this?

int NumCriticalVotes :: CountCriticalVotes(Vector<int> & blocks, int blockIndex)
{
    if (blockIndex >= 10)
    {
        return 1;
    }
    if (blocks.isEmpty())
    {
        return 0;


    } else {

        int element = blocks.get(0);
        Vector<int> rest = blocks;
        rest.remove(0);
        return  CountCriticalVotes(rest, blockIndex + element);

© Stack Overflow or respective owner

Related posts about c++

Related posts about recursion