I need to rewrite this program. Must decompose the function main into smaller fu
ID: 3837426 • Letter: I
Question
I need to rewrite this program. Must decompose the function main into smaller functions and integrate them together. Below are the requirements:
·The program must have an option for user to repeat the program.
·Use the following functions prototypes to write functions definitions: You CANnot make any change to the function prototypes.
// function to get the initial deposit
double GetInitialDeposit();
// function to get the annual rate
void GetRate (double& pRate);
// function to get the duration of the certificate (whole year)
int GetDuration (int min, int max);
// function to get all of the input values
void GetInputs (double pDeposit, double& pRate, int& pYears);
// function to calculate then ending balance
double CalculateBalance (double pDeposit, double pRate, int p years);
// function to display information of the CD
void Displayinfo (double pDeposit, double pRate, int p Years, double pBalance);
// function to calculate a CD. This is as the overall function
void CdCalculation ();
using this program
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double P, A, t, r, n=12;
char ch;
while (true)
{
while (true)
{
cout << " Enter your initial deposit:$";
cin >> P;
if (P < 0 || P > 100000)
{
cout << "Invalid must be between 0 to 100,000" << endl;
continue;
}
else
break;
}
while (true)
{
cout <<"Enter the annual nominal interest rate:";
cin >> r;
if (r < 0.5 || r > 5.0)
{
cout <<"Invalid must be between o.5 to 5.0" << endl;
continue;
}
else
break;
}
while (true)
{
cout <<"Enter how many years the money will be certified:";
cin >> t;
if (t < 1 || t > 10)
{
cout << "Invalid must be between 1 and 10" << endl;
continue;
}
else
break;
}
double A = (P*pow((1+r/(n*100)), n * t ));
std::cout << std::setprecision(2) <<
std::fixed;
cout << " CERTIFICATE of DEPOSIT" <<
endl;
cout << "Initial amount of deposit:$” <<p<<
endl;
cout << “Annual interest: " << r << "%" <<
endl;
cout << "Duration:" << t << "years(s)"<<
endl;
cout <<"Ending balance:$" << A << endl;
cout <<"Do you wish to go again? (Y/N): ";
cin >> ans;
if (ans == 'y' || ans == 'Y')
{
continue;
}
else
break;
}
return 0;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double getInitialDeposit(){
double P;
while (true){
cout << "Enter your initial deposit:$";
cin >> P;
if (P < 0 || P > 100000)
{
cout << "Invalid must be between 0 to 100,000" << endl;
continue;
}
else
break;
}
return P;
}
double getIntrestRate(){
double r;
while (true)
{
cout <<"Enter the annual nominal interest rate:";
cin >> r;
if (r < 0.5 || r > 5.0)
{
cout <<"Invalid must be between o.5 to 5.0" << endl;
continue;
}
else
break;
}
return r;
}
double getYear(){
double t;
while (true)
{
cout <<"Enter how many years the money will be certified:";
cin >> t;
if (t < 1 || t > 10)
{
cout << "Invalid must be between 1 and 10" << endl;
continue;
}
else
break;
}
return t;
}
double calculateEndingBalance(double P,double t,double r,double n){
return (P*pow((1+r/(n*100)), n * t ));
}
void print(double P,double t,double r,double A){
std::cout << std::setprecision(2) <<std::fixed;
cout << " CERTIFICATE of DEPOSIT" <<endl;
cout << "Initial amount of deposit:$" <<P<<endl;
cout << "Annual interest: " << r << "%" <<endl;
cout << "Duration:" << t << "years(s)"<<endl;
cout <<"Ending balance:$" << A << endl;
}
int main()
{
double P, A, t, r, n=12;
char ans;
while (true)
{
P = getInitialDeposit();
r = getIntrestRate();
t = getYear();
double A = calculateEndingBalance(P,r,t,n);
print(P,r,t,A);
cout <<"Do you wish to go again? (Y/N): ";
cin >> ans;
if (ans == 'y' || ans == 'Y'){
continue;
}
else
break;
}
return 0;
}