简体   繁体   中英

Counting inside a recursive function

int countLatticePoints(std::vector<int> &point, const double &radius, const int &dimension) {
static int count = 0;    

for(int i = -(static_cast<int>(std::floor(radius))); i <= static_cast<int>(std::floor(radius)); i++) {
    point.push_back(i);

    if(point.size() == dimension){
        if(isPointWithinSphere(point, radius)) count++;
    }else countLatticePoints(point, radius, dimension);

    point.pop_back();
}

return count;
}

I have the above recursive function for which I would like to increment some variable if the condition isPointWithinSphere( ... ) is true. My initial approach was to declare a static variable called count so that it maintains the count through each recursive call. OK, this works fine for when I call the function countLatticePoints the first time, but now if i call the function again it adds to the previous count value when i first called countLatticePoints . I understand that this happens because of the way that the static qualifier behaves. Is there any way that I can reset that count variable back to 0 after one call finishes before I call the second one ? I don't want to use global variables of course, is there any other approach that can work in this situation ?

You don't need any static variable or additional parameter. Here is what you need.

int countLatticePoints(std::vector<int> &point, const double &radius, const int &dimension) {
    int count = 0;
    const int iRadius = std::floor(radius);
    for(int i = -iRadius; i <= iRadius; i++) {
        point.push_back(i);
        if(point.size() == dimension){
            if (isPointWithinSphere(point, radius))
                count++;
        } else
            count += countLatticePoints(point, radius, dimension);
        point.pop_back();
    }

    return count;
}

On a side note, you don't need to pass int or double as reference.

Pass count as a parameter; give it a default value of 0, so the initial call doesn't have to supply it. Will that do for your needs?

You can use a helper function to accomplish what you want as follows:

void countLatticePoints(std::vector<int> &point, const double &radius, const int &dimension, int& count) {
    const int int_radius = static_cast<int>(std::floor(radius));
    for(int i = -int_radius; i <= int_radius; i++) {
        point.push_back(i);

        if(point.size() == dimension){
            if(isPointWithinSphere(point, radius)) count++;
        }else countLatticePoints(point, radius, dimension, count);

        point.pop_back();
    }
}

int countLatticePoints(std::vector<int> &point, const double &radius, const int &dimension) {
    int count = 0;    
    countLatticePoints(point, radius, dimension, count);
    return count;
}

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