C++ Project: Trees and Carbon

The United Nations (UN) has set forth 17 goals for sustainable development. Goal 15 is designed to “Sustainably manage forests, combat desertification, halt and reverse land degradation, halt biodiversity loss.” The effects of deforestation – both man-made and otherwise – can have a dramatic impact in a number of social, biological, and economic sectors. For example, many industries rely on trees or tree products as a fundamental component of their operations, whether it be for construction, paper, or agriculture.

Trees (and, consequently, forests) provide a number of benefits, but are a resource which takes time to replenish. In this project, we will consider the growth rate of trees.

Benefits of Trees

Trees provide a number of benefits for the environment. Trees are able to absorb dangerous gases such as carbon dioxide (CO2) while, at the same time, producing oxygen for us to breathe. Trees also absorb and distribute water that otherwise may lead to runoff, thus reducing pollution and loss of topsoil. Some forms of trees even produce food for us to eat (think: apple trees) and provide habitat for many species. Watch the following animated video to learn more about the benefits of trees to our society. Though not Pennsylvania-specific, the concepts discussed in the video apply to Pennsylvania or any other temperate region:

Trees and CO2 Absorption

Trees absorb carbon dioxide through photosynthesis. The amount of CO2 absorbed can grow over time, as a tree with greater mass can old a greater amount of carbon. Suppose we wanted to measure the amount of carbon absorbed by a tree over time. In order to calculate the amount of carbon stored by a tree, we need to know a few facts about the tree. One thing we need to know is the diameter of the tree, measured at approximately 1.4 m (4.5 ft) above ground. This height is known as the diameter breast height. The other thing we need to know is the height of the tree, in centimeters (cm). We can use the diameter d and height h to compute the green weight mass (wood content + moisture) of the tree in kilograms (kg), as follows:

Diameter Formula
Less Than 28 cm    0.0577 x d2 × h
Greater Than 28 cm    0.0346 x d2 × h

Once we have the green weight mass gw, we can use that mass to compute the amount of carbon stored by the tree, using this formula:

carbon = 0.25 * gw

Where carbon is the mass of carbon in kilograms (kg). Finally, we can approximate the amount of CO2 stored by the tree using this equivalency: 3.67 kg for every 1 kg of carbon stored. Now that we can approximate the amount of CO2 stored in a tree, let’s consider what that would mean for an entire forest.

C++ Project: Computing The Benefits of Trees

This project will provide you with an introduction to the fundamentals of problem-solving in C++. The focus of this project is on how to solve simple problems using primitive C++ data types, variables, for loops, and arithmetic operations. In this project, you will…

  • Develop code to read data from the standard input and produce data to the standard output.
  • Translate a given mathematical expression into equivalent syntactically correct programming statements.
  • Write code that conforms to a programming style specified by the instructor.
  • Select and implement the appropriate control structure(s) for this problem.

Before you begin, it is recommend that you explore the content found in the For Loops in C++ electronic resource. This resource includes code examples, exercises, video discussions of C++ concepts, and a vocabulary list.

As you write your code, be sure to properly document your code. Consult the C++ Coding Guidelines document (provided by the instructor) for style expectations. Well-written code is easier to debug, easier to maintain over time, and easier to extend as new requirements arise.

Create a program called TreeBenefits.cpp in your C++ development environment. This program will ultimately compute the approximate amount of CO2 absorbed by a cluster a trees. Your program should input a series of 10 pairs (a tree height and a tree diameter) representing measurements for 10 trees. Your must program must use a for loop to control the number of input pairs received.

Once you have received the input, use the aforementioned formulas to produce  a table of values to the screen with five labeled columns, nicely formatted and spaced:

  • The height of the tree, in cm
  • The diameter of the tree, in cm
  • The green weight (mass)
  • The amount of carbon stored by the tree, in kg
  • The amount of CO2 stored by the tree, in kg

At the end of the table, you should also include a report of the total amount of CO2 absorbed by the tree, in kg. All floating-point output values should be precise to two decimal places. Validate the height input to verify it is greater than 30.48 cm and validate the diameter input to verify it is greater than 2.0 cm (you may use one validation loop for each input, or one loop for both inputs).

Here is a sample test you can run to check if your program is working correctly; your prompts should appear exactly as shown here, and you can type in the listed input values when prompted. It is highly recommended that you test the program with multiple sets of your own data to ensure that it is working – the instructor will do the same:

