Rule of 72

This is a discussion on Rule of 72 within the C++ Programming forums, part of the General Programming Boards category; I have done part of this program so far. The Rule of 72 is used by bankers and politicians which ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    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.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    Please someone help me. All My problems are the output. I need a loop that repeats to answer.

  3. #3
    lurker
    Guest
    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?

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    yeah i'm sure everything is right. just need help with output. i'm also having trouble displaying this website

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    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.

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    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();
    }
    *Cela*

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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:
    Code:
    year=72/rate;
    There is no reason for your for loop which constantly divides year by 2.

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    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 ###

  10. #10
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Did you take out the math.h header?

  11. #11
    lurker
    Guest
    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));

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    Somebody told me to include <math.h>

  13. #13
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    abs should be in the math header file, so make sure that it is there. Did it work fine before?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File IO
    By Jack1982 in forum C++ Programming
    Replies: 9
    Last Post: 10-15-2007, 01:14 AM
  2. Winter 2006 72 Hour Game Development Competition
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-26-2006, 08:34 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Rule of 72 Code Help plz
    By wnight77 in forum C Programming
    Replies: 9
    Last Post: 01-31-2003, 03:38 PM
  5. Who should rule the world?
    By CoderBob in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 02-07-2002, 06:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21