• 0

While loop (Reforesting problem)


Question

Hi guys, this is the other half of my assignment. ( I think im pretty much done with this one, I just cant figure out 1 thing) Sorry if Im not allowed to post 2 topics at once, I looked at the forum rules and couldn't find anything saying it was against the rules.

Anyways, with this program, 2 things are happening..1st, Im getting some huge numbers when It runs the program.

second, the black debugger box isnt staying open while I run the program. It exits before I can see the numbers. (it worked once, and thats why I saw the huge numbers..but hasnt worked since).

If someone could maybe modify a line or two to make it work, or tell me what I did wrong so I can try to fix it that would be good. Id prefer if you didn't rewrite the whole program.

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <iostream>

int main()

{

/* Declare and initialize variables. */

int year=1 ;

double Acres = 14000, uncut = 2500, Regrowth_rate = .02;

double reforest = 0;

while(year<=20)

{

reforest = uncut *(1+Regrowth_rate); // calculate growth

uncut = uncut + reforest ; // add that to uncut

cout << year << "\t" << reforest << endl;

year++;

printf ("%d %lf\n", year, reforest);

}

Link to comment
https://www.neowin.net/forum/topic/630273-while-loop-reforesting-problem/
Share on other sites

6 answers to this question

Recommended Posts

  • 0
  Doli said:
what calculations are you trying to do?

Sorry I should have put that up there first. My mistake.

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. A reforestation

equation expresseses this growth as a function of the amount of timber standing and the

reforestation rate. For example, if 100 acrea are left standing after harvesting and the

reforestation rate is 0.05, then 100 + 0.05 x 100, or 105 acrees, will be forest at the end of

the first year. At the end of the second year, the number of acres of forest will be 105 +

0.05 x 105, or 110.25 acres.

1)

Assume that there are 14,000 acres total with 2,500 acres uncut and that the

reforestation rate is 0.02. Print a table showing the number of acres reforested at

the end of each year, for a total of 20 years.

  • 0

This is gonna sound weird, but initialise the values at 0 and then change them appropriately. Not sure if this will work, but add in a "break;" after the do-while.

Also, I assume you know you have a closing bracket missing at the end of your code, right (the one that closes the main method)?

EDIT: Think I know your problem...

/* your old code */
reforest = uncut *(1+Regrowth_rate); // calculate growth
uncut = uncut + reforest; // add that to uncut

/* my replacement code */
reforest = (uncut * Regrowth_rate) + uncut; // calculate growth
uncut += reforest;

Try this.

Edited by The Tjalian
  • 0
  cooltee13 said:
Sorry I should have put that up there first. My mistake.

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. A reforestation

equation expresseses this growth as a function of the amount of timber standing and the

reforestation rate. For example, if 100 acrea are left standing after harvesting and the

reforestation rate is 0.05, then 100 + 0.05 x 100, or 105 acrees, will be forest at the end of

the first year. At the end of the second year, the number of acres of forest will be 105 +

0.05 x 105, or 110.25 acres.

1)

Assume that there are 14,000 acres total with 2,500 acres uncut and that the

reforestation rate is 0.02. Print a table showing the number of acres reforested at

the end of each year, for a total of 20 years.

Your calculations in the while loop are wrong. If you tried the example :

  Quote
For example, if 100 acrea are left standing after harvesting and the

reforestation rate is 0.05, then 100 + 0.05 x 100, or 105 acrees, will be forest at the end of

the first year. At the end of the second year, the number of acres of forest will be 105 +

0.05 x 105, or 110.25 acres.

in your program you would not get the same answer so you would know that its wrong. Plan it out on paper first.

hint: result = "acres left standing" + "reforestation rate" X "acres left standing"

  • 0

int main()

{

/* Declare and initialize variables. */

int year=1 ;

double Acres = 14000, uncut = 2500, Regrowth_rate = .02;

double reforest = 0;

while(year<=20)

{

uncut = uncut + (uncut*Regrowth_rate); // calculate growth

cout << year << "\t" << uncut << endl;

year++;

}

return 0;

}

This will give you 3714.87 after 20 years assuming a 2% regrowth.

You were essentially redundantly adding to your uncut value and unnecessarily complicating things it seemed.

  • 0
  cykosis said:
int main()

{

/* Declare and initialize variables. */

int year=1 ;

double Acres = 14000, uncut = 2500, Regrowth_rate = .02;

double reforest = 0;

while(year<=20)

{

uncut = uncut + (uncut*Regrowth_rate); // calculate growth

cout << year << "\t" << uncut << endl;

year++;

}

return 0;

}

This will give you 3714.87 after 20 years assuming a 2% regrowth.

You were essentially redundantly adding to your uncut value and unnecessarily complicating things it seemed.

Ahh ok, ya I see what I did now, thanks for the help. Much appreciated.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I would suggest Ubiquiti Cloud Gateway Fiber + U7 Pro XGS   https://store.ui.com/us/en/category/all-cloud-gateways/collections/cloud-gateway-fiber/products/ucg-fiber https://store.ui.com/us/en/category/wifi-flagship/products/u7-pro-xgs?variant=u7-pro-xgs-us I have been using this combo for 3 months now.
    • YouTube app is no longer compatible with several iPhones by Taras Buria Google is rolling out a new update for the YouTube app on iOS, and it is bad news for those still using an old iPhone. Starting with version 20.22.1, the latest release in the App Store, YouTube for iPhone now requires iOS 16 and newer. Previously, the app required a device with iOS 15 and newer. The new hardware requirement makes several iPhones incompatible with the latest version of the YouTube app. Those include the iPhone 6S / 6S Plus, iPhone 7 / 7 Plus, and the original iPhone SE. The seventh-generation iPod touch is also no longer compatible. On the iPad side, the update kicks out the second-generation iPad Air and the fourth-generation iPad mini. Although old iPhones can no longer install YouTube version 20.22.1 and newer, old versions continue working just fine. However, going forward, users will not receive new features and improvements. Also, at some point in the future, the app will stop working altogether. Another workaround is to use the web version of the app, but that also provides a limited experience compared to the up-to-date client. Google is not the only service that kicks out old and now unsupported Apple devices. Recently, WhatsApp developers dropped iPhones that cannot run iOS 15.1 or newer. The news is clearly a bummer for those who are still using their old iPhones, which, to Apple's credit, have a solid lifespan (as long as you are happy with the battery life). However, it is also worth keeping in mind that the iPhone 6S series was released almost a decade ago. Having ten years of support is impressive, but it is clearly time to move on. Perhaps my friend with his day-one iPhone 7 Black can finally consider buying something less ancient.
    • Guys have already appeared saying that Windows 11 ruined their lives, stole their bank password, killed their dog, kidnapped their mother and that's why they will never install Windows 11 again.
    • I think they start to use AI in Robots but in very early stage. I think in less than 20 years we will see these movies in real life
  • Recent Achievements

    • Reacting Well
      Cryptecks earned a badge
      Reacting Well
    • Week One Done
      primussolicitors798 earned a badge
      Week One Done
    • First Post
      nothin earned a badge
      First Post
    • Enthusiast
      Epaminombas went up a rank
      Enthusiast
    • Posting Machine
      Fiza Ali earned a badge
      Posting Machine
  • Popular Contributors

    1. 1
      +primortal
      188
    2. 2
      snowy owl
      134
    3. 3
      ATLien_0
      130
    4. 4
      Xenon
      119
    5. 5
      +FloatingFatMan
      97
  • Tell a friend

    Love Neowin? Tell a friend!