0

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);
7
  • 1
    Does it have to be recursive? Commented Dec 11, 2012 at 5:09
  • yep! I am doing as part of learn recursion Commented Dec 11, 2012 at 5:10
  • This may not answer you question, but you should be able to do this without modifying the blocks vector (which seems unnecessary to the problem). You can try to pass it as Vector<int> const& blocks to force yourself not to modify it. Commented Dec 11, 2012 at 5:14
  • Also, from what you are asking it seems like all you want to do is find whether any value in the blocks vector exceeds 10. Perhaps I am misunderstanding. Commented Dec 11, 2012 at 5:16
  • That is correct I want to know if any of the values +blockIndex exceeds 10, but not all of them combined. Commented Dec 11, 2012 at 5:18

4 Answers 4

1

Doing it recursively (very inefficient by the way. no reason to do this):

bool NumCriticalVotes :: CountCriticalVotes(Vector<int> const& blocks,
                                            int blockIndex,
                                            size_t current = 0)
{
    // check if we reach the end of the vector
    if (current == block.size())
    {
        return true;
    }

    int sum = blockIndex+block.get(current);

    if (sum >= 10)
    {
        return true;
    }

    return  CountCriticalVotes(blocks, blockIndex, current+1);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could modify the code so that you compute the critical votes, then push the element back on to the front of the list. It would look something like this:

blocks.remove(0);
int votes = CountCriticalVotes(blocks, blockIndex + element);
blocks.push_front(element);
return votes;

This doesn't do exactly what you want, because it only gets reverted after the initial function call completes, but the end result is the same.

Comments

1

When you make the recursive call, you could pass a temporary vector which consists of all elements from index 1 till the end. Your original vector will remain unchanged. This does create a number of temporary vectors but so does your original code. Here's a quick code sample based on your example.

#include <iostream>
#include <vector>
using namespace std;

bool CountCriticalVotes(vector<int>& blocks, int sum = 0) {
    if (sum >= 10) {
        return true;
    } else if(blocks.empty()) {
        return false;
    } else {
        vector<int> temp(blocks.begin() + 1, blocks.end());
        return CountCriticalVotes(temp, sum + blocks.at(0));
    }
}

int main() {
    vector<int> foo = { 2, 3, 4, 5, 6};
    vector<int> bar = { 2, 3 };
    cout << boolalpha << CountCriticalVotes(foo) << endl;
    cout << boolalpha << CountCriticalVotes(bar) << endl;    
}

Comments

0

Maybe I understand it wrong, but why not just add another argument to this function: index of the current position in blocks. This way you will not have to remove elements from a vector, no temporary vectors etc.

Comments

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.