Thread: Problems with a few "for" statements

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    17

    Problems with a few "for" statements

    Have an issue with some for statements.

    I must be missing somew logic here. I'm trying to get this compound interest program going. I can get it to go through 10 years on one interest rate, but when I throw in the second for statement to try to get that same output, but for varying interest rates it does not do as planned. In fact I somehow get 11%

    Code:
    #include <stdio.h>
    #include <math.h>  
    
    int main()
    {
       double amount;             
       double principal = 1000.0; 
       double rate = .05;         
       int year;                 
    
    	for ( rate = .05; rate <= .10; rate += .01 ) {
    
    		printf( "%4s%21s\n", "Year", "Amount on deposit" );
    
    			for ( year = 1; year <= 10; year++ ) {
    				
    					amount = principal * pow( 1.0 + rate, year );
    
    					printf( "%4d%21.2f\n", year, amount );  
    						
    		}
    	}
       return 0; 
    
    }
    Last edited by khskenny; 10-06-2006 at 04:08 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You shouldn't use floating point numbers for for loops if you can help it; due to floating point inaccuracies, the loop might not run the number of times you think it will.

    The problem with your program is that you don't initialize principal before the second for loop.

    [edit] You'll want something along these lines:
    Code:
    for ( year = 1, principal = 1000.0; year <= 10; year++ ) {
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for ( rate = .05; rate <= .10; rate += .01 ); {
    Maybe because you've got an extra semicolon in there?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Hrmm, since I am not using any floats for year or rate, it should be ok right?

    Also, is principal not initialized at the top @ 1000?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Quote Originally Posted by swoopy
    > for ( rate = .05; rate <= .10; rate += .01 ); {
    Maybe because you've got an extra semicolon in there?
    HAH!

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    However 1110 after one year at 5% interest doesn't seem quite right.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Quote Originally Posted by swoopy
    However 1110 after one year at 5% interest doesn't seem quite right.
    When I ran it sans extra semi colon, it did what I wanted, just not formated in an appealing or logical pattern, which I will fix. It does seem to do the interest sets as it should...

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Nevermind, rate was getting incremented to its final value by the first for-loop

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Thanks for the help guys. Bummer it was a careless error this time, but it got fixed. This forum ftw!

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by khskenny
    When I ran it sans extra semi colon, it did what I wanted, just not formated in an appealing or logical pattern, which I will fix. It does seem to do the interest sets as it should...
    After further observation, I've come to the same conclusion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. having problems with if and else statements
    By acohrockz21 in forum C Programming
    Replies: 10
    Last Post: 03-09-2008, 07:21 AM
  4. Examples Problems needed...
    By dcwang3 in forum C Programming
    Replies: 7
    Last Post: 02-14-2008, 09:13 PM
  5. Weird function problems
    By Inquirer in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2002, 07:49 PM