The constant percentage method of computing depreciation of an asset is based on the assumption
that the depreciation charge at the end of each year is a fixed percentage of the book value of the
asset at the beginning of the year. This assumption leads to the following relationship:
S = C(1 ─ d)n
where C = original cost of assed
d = depreciation rate per year
n = number of years
S = book value of the end of n years.
Write a program to compute the number of years of useful life of an asset given the original cost,
depreciation rate, and book value at the end of its useful life (called scrap value). The program
should also display the book value at the end of each year until the end of its useful life. Your
program should allow the user to repeat this calculation as often as the user wishes.
What is the help you need? They have given you an equation, in the form of S as the dependent variable. Just re-arrange it, to get an expression for number of years (n) viz n=S/(C*(1-d)), and use it in your program.
compute the number of years of useful life of an asset
after taking cost, rate and scrap value as input.
As far as the book value at the end of each year is concerned, you'll have to put it in a loop, somewhat like:
1 2 3 4 5 6
int i, book_value;
for (i=1; i<=n; i++)
{
book_value=(C*(1-d)*i);
cout<<"\nThe value of the book after "<<i<<" years is "<<book_value;
}
Put this entire program in do-while loop for
Your program should allow the user to repeat this calculation as often as the user wishes.
S = C(1 - d)n, solving for n? logC(1 - d)(S) = n. You might want to see a math help forum for these types of questions (not that most of us can't do high school algebra. :)
Take the ln of both sides. Solve for n, then turn it into c++:
1 2 3 4 5 6 7 8 9 10 11
#include <cmath>
int main()
{
float S, C, d, n;
//Initialize S, C, D here
n = (log(S) - log(C))/log(1-d);
return 0;
}
Here I've used floats, I did so because money doesn't need to be very precise (only 2 decimal points). I didn't use ints because I want the decimal and there is no log() function for ints (in standard) and doubles are sort of a waste of memory for what we are doing. To use the log() function, you need to include <math.h> for C or <cmath> for C++.
float book_value
int i;
for (i=1; i<=n; i++)
book_value=(C*(pow((1-d), n)));
cout<<\nThe value of the book after"<<i<<" years is: "<<book_value;
And yeah, everything should be float, 'cause number of years is not necessarily integral, and neither is rate, principal amount or value at any given time...
Why do you want a function? Its a straightforward program... Doesn't really require a function... And NO, no one will post the entire code... this forum is to help people out, not do their homework for them!