Arrays Chapter 5 Definition Applications OneDimensional Declaration Initialization

![Declaration #define SIZE 10 int main(void) { float sample[SIZE]; Declaration #define SIZE 10 int main(void) { float sample[SIZE];](https://slidetodoc.com/presentation_image_h2/10ec1839521837e7e8832da5524ea7b9/image-2.jpg)
![Initialization #define SIZE 10 int main(void) { float sample[SIZE] = {5. 0, 1. 2, Initialization #define SIZE 10 int main(void) { float sample[SIZE] = {5. 0, 1. 2,](https://slidetodoc.com/presentation_image_h2/10ec1839521837e7e8832da5524ea7b9/image-3.jpg)
![Gotchas • First element of array is [0] • Last element of array is Gotchas • First element of array is [0] • Last element of array is](https://slidetodoc.com/presentation_image_h2/10ec1839521837e7e8832da5524ea7b9/image-4.jpg)
























- Slides: 28

Arrays (Chapter 5) • Definition • Applications • One-Dimensional – Declaration – Initialization – Use • Multidimensional
![Declaration define SIZE 10 int mainvoid float sampleSIZE Declaration #define SIZE 10 int main(void) { float sample[SIZE];](https://slidetodoc.com/presentation_image_h2/10ec1839521837e7e8832da5524ea7b9/image-2.jpg)
Declaration #define SIZE 10 int main(void) { float sample[SIZE];
![Initialization define SIZE 10 int mainvoid float sampleSIZE 5 0 1 2 Initialization #define SIZE 10 int main(void) { float sample[SIZE] = {5. 0, 1. 2,](https://slidetodoc.com/presentation_image_h2/10ec1839521837e7e8832da5524ea7b9/image-3.jpg)
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};
![Gotchas First element of array is 0 Last element of array is Gotchas • First element of array is [0] • Last element of array is](https://slidetodoc.com/presentation_image_h2/10ec1839521837e7e8832da5524ea7b9/image-4.jpg)
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-tofind bugs!)

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).

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

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. • O 2 C 3 NH 7n

Semantics • O 2 C 3 NH 7n – Two Oxygen (2. 0 * 15. 9994 = 31. 9988)

Semantics • O 2 C 3 NH 7n – Two Oxygen (2. 0 * 15. 9994 = 31. 9988) – Three Carbon (3. 0 * 12. 011 = 36. 033)

Semantics • O 2 C 3 NH 7n – Two Oxygen (2. 0 * 15. 9994 = 31. 9988) – Three Carbon (3. 0 * 12. 011 = 36. 033) – One Nitrogen (14. 00674)

Semantics • O 2 C 3 NH 7n – 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)

Semantics • O 2 C 3 NH 7n – 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

Parsing O 2 C 3 NH 7n • Find new atoms (letters)

Parsing O 2 C 3 NH 7n • Find new atoms (letters) – Look up atomic weight of atom (12. 011)

Parsing O 2 C 3 NH 7n • Find new atoms (letters) – Look up atomic weight of atom (12. 011) • Count how many we've got (numbers)

Parsing O 2 C 3 NH 7n • Find new atoms (letters) – Look up atomic weight of atom (12. 011) • Count how many we've got (numbers) – 3

Parsing O 2 C 3 NH 7n • 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

Parsing O 2 C 3 NH 7n • 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

Look Up Atomic Weight

Look Up Atomic Weight

Look Up Atomic Weight

Counting numatoms = numatoms * 10. 0 + newdigit;

Counting numatoms = numatoms * 10. 0 + newdigit; 123

Counting numatoms = numatoms * 10. 0 + newdigit; 123 0*10 + 1 = 1

Counting numatoms = numatoms * 10. 0 + newdigit; 123 1*10 + 2 = 12

Counting numatoms = numatoms * 10. 0 + newdigit; 123 12*10 + 3 = 123

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'

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