Recursion is HARD!

Hello coders,
I have a final tomorrow, and no matter how many tutorials I have read, I cannot figure out recursive functions. What a nightmare. Can anyone give me some insight? - in plain language, please! I don't understand any of the following:
-how to return the number of digits in a passed in function
-how to return the first digit of a positive passed in number
-how to return the sum of the digits in a passed in number

Those are basic ones I should start with. I understand the basecase part of the function, but I can't figure out the rest of it. I keep wanting to put in a loop, but the whole point is to NOT use a loop, right? I'm stumped. Please help me. I don't want to fail tomorrow. :(

How do you figure out what to put in the second part of the function (after the basecase)?
I got this first one to work, but it was really just trial and error. I need to understand the steps that made it work...anyone?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 int CountDigits(long num)
{

    static int n = 0;
    if (num <=9)
    {
        return 1;
    }
    else
    {
        CountDigits(num/10);
        n++;
    }
    return n + CountDigits(n);
}
In order to understand recursion you first have to understand recursion :D! I partially joke but this gag is what made the idea click for me.

Here's an example of recursion in action. What part are you having trouble with?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

void pause()
{
    std::cin.sync();
    std::cin.ignore();
}

int DivByTen(int &Num)
{
    if(Num > 10)
    {
        std::cout << "Dividing " << Num << std::endl;
        Num = Num / 10;
        DivByTen(Num);
    }
        return Num;
}


int main(int argc, char *argv[])
{
    int SIZE = 925164;
    
    std::cout << DivByTen(SIZE);

    pause();
    return EXIT_SUCCESS;
}
In my problem, for example, I'm having trouble mainly with the return statement. Why is that working? Lol. I don't get it. I understand why I did what I did in the else clause, but I had to play around with the return statement a bit until it worked....also, for some reason, when I run this program, it works, but the output file is showing something different. Why would it do that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fstream>

using namespace std;

// File descriptor
ofstream outfile;

// Prototype
int openOutput();
int CountDigits(long num);
int FirstDigit(long num);
int DigitSum(long num);


int main()
{
    openOutput();
    long inputNum = 0;    // Variable to hold input

    // Get number
    cout << "Input number up to ten digits long." << endl;
    outfile << "Input number up to ten digits long." << endl;
    cin >> inputNum;

    // Display number
    cout << "You entered: " << inputNum << endl;
    outfile << "You entered: " << inputNum << endl;

    cout << "That number contains: " << CountDigits(inputNum) << " digits." << endl;
    outfile << "That number contains: " << CountDigits(inputNum) << " digits." << endl;

    return 0;
}

//This function opens output file to store answers
int openOutput()
{
    outfile.open("RecursionStuffOut.out");    //Opens output file
    if (!outfile)                                                //If not in oufile
    {
        //Display error message
        cout << "Error opening output file." << endl;
        outfile << "Error opening output file." << endl;
        return 0;   //Quit
    }
    else
    {//Else, if outfile opens correctly, display message
        cout << "Outfile opened correctly." << endl << endl;
        cout << "CS 318.20181" << endl << "Erin Corona" << endl
             << "Recursion Stuff Program" << endl << endl;
        //Echo
        outfile << "Outfile opened correctly." << endl << endl;
        outfile << "CS 318.20181" << endl << "Erin Corona" << endl
                << "Recursion Stuff Program" << endl << endl;
    }
}
int CountDigits(long num)
{

    static int n = 0;
    if (num <=9)
    {
        return 1;
    }
    else
    {
        CountDigits(num/10);
        n++;
    }
    return n + CountDigits(n);
}


int FirstDigit(long num)
{

}

int DigitSum(long num)
{

}
This increments "n" to '1' at each pass and totals them because you require the formula to execute before returning. Since it adds the '1' to the returned value every time it executes it essentially counts how many times the function was called.
So does it call it again when the outfile statement executes? Why does my initial call return 5, but the outfile returns 9? I've never had the outfile not match the cout....weird. How do I fix that?
1
2
3
4
5
6
7
8
9
10
   // Display number
    cout << "You entered: " << inputNum << endl;
    outfile << "You entered: " << inputNum << endl;

    int digits = CountDigits(inputNum);
    cout << "That number contains: " << digits << " digits." << endl;
    outfile << "That number contains: " << digits << " digits." << endl;

    return 0;
}


Okay, so that's how I fixed the above problem. Is that acceptable? Or is that an easy way out? I want to do it the right way...
That's probably an acceptable answer, maybe not an "A+" but at least a "B".
What is the "A" answer?
I would give it an "A" because it will give you a correct answer, but programming teachers can be a pain sometimes.

A might be changing "CountDigits()" to something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int CountDigits(long num)
{
    int n = 0;
    
    if(num >= 10)
    {
        num = num / 10;
        n++;
        n = n + CountDigits(num);
    }
    else
    {
        return 1;
    }
    
    return n;
}


But I'm not a mind reader, again programming teachers can be a real pain.
Topic archived. No new replies allowed.