-
Rule of 72
I have done part of this program so far. The Rule of 72 is used by bankers and politicians which states that if R% is the annual rate of inflation, then a fixed sum of money will decline in value by 50% in 72/R years
I need to prove that Rule of 72 is reasonable for inflation rates of from 1 to 25%
Code:
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
int main()
{
// Declare Variables
double years;
double rate;
double calculate;
int hold;
//formula = abs(log10)(.5)/(log10(1+rate));
// Prove Rule of 72
int r = rate;
for (r = 1; r < 26; r++)
{
int y = years;
for (y = 1; y < 73; y++)
{
years = years / 2;
}
calculate = abs(log10(.5))/(log10(1+rate));
// Print Results to screen
cout << " Depreciation Chart in years to Test Rule 0f 72 " << endl;
cout << "Rate of % Inflation " << setw(5) << "#years by Rule of 72 " << setw(5) << "Calculated # of years " << endl;
cout << r << setw(5) << years << setw(5) << calculate << endl;
break;
}
cin >> hold;
return 0;
}
This is my code so far. The formula is right because I looked it up in the internet. I have a problem printing to the output. I used a for loop and i want to start from 1 and increment it by 1 until it reaches 25 and i need it to print that and the years and calculated # of years.
-
Please someone help me. All My problems are the output. I need a loop that repeats to answer.
-
I can't help with the output right now, but are you sure your code is right? For instance:
Code:
int r = rate;
for (r = 1; r < 26; r++)
first you set r to equal rate (which by the way is not initialized so it has garbage in it), then you change r to 1. Why even bother setting r to equal rate? Same thing with 'y' and 'years'. Or is there other code that you didn't post?
-
yeah i'm sure everything is right. just need help with output. i'm also having trouble displaying this website
-
What does your output look like now? And also show us what you want your output to look like exactly.
The easiest way to see what is going on in your program is to go step by step through the code with a pencil and paper and keep track of what variable is what.
-
The Output should say
Code:
Depreciation Chart in years to Test Rule 0f 72
Rate of % Inflation #years by Rule of 72 Calculated # of years
1 72 calculation formula
2 36
3 18
4 9
5 3
The first row counts years 1 through 25. I tried to use a For Loop to do this but not sure how to print results. The second row starts at 72 and divides by 2 all the way down. My program has a formula that finds out the years so it should print the results of that as well. My Output doesn't come out right. This is where I need help.
-
Here is your program that only prints the first 5, like your example output. That's easy to set however you want though. :-)
Code:
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
int main()
{
cout <<"Depreciation Chart in years to Test Rule 0f 72\n" << endl;
cout << "Rate of % Inflation " << setw(5)
<< "#years by Rule of 72 " << setw(5)
<< "Calculated # of years " << endl;
double years = 72;
for (int r = 1; r < 6; r++)
{
double calculate = abs((long)log10(.5))/(log10(1+r));
cout << r <<setw(21)<< years <<setw(20)<< calculate << endl;
years /= 2;
}
cin.get();
}
-
I thought the years was 72 divided by the inflation rate? If so, then why are you dividing the year by 2 every time? You need to set year like so:
There is no reason for your for loop which constantly divides year by 2.
-
Now when I try to run my program, it gives me an error saying
Code:
Error : undefined identifier 'abs'
Ruleof72.cpp line 26 calculate = abs(log10(.5))/(log10(1+rate));
Error : undefined identifier 'abs'
Ruleof72.cpp line 38 double calculate = abs((long)log10(.5))/(log10(1+r));
Error : expression syntax error
Ruleof72.cpp line 48 }
Error : ### Error: Compilation aborted at end of file ###
-
Did you take out the math.h header?
-
I was able to compile by changing the headers to non .h and adding 'using namespace std;, and I had to change the abs(long) to abs(long double):
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
...
double calculate = abs((long double)log10(.5))/(log10(1+r));
-
Somebody told me to include <math.h>
-
abs should be in the math header file, so make sure that it is there. Did it work fine before?