• 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

    • XnView Shell Extension 4.2.0 by Razvan Serea XnView Shell Extension is a powerful Windows Explorer add-on that enhances file management by providing quick image previews, thumbnails, and context menu tools without launching XnView. It supports over 500 image formats including RAW (CR2/NEF), WebP, HEIC, TIFF, and vector formats (PSD/SVG), allowing users to resize, convert, edit, and optimize images directly from the right-click menu. The lightweight integration streamlines workflows, enabling batch processing, metadata viewing (EXIF/IPTC), and seamless format conversion—ideal for photographers, designers, and casual users who need efficient file handling. Beyond basic previews, the extension offers advanced features like image rotation, format adjustments, and plugin support. Its intuitive interface ensures fast access to editing tools while maintaining system performance. XnView Shell Extension key features: 500+ Format Support – Opens and converts RAW, WebP, HEIC, TIFF, PSD, SVG, and more Batch Processing – Convert, resize, or rename multiple images at once Lossless JPEG Editing – Rotate, flip, and adjust without quality loss Metadata Preservation – Retains EXIF, IPTC, and XMP data during conversions Advanced Compression – Customize JPEG quality, PNG optimization, and WEBP settings Color Management – Handles ICC profiles, bit-depth (8/16/32-bit), and CMYK-to-RGB conversion PDF & GIF Support – Extract images from PDFs or create animated GIFs High-Speed Previews – Fast thumbnails and image previews in Windows Explorer Right-Click Actions – Quick access to resize, rotate, and convert without opening apps Plugin Extensibility – Add support for niche formats like DDS, HDR, or DICOM Download: XnShell 64-bit | Portable 64-bit | ~10.0 MB (Freeware) Download: XnShell 32-bit | Portable 32-bit | ~3.0 MB Links: XnView Shell Extension Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Another win for EU users? Ads in WhatsApp won't be coming this year by David Uzondu You might have heard that ads are making their way to WhatsApp after years of the company promising it would never happen. If you are in the EU (lucky you), you won't be seeing ads until 2026 at the earliest. A new report from POLITICO confirms that Meta, which owns the messaging service, has informed Ireland's privacy regulator that the new advertising model will not roll out in the European Union for quite some time, even as it appears elsewhere in the coming months. This is not some charitable act, of course. The delay gives European regulators time to scrutinize the plan, which involves using ad preferences from linked Facebook and Instagram accounts to target users. This situation follows a pattern of other "wins" for EU users, like the changes in iOS 17.4 that finally enabled sideloading. This opened the door for alternative app stores and the (temporary) return of games like Fortnite to iPhones in the region. Similarly, we are seeing Microsoft finally back off from shoving Edge down the throats of EU users, all thanks to the Digital Markets Act. This legislation has put pressure on big tech companies to operate more "fairly" within the bloc, leading to changes that users everywhere else can only dream of for now. These regulations are precisely what companies like Apple hate. Remember, Apple has issued a warning to Australia, telling the country not to follow Europe's lead on these matters because it would create massive security and privacy risks. Apple argues that its control over the ecosystem keeps users safe, so any attempt to break that open is dangerous. The Irish Data Protection Commission will be meeting with WhatsApp to discuss the matter further. According to Commissioner Des Hogan, they plan to discuss the ad model with other European data protection authorities to gather any collective concerns. Commissioner Dale Sunderland noted that discussions with the company are "still early days", and it is too soon to identify what, if any, specific "red line issues" might exist with Meta's advertising plans. For now, Europeans can continue using their ad-free messenger, while the rest of the world prepares for the inevitable.
    • Welcome to Neowin!
    • Idiots never imagine their insane actions troubling everyone.  
    • Photo Variants 2.3 by Razvan Serea Photo Variants is an all-in-one photo editor for Windows. Quickly cull, import, and edit your images with powerful tools. Enjoy full layer support, precise retouching features, and a wide range of filters and color adjustments. Create multiple versions of a photo instantly with presets, or design from scratch using vector graphics and advanced editing options. Free for personal and commercial use. Photo Variants key Features: Advanced Adjustment Tools: Provides precise control over image modifications. ​ Extensive Filter Collection: Offers over 99 photo filters to apply various effects. ​ Animated Photo Effects: Enables the addition of dynamic elements to images. ​ Automatic Face Retouching: Includes features for enhancing facial features automatically. ​ Support for Multiple Formats: Compatible with over 100 graphic formats, including RAW and PSD files, allowing users to open, edit, and save in these formats. ​ Drawing and Transformation Tools: Facilitates freehand drawing, erasing, filling, cropping, resizing, rotating, and flipping images. Photo Variants supports a wide array of image formats, making it a versatile tool for all your editing needs. Key supported formats include: Raster Formats: .jpeg, .jpg, .png, .bmp, .gif, .tiff, .webp, .ico, .pcx. Camera RAW: .crw, .cr2, .dng, .nef, .raf, .arw, .orf, .x3f, .raw. Professional Formats: .psd, .ai, .svg, .tga, .pdf, .pcl. Specialized Formats: .dicom, .dcm, .heic, .heif, .avif, .exr, .dds. Other: .wmf, .emf, .xps, .jpeg2000 (.jp2)...etc... With support for these formats, Photo Variants offers seamless editing and flexibility for photographers, designers, and creatives. Photo Variants 2.3 changes: New effects for layers. New shapes and options for brushes. Download: Photo Variants 2.3 | 70.5 MB (Freeware) View: Photo Variants Home page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      Wayne Robinson earned a badge
      Week One Done
    • One Month Later
      Karan Khanna earned a badge
      One Month Later
    • Week One Done
      Karan Khanna earned a badge
      Week One Done
    • First Post
      MikeK13 earned a badge
      First Post
    • Week One Done
      OHI Accounting earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      688
    2. 2
      ATLien_0
      265
    3. 3
      Michael Scrip
      204
    4. 4
      +FloatingFatMan
      170
    5. 5
      Steven P.
      144
  • Tell a friend

    Love Neowin? Tell a friend!