Trying to understand this recursive algorithm

I try to understand this piece of code
So for the recursive function in calculate interest amount. I don't understand why we have to have month-1? And when we call it back in main, why do we have to have 1+rate? I understand interest rate /100 since it is in percentage, but why plus 1?
And I do not understand this phrase:
return ((number%10 * pow(10, (double)((int) log10((double)number)))) + reverseNumber(number/10)).
How can this recursive function return a reverse number?

. Thank you

Prompt: Your program will contain two different recursive functions to accomplish two different tasks.

The first task will be to compute the final value of an account that is accruing compound interest. Your program will read from a file called bank_accounts.dat. This file will have one line for each different account for which you need to calculate the interest. The data on each line is made up of the following values:

Starting value Term Length (in months) Monthly interest rate

For each account, your program should call the recursive function, sending the data read from the file. Once the final value is calculated, the following information should be displayed to the screen:

Final value (dollars) Total interest earned (dollars)

The second task will be to reverse the digits in an integer. For example, if the number 345 is passed to your function, it should return the value 543. Your program will read a sequence of integers from a file called numbers.dat. For each number in the file, call your recursive function and display the return value to the screen. Your function should not use any string manipulation, only mathematical calculations.

Make sure your program is properly documented and good programming standards are followed.

-----------bank_accounts.dat--------------------------------------------------------

15000 36 2

20000 24 3

40000 36 3

50000 48 2

---------------------numbers.dat---------------------------------

123
345
100
1001
62

And here is my friend solution
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
C++ Program:

File: RecMethods.h

#ifndef RECMETHODS_H_INCLUDED
#define RECMETHODS_H_INCLUDED

//Function prototypes
double calculateInterest(double, int, double);
int reverseNumber(int number);

#endif // RECMETHODS_H_INCLUDED

File: RecMethods.cpp

#include<math.h>
#include "RecMethods.h"

//Function that calculates total value and interest earned
double calculateInterest(double amt, int months, double intRate)
{
    double rate;

    //Recursion termination condition
    if(months == 0)
    {
        return amt;
    }

    //Calculating amount
    amt *= intRate;

    //Recursive call
    return calculateInterest(amt, months - 1, intRate);
}


//Function that reverse a number
int reverseNumber(int number)
{
    // Recursion termination condition
    if(number == 0)
    {
        return 0;
    }

    //Recursive call
    return ((number%10 * pow(10, (double)((int) log10((double)number)))) + reverseNumber(number/10));
}

File: main.cpp

#include <iostream>
#include <fstream>
#include "RecMethods.h"

using namespace std;

//Main function
int main()
{
    double amt, finalAmt, rate;
    int months;
    int number;

    fstream file1("bank_accounts.dat", ios::in);

    //Reading data from file
    while(file1.good())
    {
        //Fetching single line of data
        file1 >> amt >> months >> rate;

        //Printing actual values
        cout << "\n\n Initial Amount: " << amt << " \t Months: " << months << " \t Monthly Rate: " << rate;

        //Calling function
        finalAmt = calculateInterest(amt, months, (1+rate/100));

        //Printing final result
        cout << "\n Final Amount: " << finalAmt << " \t Total Interest Earned: " << (finalAmt - amt) << " \n";
    }

    //Closing file
    file1.close();

    cout << "\n\n";

    //Opening with second file
    file1.open("numbers.dat", ios::in);

    //Reading data from file
    while(file1.good())
    {
        //Reading a number
        file1 >> number;

        //Printing number and its reverse
        cout << "\n Actual Number: " << number << " \t Reverse: " << reverseNumber(number);
    }

    cout << "\n\n";

    //Closing file
    file1.close();

    return 0;
}



Thank you for your attention.
Last edited on
You could try tracking the execution. Someone finds it useful, someone confusing:
bank_accounts.dat:
100.00 02 5.10
2300.00 04 2.75
1999.51 06 8.14


numbers.dat:
7991
20296
14804


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
84
85
86
87
88
89
90
#include <math.h>
#include <fstream>
#include <iomanip>
#include <iostream>

double calculateInterest(double, int, double);
int reverseNumber(int number);

int main()
{
    std::ifstream file1("bank_accounts.dat");
    if(!file1) {
        std::cout << "First file can't be opened.\n Exiting now.\n";
        return 1;
    }
    //Reading data from file
    double amt {}, rate {};
    int months {};
    while(file1 >> amt >> months >> rate)
    {
        // Print initial values:
        std::cout << "\nInitial Amount: " << amt << " \t Months: " 
                  << months << " \t Monthly Rate: " << rate << '\n';

        double finalAmt = calculateInterest(amt, months, (1+rate/100));

        // Print final result:
        std::cout << "\nFinal Amount: " << finalAmt 
                  << " \t Total Interest Earned: " << (finalAmt - amt) << '\n';
    }
    file1.close();
    
    // Uncomment the following line to prevent executing the second part of 
    // the program:
    // return 0;

    file1.open("numbers.dat");
    if(!file1) {
        std::cout << "Second file can't be opened.\n Exiting now.\n";
        return 1;
    }

    //Reading data from file
    int number;
    std::cout << "\n\n";
    while(file1 >> number)
    {
        int reversed = reverseNumber(number);
        // Print number and its reverse:
        std::cout << "\nActual Number: " << number 
                  << " \t Reverse: " << reversed << '\n';
    }
    file1.close();
    return 0;
}

