Arrays (Chapter 5)? - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays (Chapter 5)?

Description:

... can use a table-driven approach to looking up the atomic weights of the elements. ... First letter: make sure we don't put gibberish in molecular weight ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 29
Provided by: csN9
Learn more at: https://www.cs.nmsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Arrays (Chapter 5)?


1
Arrays (Chapter 5)?
  • Definition
  • Applications
  • One-Dimensional
  • Declaration
  • Initialization
  • Use
  • Multidimensional

2
Declaration
  • define SIZE 10
  • int main(void)?
  • float sampleSIZE

3
Initialization
  • define SIZE 10
  • int main(void)?
  • float sampleSIZE 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
ParsingO2C3NH7\n
  • Find new atoms (letters)?

14
ParsingO2C3NH7\n
  • Find new atoms (letters)?
  • Look up atomic weight of atom (12.011)?

15
ParsingO2C3NH7\n
  • Find new atoms (letters)?
  • Look up atomic weight of atom (12.011)?
  • Count how many we've got (numbers)?

16
ParsingO2C3NH7\n
  • Find new atoms (letters)?
  • Look up atomic weight of atom (12.011)?
  • Count how many we've got (numbers)?
  • 3

17
ParsingO2C3NH7\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
ParsingO2C3NH7\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
  • 312.011 36.033

19
Look Up Atomic Weight
20
Look Up Atomic Weight
21
Look Up Atomic Weight
22
Counting
  • numatoms numatoms 10.0 newdigit

23
Counting
  • numatoms numatoms 10.0 newdigit
  • 123

24
Counting
  • numatoms numatoms 10.0 newdigit
  • 123
  • 010 1 1

25
Counting
  • numatoms numatoms 10.0 newdigit
  • 123
  • 110 2 12

26
Counting
  • numatoms numatoms 10.0 newdigit
  • 123
  • 1210 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
Write a Comment
User Comments (0)
About PowerShow.com