1

Im trying to create a recursive function that contains a vector of numbers and has a key, which is the number we are looking for in the vector.

Each time the key is found the function should display a count for how many times the key appears in the vector.

For some reason my recursive function is only returning the number 1 (disregard the 10 I was just testing something)

Here's my code:

int recursive_count(const vector<int>& vec, int key, size_t start){
    if (start == vec.size())
        return true;
    return (vec[start] == key? 23 : key)
        && recursive_count(vec, key, (start+1));
}


int main() {

    vector <int> coco;

    for (int i = 0; i<10; i++) {
        coco.push_back(i);
    }

    cout << coco.size() << endl;

    int j = 6;


    cout << recursive_count(coco, j, 0) << endl;

}
amit
  • 175,853
  • 27
  • 231
  • 333
Manny O
  • 97
  • 2
  • 14
  • 1
    What happens when you step through it with a debugger? – Daniel Daranas Oct 15 '13 at 13:26
  • 3
    What did you expect the result to be? You are returning a boolean answer – amit Oct 15 '13 at 13:26
  • Not sure what you are trying to do, but as is - your function will return false (0) if and only if the input `key` is 0, and 0 is in the vector. Otherwise it will return 1. – amit Oct 15 '13 at 13:27

4 Answers4

3

Not sure what you are trying to do, but as is - your function will return false (0) if and only if the input key is 0 and it is in the vector. Otherwise it will return 1.

This is because you are basically doing boolean AND operation. The operands are true for all values that are not 0, and the only way to get a 0 - is if it is in the vector - and the key is 0.

So, unless you get a false (0) along the way, the answer to the boolean formula is true, which provides the 1.


EDIT:

If you are trying to do count how many times the key is in vec - do the same thing you did in iterative approach:

  1. Start from 0 (make stop condition return 0; instead of return true;)
  2. Increase by 1 whenever the key is found instead of using operator&&, use the operator+.

(I did not give a direct full answer because it seems like HW, try to follow these hints, and ask if you have more questions).

amit
  • 175,853
  • 27
  • 231
  • 333
  • Ok I see but when I changed it to: if (start == vec.size()) return 0; I still received the same error. The start variable is supposed to be set to 0 and keep increasing by 1 until it gets to the size of the vector and then the recursion ends. – Manny O Oct 15 '13 at 13:32
  • 1
    @MannyO: That does not change anything. `23 && 5 == 1`, because result of `&&` is always 0 or 1. You'd only get 5 if the vector was empty. It isn't. – Jan Hudec Oct 15 '13 at 13:34
  • @MannyO What exactly are you trying to achieve? – amit Oct 15 '13 at 13:36
  • I want the function to go through recursively and count how many times the key appears in the vector – Manny O Oct 15 '13 at 13:37
  • @MannyO I gave you some hints how to do so, try to have a look and feel free to ask more question if you fail – amit Oct 15 '13 at 13:41
2

Your recursive_count function always evaluates to a bool

You are either explicitly returning true

if (start == vec.size())
  return true;

or returning a boolean compare

return (vec[start] == key? 23 : key) // this term gets evaluated
        &&  // the term above and below get 'anded', which returns true or false.
        recursive_count(vec, key, (start+1)) // this term gets evaluated

It then gets cast to your return type ( int ), meaning you will only ever get 0 or 1 returned.

Kindread
  • 926
  • 4
  • 12
2

To me it seems that a recursive function for that is nonsense, but anyway...

Think about the recursion concepts.

What is the break condition? That the current character being checked is not in the string anymore. You got that right.

But the recursion case is wrong. You return some kind of bool (what's with the 23 by the way? The one recursion round needs to return 1 if the current element equals key, and 0 otherwise.

Then we only need to add up the recursion results, and we're there!

Here's the code

int recursive_count(const vector<int>& vec, int key, size_t start) {
    if (start >= vec.size()) {
        return 0;
    } else {
        return
        ((vec[start] == key) ? 1 : 0) + 
                recursive_count(vec, key, start+1);
    }
}

Since this is even tail-recursion, good compilers will remove the recursion for you by the way, and turn it into its iterative counterpart...

Community
  • 1
  • 1
codeling
  • 11,056
  • 4
  • 42
  • 71
  • Its part of a project that im doing for school on recursions. Don't mind the 23 I was just experimenting to see what result I would get. I've been trying to get a better understanding of recursions for a little while now. – Manny O Oct 15 '13 at 13:43
  • So did you snatch my implementation in the few minutes it was up ;)? – codeling Oct 15 '13 at 13:48
0

As per integral promotion rules on cppreference.com

The type bool can be converted to int with the value false becoming ​0​ and true becoming 1.

With,

if (start == vec.size())
        return true;

your function with return type int returns 1

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59