Sale!

HOMEWORK 4

$30.00

Category:

Description

Rate this product

HOMEWORK 4

NOTE: Homework will be graded on build.tamu.edu using g++ 5.3.0 with
-std=c++14. You are free to develop your programs on Visual C++ or any other
platform, but it is your responsibility to make sure your programs also compile
and execute correctly on build.tamu.edu using g++ -std=c++14.

The grade for this lab will be based on style (formatting, variable names,
comments, etc.), syntax (no compilation or link errors), and correctness
(passes all test cases). Follow the style guide at
http://www.stroustrup.com/Programming/PPP-style.pdf. Your grade for this lab is:
Problem # 1 2 3
Style /4 /4 /2
Syntax /6 /6 /3
Correctness /10 /10 /5
——————————————————————-
Total /20 /20 /10
Grand total _____/50

1. (20 points) Chemical formulae are usually written with the proportion of the
different atoms expressed as ratios of small integers, subject to the possible
ionization states. For example, water is H2O, which means that the ratio of the number of hydrogen atoms in a particular volume of water to the number of oxygen atoms is 2 to 1. Different atoms have different masses, so the ratio of the weights of hydrogen to oxygen in water is (2 * mass of H) to (1 * mass of O), i.e., 2 * 1 to 1 * 16, which is 2 to 16 by weight, so 18 grams of water would contain 2 grams of H and 16 grams of O. Going the other direction, if we know the ratio of weights, we can divide by the molar masses to get the formula.

Unfortunately, the calculation is often not that simple, since atomic masses are measured against carbon-12. That makes sodium 22.989769 instead of 23, the actual number of protons and neutrons per sodium atom. To further complicate matters, many atoms have several isotopes with different mass numbers. For example, most oxygen is 16, but a tiny fraction is 17 or 18. The result is an average value slightly over 16. Therefore, when working with real-world measurements, we need to find the closest simple ratio (where “simple” means “having the smallest denominator”).

In the case of ammonia, we may find that Avogadro’s number of ammonia molecules (called “1 mole”) contains 14.01 grams of nitrogen and 3.03 grams of hydrogen. Dividing by the molar masses (14 for nitrogen and 1 for hydrogen), we get 14.01/14 atoms of N to 3.03/1 atoms of H, i.e., 1.00071 to 3.03000, so the formula of ammonia would be N100071H303000, since the ratio needs to be expressed in integers. However, it is obviously much easier to say ammonia is NH3, and 1/3 is within 1% of 100071/303000, (and if we used ionization states we could deduce 1/3 is actually correct).

Now that you understand why it is needed, write a program named hw4pr1.cpp which repeatedly reads experimental data from the keyboard and finds the fraction with the smallest denominator which has less than 1% relative error.

Hint: Use nested for loops to try fractions in order of increasing denominator, e.g., 1/1, 1/2, 1/3, 2/3, …, till you find one close enough. (The sequence will be different if the ratio is greater than 1.)

In the example of ammonia above, your program should run like this:

What is the symbol for your first atom? N
What is the mass number of N? 14
What is the measured weight of N in grams? 14.01
What is the symbol for your second atom? H
What is the mass number of H? 1
What is the measured weight of H in grams? 3.03
Simplest close ratio of N to H is 1 to 3, so formula is NH3

(By convention, chemists write NH3, not N1H3, so don’t print the 1.)

Note: This process is called stoichiometry, if you want to look up more about it. There is an online reference table including mass numbers at
http://physics.nist.gov/cgi-bin/Compositions/stand_alone.pl.

Here is another test case: For 28.10 grams of silicon and 31.98 grams of oxygen your program should find SiO2.

2. (20 points) There is an old poem which was supposed to help you remember how to spell words containing “i” and “e” together (like “believe” and “receive”):

I before E,
Except after C,
Or sounded as A
In “neighbor” or “weigh.”

However, it was eventually realized that this “rule” is useless, since it is incorrect for so many words (like “ancient,” “weird,” “society,” etc.). Write a program named hw4pr2.cpp to count how many words in the online dictionary contain “cei” and how many words contain “cie.” The dictionary on build.tamu.edu is at /usr/share/dict/american.

3. (10 points) A version of the calculator program has had three bugs deviously inserted for you to find and correct. Find the errors by running the program and reading the code, fix the errors, and name your corrected version hw4pr3.cpp. The buggy program is at http://courses.cse.tamu.edu/daugher/misc/PPP/homeworks/calculator_buggy.cpp.