C++ Project: Ocean Acidification

The United Nations (UN) has set forth 17 goals for sustainable development. Goal 14 is designed to “Conserve and sustainably use the oceans, seas and marine resources for sustainable development.” The oceans serve the human population in many different ways. Besides the economic provided by such industries as fishing, oceans also serve to help minimize the effects of global warming (absorbing CO2) and are the primary protein source food for billions of people. Maintaining the water quality and ecosystems of our oceans is truly an imperative for human survival, but the oceans are all too often impacted by man-made issues (e.g. pollution).

In this project, we will consider one indicator of climate change: the level of acidity present in our oceans.

Acidity and pH

One measure of acidity (alkalinity) in a chemical solution is the potential hydrogen (pH) level. The pH of a solution is a measure of the concentration of hydrogen ions using a simple numeric scale. The pH level is said to vary from 0.0 to 14.0, with the following categories:

pH The Solution Is… Examples of This…
[0.0…3.0) Very Acidic Stomach acid, lemon juice
[3.0…7.0) Acidic Orange juice, beer
7.0 Neutral Distilled water
(7.0…12.0) Alkaline Baking soda, blood
[12.0…14.0] Very Alkaline Bleach, ammonia

 

Note: remember that [ means “inclusive” and ) means “non-inclusive”. Calculations for the pH value of a solution are useful to identify the effects of chemical reactions, as well as for environmental purposes. For example, lower than normal pH levels in lakes cause a higher concentration of toxic metals to develop. This increased metal concentration has negative effects on the ability of fish and other aquatic life to survive. This is one of the primary problems posed by so-called “acid rain”.

Ocean Acidification

By measuring pH in the oceans, scientists can get a sense of any changes in the ocean chemistry related to the amount of CO2 absorbed by the water. As  CO2 levels rise in the atmosphere, they also rise in the ocean; this results in lower pH (more acidic) and can impact the ability of various organisms to live and thrive, especially shellfish. On average, the oceans absorb about 25% of the world’s  CO2 in any given year, much of it from man-made sources (e.g. industrialization). Check out this brief video which depicts this process and its effects:

The government currently sponsors monitoring stations across the world to keep track of changes in ocean chemistry. The data from these observation stations is made public for the purposes of research and education. In this project, we will examine a set of data from one such observation station, in the Pacific Ocean, near Hawaii.

Here are some links to additional information on this topic:

C++ Project: Ocean Acidification

This project will provide you with an introduction to the fundamentals of user-defined structured data types and file I/O in C++. In this project, you will…

  • Develop code to read data from the standard input and produce data to the standard output.
  • Develop code to read data from a text file and write data in a prescribed format to a text file.
  • Write code that conforms to a programming style specified by the instructor.
  • Select and implement the appropriate control structure(s) for this problem.
  • Decompose the given problem into a sequence of single-purpose functions that are highly cohesive and loosely coupled.
  • Declare, define, and initialize one-dimensional arrays of a fixed size.
  • Demonstrate the ability to read from and write to an arbitrary array element using array indices.
  • Demonstrate an ability to process the entire array, one element at a time, performing both read and write operations.
  • Demonstrate the ability to create and utilize a user-defined structured data type (struct).

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.

Exercise #1: With this project, we will begin to create a simple program to analyze a set of data on ocean acidity. Create a program called Ocean.cpp in your C++ development environment. Add code to your main function to prompt the user for a filename and echo-print it back to the user. Make sure your program permits entry of a string with spaces! Use the following test case as a trial run:

Test Case #1

Please enter the name of a file: sample.txt
You entered the filename 'sample.txt'.

Exercise #2: Next, we will add code to open the file selected by the user.  Make sure that you include the <fstream> library. Add code to your  main function to do the following:

  • Create an ifstream variable.
  • Using the ifstream variable, open the user-specified file entered and check to see if the “open” worked. If it did not, inform the user. If the “open” worked, output a simple message to the user indicating that the file is now open.
  • Close the file and tell the user it is closed.

Make sure that everything compiles and builds. Run the application. Make sure your output matches that shown in the following test case; please note that it assumes a simple file named sample.txt exists in the same folder:

