Colour Theme Font Size Options
 
 
 
Exercise 4

Programming Problem

Chemists, biologists, and biochemists, are often interested in calculating the pH level of a given solution. A solution with a pH level less than 7 is acidic; otherwise, it is basic. Determining the pH of a solution is important, since many organisms and micro-organisms can only live in a limited pH range.

Instructions:

  • Start a C++ program from scratch to solve the above problem.
  • Add code to template file ex4_pH.cpp in the replit team project or create it in your IDE.
  • Request an input for a concentration of hydroxide ions, denoted OH , and represented as a real number (you decide the appropriate type).
  • Calculate the concentration of hydronium ions, abbreviated H +, using this formula:


  • Calculate the pH level from the hydronium concentration with this formula:



  • For example, if the input solution is 0.002 OH , your program should print something like this:
       
    The pH level of solution 0.002 is 11.3010299957. The solution is basic.
  • Use proper loops and selection control structures - avoid use of continue and early return statements. (You should also avoid use of break to end loops for this exercise.)
  • Use meaningful variable names, proper indentation, and appropriate comments.
  • Use the output format and style shown in the sample run below.
  • Here are four hydroxide concentrations to use as test input :
          
    0.002 ( or 2.0e-3 ) 0.00006 ( or 6.0e-5 ) 0.000000007 ( or 7.0e-9 ) 0.0000000009 ( or 9.0e-10 )
  • Show your lab instructor that your program works.

Tips:

You may find the following will help you with the formulas:
  • This program requires you to use log base 10 - and the log() function doesn't do that in C++. There are several log functions in the cmath library - be sure to choose the right one.

  • C++ allows you to initialize and print values in scientific notation like this:
             double avogadro = 6.022e+23; // Avogadro's Number
          
    If you find that confusing, you can also use the pow() function from cmath.
  •             double avogadro = 6.022*pow(10.0,23.0); // Avogadro's Number
             

Sample Output

   
This program calculates ph level Enter 0 to quit the program. Please input the concentration of hydroxide: 0.002 The ph level of solution 0.0020000000 is 11.3010299957. The solution is basic. Please input the concentration of hydroxide: 0.00006 The ph level of solution 0.0000600000 is 9.7781512504. The solution is basic. Please input the concentration of hydroxide: 0.000000007 The ph level of solution 0.0000000070 is 5.8450980400. The solution is acidic. Please input the concentration of hydroxide: 0.0000000009 The ph level of solution 0.0000000009 is 4.9542425094. The solution is acidic. Please input the concentration of hydroxide: 0 You entered 0, so quit the program.

When you are done:

  • Take a screenshot demonstrating either:
    • Replit: 3 out of the 4 Input/Ouput tests are successful.
    • Other IDEs: a sample run with the test data shown in this exercise.
  • For this exercise, you will have: ex4_pH.cpp.
  • If you have completed this exercise, congratulations! Option 2 is complete! Please upload .cpp file and screenshots for the output to URCourses.