Test Case

Enter the height <space> diameter, both in cm: 57.0 9.2
Enter the height <space> diameter, both in cm: 50.0 7.0
Enter the height <space> diameter, both in cm: 68.0 8.7
Enter the height <space> diameter, both in cm: 64.0 10.1
Enter the height <space> diameter, both in cm: 58.0 9.6
Enter the height <space> diameter, both in cm: 50.0 9.0
Enter the height <space> diameter, both in cm: 60.0 8.6
Enter the height <space> diameter, both in cm: 70.0 9.3
Enter the height <space> diameter, both in cm: 52.0 6.6
Enter the height <space> diameter, both in cm: 47.0 9.9

     HEIGHT   DIAMETER        MASS          CARBON           CO2
     ------   --------        ----          ------           ---
     57.00      9.20         278.37          69.59         255.41
     50.00      7.00         141.37          35.34         129.70
     68.00      8.70         296.98          74.24         272.48
     64.00     10.10         376.70          94.18         345.62
     58.00      9.60         308.42          77.11         282.98
     50.00      9.00         233.69          58.42         214.41
     60.00      8.60         256.05          64.01         234.93
     70.00      9.30         349.33          87.33         320.51
     52.00      6.60         130.70          32.67         119.91
     47.00      9.90         265.79          66.45         243.87

                        TOTAL CO2: 2419.81

Here is an example of the feedback the user should see if he/she enters an invalid value:

Enter the height <space> diameter, both in cm: 20.0 7.0
*** ERROR: The height must be greater than 30.48 cm. Please try again.

Enter the height <space> diameter, both in cm: 68.0 7.0
Enter the height <space> diameter, both in cm: 64.0 1.7
*** ERROR: The diameter must be greater than 2.0 cm. Please try again.

Enter the height <space> diameter, both in cm: 64.0 9.6
Enter the height <space> diameter, both in cm: 30.0 1.0
*** ERROR: The height must be greater than 30.48 cm. Please try again.
*** ERROR: The diameter must be greater than 2.0 cm. Please try again.

Enter the height <space> diameter, both in cm: 60.0 8.6
...

Your program is now complete.

Applying The Program

Now that your program is complete, you will use your program to run some tests and perform some analyses. Create a Microsoft Word document (.docx) to hold the results of the tasks which follow. Clearly label each exercise, and add a title page to your document with the assignment, course, name, and date.

Exercise #1: Go to the Internet and search for two (2) new sets of data to use for testing. A good idea is to use the search query string ‘Tree dbh dataset” to find suitable datasets. Try to find data for trees of a larger diameter and a larger height than is shown in the test case above, and look for trees of at least two different species. List the source of your data and the data itself in your document. Remember to name the types of trees in your data.

If the datasets you find include diameter at breast height (dbh) data but omit height data, you may use the USDA formula to estimate the height data from the dbh:

h = a + b(dbh) + c(dbh)2 + d(dbh)3

A list of the coefficients (a, b, c, and d) can found in the file tree_coefficients.csv. Once you open the file in Microsoft Excel, look for the rows which include the scientific name of your tree (in column B) and look for the single row which includes the coefficients for tree height (see column E). The coefficients can be found in columns I through L.

Exercise #2: Run your program and enter the data you found. Copy-and-paste the output from your program into the Microsoft Word document (Hint: when your program at the “Press Any Key…” stage, right-click on the title bar of the DOS window and choose Edit -> Select All. Next, repeat the process but choose Edit -> Copy). When you paste the output into your Word document, make sure you change the font of the output to a fixed-type font such as Lucida Console or Courier New.

Exercise #3: Looking at the results you got from your trial runs, do some types of trees seem to absorb more carbon and CO2, or is it simply a matter of the height and diameter?

Exercise #4: Consider that every gallon of gasoline burned produces 8.89 kg of CO2, Based on the results from your data and program runs, along with your own calculations and research, write a 100-200 word argument that defends one and only one of the following two statements: (a) Our society can plant enough trees to overcome this level of emissions, OR (b)  Our society cannot plant enough trees to overcome this level of emissions. Your argument should be detailed and should be defensible against counterarguments – research can assist you in providing data to back up your argument.

Deliverables

See the instructor for submission instructions and due date(s).

Powered by WordPress. Designed by WooThemes

Skip to toolbar