Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays (Chapter 5)‏ Definition Applications One-Dimensional –Declaration –Initialization –Use Multidimensional.

Similar presentations


Presentation on theme: "Arrays (Chapter 5)‏ Definition Applications One-Dimensional –Declaration –Initialization –Use Multidimensional."— Presentation transcript:

1 Arrays (Chapter 5)‏ Definition Applications One-Dimensional –Declaration –Initialization –Use Multidimensional

2 Declaration #define SIZE 10 int main(void)‏ { float sample[SIZE];

3 Initialization #define SIZE 10 int main(void)‏ { float sample[SIZE] = {5.0, 1.2, -4.6, -7.5, 8.9, -1.0, 12.7, 0.0, 6.6, 4.2};

4 Gotchas First element of array is [0] Last element of array is [n-1] (major source of bugs!)‏ No bounds check (MAJOR source of impossible-to- find bugs!)‏

5 Amino Acids I like this problem, but I don't especially like the way the book does it. The book isn't careful enough about range errors. We can use a table-driven approach to looking up the atomic weights of the elements. This takes more space (bad), less time (good) than the way the book looks up the weights. Also less opportunity for bugs (very, very good).

6 Background Amino Acid: Organic molecule composed of carbon, hydrogen, nitrogen, oxygen, sulfur. Building blocks of proteins. Amino acids are what DNA codes for. Weight is sum of weights of component atoms

7 Syntax One amino acid formula on a single line Amino acid formula consists of sequence of atom counts Atom count is a letter specifying the atom, maybe followed by a number specifying how many of them. O2C3NH7\n

8 Semantics O2C3NH7\n –Two Oxygen (2.0 * 15.9994 = 31.9988)‏

9 Semantics O2C3NH7\n –Two Oxygen (2.0 * 15.9994 = 31.9988)‏ –Three Carbon (3.0 * 12.011 = 36.033)‏

10 Semantics O2C3NH7\n –Two Oxygen (2.0 * 15.9994 = 31.9988)‏ –Three Carbon (3.0 * 12.011 = 36.033)‏ –One Nitrogen (14.00674)‏

11 Semantics O2C3NH7\n –Two Oxygen (2.0 * 15.9994 = 31.9988)‏ –Three Carbon (3.0 * 12.011 = 36.033)‏ –One Nitrogen (14.00674)‏ –Seven Hydrogen (7.0 * 1.00794 = 7.05558)‏

12 Semantics O2C3NH7\n –Two Oxygen (2.0 * 15.9994 = 31.9988)‏ –Three Carbon (3.0 * 12.011 = 36.033)‏ –One Nitrogen (14.00674)‏ –Seven Hydrogen (7.0 * 1.00794 = 7.05558)‏ Total: 31.9988 + 36.033 + 14.00674 + 7.05558 = 89.09412

13 Parsing O2C3NH7\n Find new atoms (letters)‏

14 Parsing O2C3NH7\n Find new atoms (letters)‏ –Look up atomic weight of atom (12.011)‏

15 Parsing O2C3NH7\n Find new atoms (letters)‏ –Look up atomic weight of atom (12.011)‏ Count how many we've got (numbers)‏

16 Parsing O2C3NH7\n Find new atoms (letters)‏ –Look up atomic weight of atom (12.011)‏ Count how many we've got (numbers)‏ –3

17 Parsing O2C3NH7\n Find new atoms (letters)‏ –Look up atomic weight of atom (12.011)‏ Count how many we've got (numbers)‏ –3 When we reach next atom, add previous to atomic weight of molecule

18 Parsing O2C3NH7\n Find new atoms (letters)‏ –Look up atomic weight of atom (12.011)‏ Count how many we've got (numbers)‏ –3 When we reach next atom, add previous to atomic weight of molecule –3*12.011 = 36.033

19 Look Up Atomic Weight

20

21

22 Counting numatoms = numatoms * 10.0 + newdigit;

23 Counting numatoms = numatoms * 10.0 + newdigit; 123

24 Counting numatoms = numatoms * 10.0 + newdigit; 123 0*10 + 1 = 1

25 Counting numatoms = numatoms * 10.0 + newdigit; 123 1*10 + 2 = 12

26 Counting numatoms = numatoms * 10.0 + newdigit; 123 12*10 + 3 = 123

27 Special Cases First letter: make sure we don't put gibberish in molecular weight End of string: make sure we account for last atom If user didn't enter number for an atom, it means '1'

28 Writing the code Write initialization, input, core loop, output Handle special cases Error checking


Download ppt "Arrays (Chapter 5)‏ Definition Applications One-Dimensional –Declaration –Initialization –Use Multidimensional."

Similar presentations


Ads by Google