Thread: Need Suggestions on how to start with a For loop

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    Question Need Suggestions on how to start with a For loop

    A problem in timber management is to determine how much of an area to leave uncut so that the harvested area is reforested in a certain period of time. It is assumed that reforestation takes place at a known rate per year, depending on climate and soil conditions. The reforestation rate expresses this growth as a function of the amount of timber standing. For example, if 100 acres are left standing and the reforestation rate is .05, then at the end of the first year, there are 100+.05*100 or 105 acres forested. At the end of the second year, the number of acres forested is

    105+.05*105 or 110.25 acres.

    Assume that the total area to be forested, the uncut area, and the reforestation rate are known. Write a program to determine the percentage of the total area that is forested after X years. The program should be user friendly with prompts and comments where needed. Remember the forest service depends upon you. Output should give the input data plus the number of acres forested and the percentage of the total that this represents.

    Use the following input data to test the program:

    ************************************************** ******************************

    Area Number Total Area Uncut Area Reforestation Rate
    ************************************************** ******************************

    45 10,000 100 .05

    83 1,000 50 .08

    153 20,000 500 .10

    192 14,000 3,000 .02

    234 6,000 1,000 .01

    years 20

    i cant figure out how to start the for loop?
    i know i need a LCV (loopcontrol variable) only one i could think of using is years like
    for ( int years; years>0; years--)
    but if this works what do i put after ? an if statement? to make it do the calculating? any suggestions would be greatly welcomed

    thanks in advance

    416 18,000 1,500 .05

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    You should use power, e.g. as in the example 100 acres, rate of 0.5, after X years: 100*(0.5^X): 100*pow(0.5, X), whithout a for loop, or with for loop:

    double dAccum = 1;
    for (int i = 0; i < X; ++i)
    {
    dAccum *= 0.5;
    }
    Which is just the same, but even slower

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    ko0 thanks..but we HAVE to use for loops grrr

    ko0 whats the "*="? i havent came across that yet anywhere in class or any of the book so far?
    when u used dAccum? that was for forested area right? an the loop the "i"? doesnt represent anything?

    i think i get it kinda.........but all the example only have
    for(int x=0;x<=100;x++)
    cout << x
    i have tried to place a C++ statement like area_forest=uncut_area+(rate*uncut_area)
    but it just outputs a zero everytime.........

    i think i am just completely LOST :

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    Please read more before posting here.
    A *= B; means: A = A*B
    give us a code and we will show you your errors, but again read more, you should understand principles of programming first

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    okie but what did u have dAccum stand for?

    i havent seen it used in that form..........i guess the author of our book choose a different way or skipped it sorry........

    i think i got it........just wondering why the dAccum?

    oh yeah illpost part the "hopefully" working code in a few minz

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    here is the part of the code i am having problems with.....

    i cant seem to figure out whats wrong....i have narrowed it down to this loop

    for (double i=1; i<years+1;++i)
    {
    area_forested = (uncut_area + (forest_rate * uncut_area)*i)
    }

    its supposed to input X number of years (years), the rate of reforestation(forest_rate), the area left uncut(uncut_area).
    And then determine what the area forested will be after X years has passed.

    when i enter i year it output 105 which is correct, but when i enter 2 yrs it output 110.00 and it should be 110.25, it seems to drop off all the decimals? I have set all my variables to double.
    took out the set percission an then there was no decimal? I tested each section of my code an all is working fine except this for loop part? i get correct answers for years 1-9 without the decimal?

    if it helps the desk check i am using is 20years,rate .05,total area 10,000 , and uncut area 100. i was told the answer should be something like 635.33?

    ......any ideas?.......i even confussed my prof. haha.....any suggestions will be greatlly welcomed



  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    First of all you do not need all of the variables to be double; but you need "area_forested", "uncut_area" & "forest_rate" of that type. "i" can be (& is better to be) int.

    for (double i=1; i<years+1;++i)
    {
    area_forested = (uncut_area + (forest_rate * uncut_area)*i)
    }

    Of course that is not you problem. Your real problem is that you do not know how to power a number. Let us see the result of your program now:
    1 year:
    area_forested =100 + 100*0.05*1;
    so you have 105

    2 year
    i = 1:
    area_forested = 105
    i = 2:
    area_forested = 100 +100*2*0.05 = 110
    so loop exits with 110

    So why do you need the loop, as only the last step is used? The idea is that the value should acumulate the pow of 0.05:
    double dMultiplyAccum = 1;
    i = 1:
    dMultiplyAccum *= 0.05
    i = 2:
    dMultiplyAccum *= 0.05
    ....
    After the loop:
    area_forested = area_forested + area_forested*dMultiplyAccum;

    this means area_forested = area_forested + area_forested*forest_rate^years
    where '^' means powered to:

    double dMultiplyAccum = 1;
    for (int i=1; i<years+1;++i)
    {
    dMultiplyAccum *= forest_rate;
    }

    //here dMultiplyAccum is forest_rate^years
    area_forested = uncut_area + uncut_area*dMultiplyAccum;

    By the way have you study mathematics?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free Book Suggestions!
    By valaris in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-08-2008, 10:25 AM
  2. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM
  3. don't know where to start
    By spikerotc04 in forum C Programming
    Replies: 5
    Last Post: 04-26-2005, 04:21 PM
  4. C++ help: Don't know where to start
    By firestorm in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2005, 06:39 PM
  5. Linked List Anamoly
    By gemini_shooter in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2005, 05:32 PM