Test Case #2

Please enter the name of a file: sample.txt 
You entered the filename 'sample.txt'.

File 'sample.txt' is now open.
File 'sample.txt' is now closed.

Assuming the sample.txt file does NOT exist in the folder, you should see the following output:

Test Case #3

Please enter the name of a file: sample.txt 
You entered the filename 'sample.txt'.

ERROR: File 'sample.txt' is NOT open.

Make sure your program works for both of these tests.

Exercise #3: Create a new file in your C++ project called AcidityRecord.h. Add a new struct definition to the file called AcidityRecord. This struct will have three members (fields) – the year timestamp, the pH level of the water, and the amount of dissolved CO2 (all of type double). Once the file has been created and the new type declaration has been added, save your file in the same folder as your Ocean.cpp file. Make sure you add code to your Ocean.cpp file to include the new AcidityRecord.h file.

Next, add some code to the main function to create 512-element array of type AcidityRecord  (the variable name is up to you, adhering to style guidelines). Create a function to read the user-specified data file,  one field at a time, until the end-of-file (EOF) has been encountered. The user-specified file is your data file for the program; it includes three values per observation (year timestamp, observed pH, and amount of dissolved CO2 measured as partial pressure in micro-atmospheres). Each observation is one a single line, with the three values separated by spaces. Store each three-part “record” into one element of your array. Once the EOF has been encountered, output the number of “records” input from the file. Add code to call this function in your program.

For testing, download the file named ocean-acidity.txt from Canvas, placing it into the same folder as your code. Make sure that everything compiles and builds. Run the application, and test your code against the following test case:

Test Case #4

Please enter the name of a file: ocean-acidity.txt 
You entered the filename 'ocean-acidity.txt'.

File 'ocean-acidity.txt' is now open.
262 record(s) have been read from the file.
File 'ocean-acidity.txt' is now closed.

Once you see the output above, continue on to the next exercise.

Exercise #4: Add a function to your program to output the contents of your array as a report, complete with headers. For purposes of the report, you will have four columns – year, timestamp, pH value, and dissolved CO2 value.  Make sure that everything compiles and builds. Run the application, and test your code against the following abbreviated test case:

Test Case #4

Please enter the name of a file: ocean-acidity.txt 
You entered the filename 'ocean-acidity.txt'.

File 'ocean-acidity.txt' is now open.
262 record(s) have been read from the file.

YEAR      TIMESTAMP          PH            DISSOLVED CO2
----      ---------       --------         -------------
1988         833333	    8.1097	        330.9000
1988         920765	    8.1092	        330.6000
...          ...            ...                 ...
2014         961644	    8.0744	        369.1000

File 'ocean-acidity.txt' is now closed.

Once you see the output above, continue on to the next exercise.

Exercise #5: As a final step, we will add code to determine some simple statistics from the file input data. Add one or more functions to compute….

  • The average of the first 10 pH readings
  • The average of the last 10 pH readings
  • The percentage difference between the second average and the first average

You will repeat this process for the dissolved CO2 readings. Make sure that everything compiles and builds. Run the application, and test your code against the following test case; your prompts and error messages should appear exactly as shown here:

Test Case #5

Please enter the name of a file: ocean-acidity.txt
You entered: the filename 'ocean-acidity.txt'.

File 'ocean-acidity.txt' is open.
262 records have been read from the file.

YEAR      TIMESTAMP          PH            DISSOLVED CO2
----      ---------       --------         -------------
1988         833333	    8.1097	        330.9000
1988         920765	    8.1092	        330.6000
...          ...            ...                 ...
2014         961644	    8.0744	        369.1000

************ pH Levels ************
Average (First 10): 8.11
Average (Last 10): 8.07
Percent Difference: -0.48

************ Dissolved CO2 Levels ************
Average (First 10): 329.69
Average (Last 10): 372.04
Percent Difference: 12.85

File 'ocean-acidity.txt' is closed.

Note here the changes in levels; as the dissolved CO2 levels has risen, the pH has fallen.

Your program is now complete.

Deliverables

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

Powered by WordPress. Designed by WooThemes

Skip to toolbar