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.