Solved

ANOTHER C++ QUESTION

Posted on 2013-07-01
7
212 Views
Last Modified: 2013-07-15
I am creating a financial consule application to calculate deposits each month for 40 years at 0.07 interest.  The amount deposited per month is $300.00 for a total of 480 months.  I need to calculate that total, then take an annual payment of $91800 per year for 30 years from the total calculation.  With each annual withdrawal I want to calculate the new balance at .085 percent interest until the end of the 30 year period.  The problem I am having is that my code seems to show the inital calculation in exponent form, also when I take the annual payment it adds instead of subtracting from the balance.

The code for the first 40 years is below:

double  p(0), fvr(0.07), r(.085), fv(0), Pi(0), dep(300), pmt(91800);

 int     n(480), i(0), e(0), y(30);
 

   while( i != n )                            //  iterate  n  times
   {
      

         fv = fv * ( 1 + fvr )  +  dep;

             ++i ;          //increment to next period
   }

//----- Code for the remaining 30 years-----

    while( e != y )                            //  iterate  n  times
   {
      

         Pi  =  Pi * (1 + r ) - pmt;
        

             ++e ;          //increment to next period
   }

--------------------------------

Thanks in advance.

aj85
0
Comment
Question by:aj85
7 Comments
 
LVL 20

Expert Comment

by:n2fc
ID: 39291681
Do you really mean .07  interest PER MONTH?

You probably want to divide it by 12 to get monthly factor!

Probably why your money grew so fast!
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 400 total points
ID: 39291839
To understand your problem:

You have a situation where you are depositing $300 / month for 480 months @ 7% p.a.

Do you then want to start withdrawals from the total account of $91800 / year for 30 years at 8.5%

Why can't you use the geometric formula here?

For the first bit
Sn = a(1-r^n)/1-r
So for your example that would be
S(480) = 300(1-(1+7%/12)^480)/(1-(1+7%/12)) = 787444

Don't understand the next bit - if you are going to be withdrawing $91800 / year against the 787444 amount with 8.5% interest - your account will be empty after around 13 years - so not sure where the 30 years comes into it

Or did I miss the boat ...
0
 

Author Comment

by:aj85
ID: 39292162
Hello Julian,

Well you are correct in your understanding.  The issue is that the interest on the first 40 years should be componded daily at the 7%.  Then when the annual withdrawal is made, the first 91800 should come from the principal balance, then what is remaining should be calculated at the 8.5%.

Thanks.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 100 total points
ID: 39319990
The problem I am having is that my code seems to show the inital calculation in exponent form

to output a floating point value in decimal form, you may use appropriate formatting:

double mydouble = (123/100)*7;
int fraction = 2; 
// calling fixed + setprecision determines count of fraction (digits after decimal point)
std::cout << std::fixed << std::setprecision((size_t)fraction) << mydouble;

Open in new window


Sara
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39320512
Is there a reason you are not using the geometric fomula as outlined in my previous post?
0
 

Author Closing Comment

by:aj85
ID: 39327878
Thank you both for the quick and sound solution.  Julian, you were correct about the geometric formula solution.  Sara, thanks for the correct formatting as well.

Thanks,
AJ
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39328741
You are welcome - thanks for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 186
C++ question 3 61
Printing the elements of a set declared inside a map in C++. 3 38
Path to  STL Map header file 1 41
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now