Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the...
-
Upload
ruth-willis -
Category
Documents
-
view
216 -
download
0
Transcript of Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the...
![Page 1: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/1.jpg)
ChemistryCS1020
![Page 2: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/2.jpg)
Given a set of mappings and chemical formula:
• Calculate the molecule mass of the formula
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
![Page 3: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/3.jpg)
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
![Page 4: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/4.jpg)
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
14 + 4 x 1
![Page 5: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/5.jpg)
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
18 * 2
![Page 6: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/6.jpg)
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
36 + CO3
((CH3)CH4C3H8)4
CH4OH
12 + 3 x 16
![Page 7: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/7.jpg)
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
36 + CO3
((CH3)CH4C3H8)4
CH4OH
60
![Page 8: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/8.jpg)
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
36 + 60
((CH3)CH4C3H8)4
CH4OH
![Page 9: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/9.jpg)
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
![Page 10: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/10.jpg)
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
12 + 4 x 1 + 16 + 1
![Page 11: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/11.jpg)
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
33
![Page 12: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/12.jpg)
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
![Page 13: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/13.jpg)
Let’s work on a few examples based on the given mapping
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
12 + 3 x 1
![Page 14: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/14.jpg)
Let’s work on a few examples based on the given mapping
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
15
![Page 15: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/15.jpg)
Let’s work on a few examples based on the given mapping
C 12H 1N 14O 16
(NH4)2CO3
(15 + CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
12 + 4 x 1 + 3 x 12 + 8 x 1
![Page 16: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/16.jpg)
Let’s work on a few examples based on the given mapping
C 12H 1N 14O 16
(NH4)2CO3
(15 + CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
60
![Page 17: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/17.jpg)
Let’s work on a few examples based on the given mapping
C 12H 1N 14O 16
(NH4)2CO3
(15 + 60)4
CH4OH
Mr = 96
Mr = 33
![Page 18: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/18.jpg)
Let’s work on a few examples based on the given mapping
Problem Description
C 12H 1N 14O 16
(NH4)2CO3
((CH3)CH4C3H8)4
CH4OH
Mr = 96
Mr = 33
Mr = 300
![Page 19: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/19.jpg)
?What data structure should I use for:
MassMapping
HashMap FormulaProcessing
Stack
![Page 20: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/20.jpg)
HashMap
https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
HashMap<K, V> hashMapName = new HashMap<K, V>();
HashMap<Character, Integer> massMapping = new HashMap<Character, Integer>();
massMapping.put(‘C’, 12);massMapping.put(‘H’, 1);massMapping.put(‘N’, 14);
massMapping.get(‘C’);
massMapping.get(‘H’);
massMapping.get(‘N’);
Setting Keys and Values Retrieving Values
Returns 12
Returns 1
Returns 14
![Page 21: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/21.jpg)
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The ElementsOR
![Page 22: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/22.jpg)
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The Elements
We will use a stack of integers
CH4
Stack
![Page 23: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/23.jpg)
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The Elements
We will use a stack of integers
CH4
Stack
12
![Page 24: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/24.jpg)
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The Elements
We will use a stack of integers
CH4
Stack
12
1
![Page 25: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/25.jpg)
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The Elements
We will use a stack of integers
CH4
Stack
12
14
![Page 26: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/26.jpg)
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Stack
What should my stack contain?
Mass of each Atom The Elements
We will use a stack of integers
CH4
Stack
12
14
Total = 12 + 4 = 16
![Page 27: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/27.jpg)
How do I store the formula?
?As a character array
String nextLine = sc.nextLine();char[] formula = nextLine.toCharArray();
How do I process the formula?
Loop through all characters
processInput(formula);
![Page 28: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/28.jpg)
public void run() {Scanner sc = new Scanner(System.in);
int n = sc.nextInt();sc.nextLine();
initializeMassMapping(sc, n); //implement it
String nextLine = sc.nextLine();char[] formula = nextLine.toCharArray();
processInput(formula);
int total = accumulate(); //will be defined later
System.out.println(total);
sc.close();}
![Page 29: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/29.jpg)
private void processInput(char[] formula) { for (char c : formula) {
//what should I do??? }}
Next character can be one of these:
(
)
X
n
Open Bracket
Close Bracket
Atom Name
Number of atoms (or molecules)
What you should do if you encounter it:
Start New “Session”
Acummulate Current “Session”
Push Its Mass
Multiply Top of The Stack by n
![Page 30: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/30.jpg)
private void processInput(char[] formula) { for (char c : formula) {
if (c == '(') { //open bracket results.push(-1); //start a new “session”} else if (c == ')') { //close bracket int sum = accumulate(); //acummulate is a “helper” //then push the sum into the stack} else if (Character.isDigit(c)) {//it’s a number //multiply the top of the stack. How? } else { //then c must be a ??? //push the corresponding atom’s mass}
}}
![Page 31: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/31.jpg)
private int accumulate() {
int result = 0; int top = results.pop();
while (/* top is not -1 and stack is not empty */) {//add current top to result//update top to be next element in stack
}
return result;}
![Page 32: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/32.jpg)
Visualization
CH4
Stack
-1
![Page 33: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/33.jpg)
Visualization
CH4
Stack
-1
12
Push The Mass of ‘C’ Into The Stack
![Page 34: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/34.jpg)
Visualization
CH4
Stack
-1
12
Push The Mass of ‘H’ Into The Stack
1
![Page 35: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/35.jpg)
Visualization
CH4
Stack
-1
12
Multiply top of stack by 4 1
![Page 36: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/36.jpg)
Visualization
CH4
Stack
-1
12
Multiply top of stack by 4 4
![Page 37: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/37.jpg)
Visualization
CH4
Stack
-1
12
Accumulate all values 4
4 + 12 = 16
![Page 38: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/38.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
![Page 39: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/39.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
Push The Mass of ‘N’ Into The Stack
14
![Page 40: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/40.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
Start New Session
14
-1
![Page 41: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/41.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Push The Mass of ‘C’
Into The Stack
12
![Page 42: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/42.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Push The Mass of ‘H’
Into The Stack
12
1
![Page 43: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/43.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Multiply top of stack by 2
12
1
![Page 44: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/44.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Multiply top of stack by 2
12
2
![Page 45: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/45.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Start New Session
12
2
-1
![Page 46: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/46.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Push The Mass of ‘C’
Into The Stack
12
2
-1
12
![Page 47: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/47.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Push The Mass of ‘H’
Into The Stack
12
2
-1
12
1
![Page 48: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/48.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Multiply Top Of Stack by 3
12
2
-1
12
1
![Page 49: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/49.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Multiply Top Of Stack by 3
12
2
-1
12
3
![Page 50: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/50.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Accumulate
12
2
-1
12
3
3 + 12 = 15
![Page 51: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/51.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Push Result
12
2
15
3 + 12 = 15
![Page 52: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/52.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Multiply Top of Stack by 2
12
2
15
![Page 53: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/53.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
-1Accumulate
12
2
30
30 + 2 + 12 = 44
![Page 54: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/54.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
Push Result44
30 + 2 + 12 = 44
![Page 55: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/55.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
Multiply Top of Stack by 344
![Page 56: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/56.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
Multiply Top of Stack by 3132
![Page 57: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/57.jpg)
Visualization
N(CH2(CH3)2)3
Stack
-1
14
Accumulate all values132
132 + 14 = 146
![Page 58: Chemistry CS1020. Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.](https://reader035.fdocuments.us/reader035/viewer/2022062409/5697c01f1a28abf838cd1a2d/html5/thumbnails/58.jpg)
Summary
• Start a new session by pushing “-1” (or any other invalid mass values that you desire).
• Push the mass of each atom when you encounter them.
• Multiply top of stack each time you encounter a number.
• Accumulate all values (until stack is empty or you pop a “session start” value).
• When done iterating, some all values in the stack (without counting the invalid value, of course).