jeudi 8 avril 2021

How Does Combination Recursion is working in this program.?

I'm trying to understand this Recursive code, and I can't understand what happening on few steps i-e what it returns and how one of it's condition "if (number[numcount + 1] != '\0')" in the function works.

It's a great program to understand recursions, so getting an explanation would really help.

#include <iostream>
#include <string>
using namespace std;
void Combinations(string number, char anscomb[], int numcount, int combcount);
int main()
{
    string num;
    cout << "Please Enter A Number = ";
    cin >> num;

    //size
    /*int size;
    size = digit.length();*/

    char combi[500] ;

    combi[0] = '\0';

    int combcount = 0;
    int numcount = 0;
    cout << "THE DIFFERENT COMBINATIONS = " << endl;
    Combinations(num, combi, numcount, combcount);

    system("pause");
    return 0;

}
void Combinations(string number, char anscomb[] , int numcount, int combcount)
{
    if (number[numcount] == '\0')
    {
        anscomb[combcount] = '\0';

        for (int i = 0; i < combcount; i++)
        {
            cout << *(anscomb + i);
        }
        cout << endl;
        return;
    }
    else
    {
        anscomb[combcount] = number[numcount];
        anscomb[combcount + 1] = ' ';

        Combinations(number, anscomb, numcount + 1, combcount + 2);

        if (number[numcount + 1] != '\0')
        {
            cout << endl << "This is numcount = " << numcount<<endl;
            Combinations(number, anscomb, numcount + 1, combcount + 1);
        }

    }

}

Aucun commentaire:

Enregistrer un commentaire