Home > Enterprise >  Summing recursive functions return values to a variable
Summing recursive functions return values to a variable

Time:10-25

I'm currently learning C and I'm trying to complete a recursive function, which first reads a data-structure and then returns itself n-amount of times to the main function. These returns are supposed to be summed up in the main-function and assigned to a variable.

In my program I have a little trickier data-structure. It is a Map, which consists of string and a vector-string. The purpose of the data-structure is to display a pyramid scheme: all the people associated with the leaders of the pyramid scheme.

#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;


int recursive_function(map<string, vector<string>> data_structure, string id)
{
    // Going trough every name associated with the current id-name.
    for (auto& name : data_structure[id])
    {
        // Recalling the function, when the current name is associated to the
        // first name-id declared in the main function (Hugo).
        recursive_function(data_structure, name);
    }

    // Returning the function, every time a single associate has been found.
    // In this case, we get 10 associates with Hugo, which means the recursive
    // function will be returned a total of 11 times (1 of these returns will be
    // ignored, since Hugo can't be an associate with himself in the example...

    cout << "Now returning." << endl; // This is just for clarity.
    return 1;

}


int main()
{
    // Creating the data-structure, which displays a pyramid scheme. It can be
    // used to navigate trough connections between the people.
    map<string, vector<string>> data_structure;
    data_structure = {
        {"Hugo", {"Laura", "Jasper"}}, // Hugo is one of the leaders. He is
                                       // associated with Laura and Jasper, who
                                       // are associated with others etc. Laura
                                       // and Jasper too are one of the leaders.
        {"Laura", {"Helena", "Elias"}},
        {"Jasper", {"Maria", "Bibek", "Raul"}},
        {"Helena", {"Sofia", "Amelia", "Rick"}},
        {"Sofia", {}}
    };
    string id = "Hugo";

    int associate_counter = 0;

    associate_counter  = recursive_function(data_structure, id);

    cout << "Hugo has a total of "
         << associate_counter - 1   // Should print the equation 10-1 (10), but
         << " associates."          // instead, prints 1-1 (0).
         << endl;

    return 0;
}

What am I doing wrong. Why can't I sum up the functions return times in the main functions associate_counter variable, by using recursion?

CodePudding user response:

You're just discarding the values returned from all the calls to recursive_function. You need to add them up.

Example:

int sum = 0;
for (auto& name : data_structure[id])
{
    sum  = 1   recursive_function(data_structure, name);
    //   1 for the associate
    //   the sum of the associate's associates (recursively)
}
return sum;

This will make it return 10 for Hugo.

Demo

  • Related