Thread: Help!!!

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    11

    Running total for multiple years

    Hello - I am trying to figure out how to create a running total per year. The start total should be my previous ending year total. Can anyone help me figure out what I am doing wrong because I am clueless.
    Thanks for the help


    Code:
    #include <stdio.h>
    
    int main()
    {
    	 float amt = 1000;
         float percent = .08;
         int year = 0;
         float yeartotal = 1000;
         
         yeartotal=yeartotal+(amt*percent);
    
         printf("Year     Start    End\n");
         printf("-----    -----    ----\n");
    
    	 for(year=1; year<=10; ++year)
         printf("%3d      %3.0f     %4.0f\n", year, amt, yeartotal*year);
    
    
         return 0;
    }
    This is what I get when I run my code.

    Code:
    Year     Start    End
    -----    -----    ----
      1      1000     1080
      2      1000     2160
      3      1000     3240
      4      1000     4320
      5      1000     5400
      6      1000     6480
      7      1000     7560
      8      1000     8640
      9      1000     9720
     10      1000     10800
    This is what it should look like.

    Code:
    Year     Start		  End
    -----    -----		  ----
      1      1000.00     1080.00
      2      1080.00     1166.40
      3      1166.40     1259.71
      4      1259.71     1360.49
      5      1360.49     1469.33
      6      1469.33     1586.88
      7      1586.88     1713.83
      8      1713.83     1850.94
      9      1850.94     1999.02
     10      1999.02     2158.94
    Last edited by Betty; 03-15-2006 at 06:16 PM. Reason: Title

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - Read the forum guidelines. "HELP!!!!" is not an appropriate title.
    2 - You only get one output, because that's all your loop does. You need to make your loop actually change the values inside the loop. Not just once outside it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    Thanks for the help - I have changed the title. I have tried changing the values inside the loop and had no success. Could you please be a little more specific, because I am new to C programming.
    Thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( x = 0; x < 10; x++ )
    {
        y = y + 2;
        printf("y is now %d\n", y );
    }
    You don't do anything in your loop, other than print the same unchanging value over and over.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    Thank you so much for the help. It is working. I am forever greatful.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    I jumped the gun. Your still great, but I need more help. My ending numbers are correct, but the start numbers are not. Year 1 should start at 1000, year 2 at 1080 and so forth. Thanks again
    Code:
    #include <stdio.h>
    
    int main()
    {
    	 
         float percent = .08;
         int year = 0;
         float yeartotal = 1000;
    
         
         printf("Year     Start    End\n");
         printf("-----    -----    ----\n");
    
    
    	 for(year=1; year<=10; ++year)
    	 {
    	 yeartotal=yeartotal+(yeartotal*percent);
    	 printf("%3d      %3.0f     %4.0f\n", year, yeartotal, yeartotal);
    	 }
    
         return 0;
    }
    Code:
    This is what I get.
    /* 
    Year     Start    End
    -----    -----    ----
      1      1080     1080
      2      1166     1166
      3      1260     1260
      4      1360     1360
      5      1469     1469
      6      1587     1587
      7      1714     1714
      8      1851     1851
      9      1999     1999
     10      2159     2159
     */

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Look at your for loop, and the order in which your statements are executed. What is the first thing that happens in the loop?

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    Thanks for the help. The first thing is it gives me the yeartotal, but I don't know how to fix it.

  9. #9
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    No, the second thing it does is print out the yeartotal. The first thing it does is update the yeartotal. This would seem to be the wrong way around, no?

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    Thanks for the help, but I'm confused. I am a beginner to C programming.
    This is my code. Are you saying that I should move this below the printf? Like this.
    Code:
    printf("%3d      %3.0f     %4.0f\n", year, yeartotal, yeartotal)
    yeartotal=yeartotal+(yeartotal*percent);

  11. #11
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Try it and see?

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    It didn't work. This is what I get. Any other suggestions would be greatly appreciated.
    Code:
    Year     Start    End
    -----    -----    ----
      1      1000     1000
      2      1080     1080
      3      1166     1166
      4      1260     1260
      5      1360     1360
      6      1469     1469
      7      1587     1587
      8      1714     1714
      9      1851     1851
     10      1999     1999

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Look at your output. You're outputting the same exact variable for start and end. So you can't very well expect printf() to do anything that makes them different. So you have an assignment statement that changes the value of year total. But when do you want to do that statement?

    Look at your code:
    Code:
    printf("%3d      %3.0f     %4.0f\n", year, yeartotal, yeartotal)
    yeartotal=yeartotal+(yeartotal*percent);
    Anyway you swap these two lines around, the last thing that's printed out before the assignment is yearend. Correct? But is that where you want to change your value? Look at your correct output:
    Code:
    Year     Start	     End
    -----    -----       ----
      1      1000.00     1080.00
      2      1080.00     1166.40
      3      1166.40     1259.71
      4      1259.71     1360.49
      5      1360.49     1469.33
      6      1469.33     1586.88
      7      1586.88     1713.83
      8      1713.83     1850.94
      9      1850.94     1999.02
     10      1999.02     2158.94
    What is the last output before the value changes?
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed