Thread: Help ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    4

    Help ...

    First off, I'm new to this board and to C programming, but I have done some programming in VB. I am 5 weeks into a 10-week C class, and so far I've pretty well. So I am not completely lost. But I have come across a problem I don’t know how to tackle. I need to create a simple table showing the reforestation rate with a given amount of acreage (2500 acres) over a 20-year span at .02% rate. The formula is simple, 2500+(2500*.02). My problem is how to create an algorithm or method to calculate my reforestation rate for each year taking into account the previous years increase in acerage. I’ve created a program that does this, but it uses 20 While loops and 130 lines of code.

    Help, Thanks in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about one loop and two lines of code?
    Code:
    for( acres = startingacres, years=0; years < 20; years++ )
        acres *= 1.02;
    If my understanding of your math is right anyway...

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

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    4
    Thanks for the reply.....
    acres *= 1.02;
    gives me the proper answer for my first year, where i'm running into problems is for my second,third and subsequent years on my table. My end result will be

    YR Acres Forested
    1 2550
    2 2601
    3 2653
    4 2706
    etc. up to 20 years

    Thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It does give you the right amount.
    Code:
    #include <stdio.h>
    int main( void )
    {
            float acres, startingacres=2500.0;
            int years;
            for( acres = startingacres, years=0; years < 20; years++ )
            {
                    printf("%.0f acres at year %d\n", acres, years );
                        acres *= 1.02;
            }
            printf("%.0f acres at year %d\n", acres, years );
            return 0;
    }

    /* My output:
    2500 acres at year 0
    2550 acres at year 1
    2601 acres at year 2
    2653 acres at year 3
    2706 acres at year 4
    2760 acres at year 5
    2815 acres at year 6
    2872 acres at year 7
    2929 acres at year 8
    2988 acres at year 9
    3047 acres at year 10
    3108 acres at year 11
    3171 acres at year 12
    3234 acres at year 13
    3299 acres at year 14
    3365 acres at year 15
    3432 acres at year 16
    3501 acres at year 17
    3571 acres at year 18
    3642 acres at year 19
    3715 acres at year 20
    */
    Now that I've done all your work for you...

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

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    4
    Thanks, sometimes its hard to see the forest from the trees. I've been working on this problem all weekend. Although I know some C programming, I still have a lot to learn.

Popular pages Recent additions subscribe to a feed