简体   繁体   中英

Recursive Function Error

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;

}

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

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.

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

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

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