//Function that calculates total value and interest earned
double calculateInterest(double amt, int months, double int_rate)
{
    std::cout << "amt: " << std::fixed << std::setprecision(2) << amt
              << "; months: " << months 
              << "; int_rate: " << std::fixed << std::setprecision(2) << int_rate;
    //Recursion termination condition
    if(months == 0) { return amt; }

    //Calculating amount
    amt *= int_rate;
    std::cout << " >>> amt --> " << amt << '\n';

    //Recursive call
    return calculateInterest(amt, months - 1, int_rate);
}

//Function that reverse a number
int reverseNumber(int number)
{
    std::cout << "number: " << std::setw(5) << number;
    // Recursion termination condition
    if(number == 0) { return 0; }

    //Recursive call
    std::cout << "; number % 10: " << number % 10 
              << "; pow(...): "
              << pow(10, (double)((int) log10((double)number)))
              << "; nnumber%10 * pow(...): " 
              << number%10 * pow(10, (double)((int) log10((double)number)))
              << "; number / 10: " << number / 10 << '\n';
    return ((number%10 * pow(10, (double)((int) log10((double)number)))) 
             + reverseNumber(number/10));
}


Output:

Initial Amount: 100      Months: 2       Monthly Rate: 5.1
amt: 100.00; months: 2; int_rate: 1.05 >>> amt --> 105.10
amt: 105.10; months: 1; int_rate: 1.05 >>> amt --> 110.46
amt: 110.46; months: 0; int_rate: 1.05
Final Amount: 110.46     Total Interest Earned: 10.46

Initial Amount: 2300.00          Months: 4       Monthly Rate: 2.75
amt: 2300.00; months: 4; int_rate: 1.03 >>> amt --> 2363.25
amt: 2363.25; months: 3; int_rate: 1.03 >>> amt --> 2428.24
amt: 2428.24; months: 2; int_rate: 1.03 >>> amt --> 2495.02
amt: 2495.02; months: 1; int_rate: 1.03 >>> amt --> 2563.63
amt: 2563.63; months: 0; int_rate: 1.03
Final Amount: 2563.63    Total Interest Earned: 263.63

Initial Amount: 1999.51          Months: 6       Monthly Rate: 8.14
amt: 1999.51; months: 6; int_rate: 1.08 >>> amt --> 2162.27
amt: 2162.27; months: 5; int_rate: 1.08 >>> amt --> 2338.28
amt: 2338.28; months: 4; int_rate: 1.08 >>> amt --> 2528.61
amt: 2528.61; months: 3; int_rate: 1.08 >>> amt --> 2734.44
amt: 2734.44; months: 2; int_rate: 1.08 >>> amt --> 2957.03
amt: 2957.03; months: 1; int_rate: 1.08 >>> amt --> 3197.73
amt: 3197.73; months: 0; int_rate: 1.08
Final Amount: 3197.73    Total Interest Earned: 1198.22


number:  7991; number % 10: 1; pow(...): 1000.00; nnumber%10 * pow(...): 1000.00; number / 10: 799
number:   799; number % 10: 9; pow(...): 100.00; nnumber%10 * pow(...): 900.00; number / 10: 79
number:    79; number % 10: 9; pow(...): 10.00; nnumber%10 * pow(...): 90.00; number / 10: 7
number:     7; number % 10: 7; pow(...): 1.00; nnumber%10 * pow(...): 7.00; number / 10: 0
number:     0
Actual Number: 7991      Reverse: 1997
number: 20296; number % 10: 6; pow(...): 10000.00; nnumber%10 * pow(...): 60000.00; number / 10: 2029
number:  2029; number % 10: 9; pow(...): 1000.00; nnumber%10 * pow(...): 9000.00; number / 10: 202
number:   202; number % 10: 2; pow(...): 100.00; nnumber%10 * pow(...): 200.00; number / 10: 20
number:    20; number % 10: 0; pow(...): 10.00; nnumber%10 * pow(...): 0.00; number / 10: 2
number:     2; number % 10: 2; pow(...): 1.00; nnumber%10 * pow(...): 2.00; number / 10: 0
number:     0
Actual Number: 20296     Reverse: 69202
number: 14804; number % 10: 4; pow(...): 10000.00; nnumber%10 * pow(...): 40000.00; number / 10: 1480
number:  1480; number % 10: 0; pow(...): 1000.00; nnumber%10 * pow(...): 0.00; number / 10: 148
number:   148; number % 10: 8; pow(...): 100.00; nnumber%10 * pow(...): 800.00; number / 10: 14
number:    14; number % 10: 4; pow(...): 10.00; nnumber%10 * pow(...): 40.00; number / 10: 1
number:     1; number % 10: 1; pow(...): 1.00; nnumber%10 * pow(...): 1.00; number / 10: 0
number:     0
Actual Number: 14804     Reverse: 40841

Anyway please note: if you ask for help about a code which requires some files and you don’t provide any example of those files, your post is likely to be ignored.
Hello @Enoizat,

Thank you for answering. I really appreciate it.
I am new to this, what do you mean by provide examples of the files?

Thank you
what do you mean by provide examples of the files?

Nothing, tieuholy, sorry. I didn’t notice you copied the content of the two files needed by the program inside your explanations. I’m not a native English speaker and I tend to go to the essential (the code), so I didn’t read your introduction carefully enough. Sorry for that. (However, mine was intended to be a piece of advice, not a criticism.)
Oh you are absolutely fine @Enoizat. Thanks for your advice.

I am just starting out with all this so I hope I would be able to meet more people like you.

Please have a nice day!
Topic archived. No new replies allowed.