L3 - Functions - CS1010E Programming Methodology Functions...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
CS1010E Programming Methodology Functions Get this job done for me!
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture Overview Top Down Approach to Problem Solving Functions basic idea execution flow prototype and declaration scoping rules C Standard Library Math Library [ CS1010E AY1112S1 Lecture 3 ] 2
Background image of page 2
Top Down Approach When solving large problems, it is hard to come up with a detailed solution right away A common approach is: Specify the major steps of the algorithm Review each major step and break it down into smaller steps (step-wise refinement) Repeat this process if needed until the steps are well defined This is known as Top Down Approach to problem solving 3 [ CS1010E AY1112S1 Lecture 3 ]
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example 1 One way to calculate the PI π constant: ......... 9 4 7 4 5 4 3 4 1 4 + + = π When you compute the term using the sequence above: 1. PI starts as 4/1 ( PI 4/1) 2. Subtract 4/3 from PI ( PI PI – 4/3) 3. Add 4/5 to PI ( PI PI + 4/5) 4. Subtract 4/7 from PI ( PI PI – 4/7) 5. … 4
Background image of page 4
Example 1(cont’d) A more general algorithm: ......... 9 4 7 4 5 4 3 4 1 4 + + = π even terms are negative odd terms are positive Denominator increases by 2 every term 5 1. Initialize PI 4 2. Denom 3 3. nTerm 2 4. while nTerm is <= 1000 i. Calculate new term ii. Update PI iii. nTerm nTerm + 1 5. Show PI
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PI Algorithm 6 a. Term 4 / Denom 1. Initialize PI 4 2. Denom 3 3. nTerm 2 4. while nTerm is <= 1000 i. Calculate new term ii. Update PI iii. nTerm nTerm + 1 5. Show PI a. If nTerm is even PI PI Term b. If nTerm is odd PI PI + Term First draft with only the major steps refines Major steps are further broken down [ CS1010E AY1112S1 Lecture 3 ]
Background image of page 6
Example 2: Anagrams Two phrases are anagrams if one is formed by rearranging the letters of the other, disregarding spaces and punctuation marks. Debit card ” = “ Bad credit The eyes ” = “ They see Astronomer ” = “ Moon starer A telescope ” = “ To see place A decimal point ” = “ I’m a dot in place How do you determine if two words X and Y are anagrams? 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example: Anagram Algorithm One possible solution using sorting 8 1. Read two words w1 and w2 from user 2. len1 = length of w1 , len2 = length of w2 3. if len1 not equal len2 print " Not Anagrams " and stop 4. Sort w1 and w2 by alphabetical order 5. From position 0 to len1 - 1 If w1 position not equal w2 position print " Not Anagrams " and stop 6. print “ They are Anagrams " and Stop Steps to calculate the length Steps to sort a word into alphabetical order [ CS1010E AY1112S1 Lecture 3 ]
Background image of page 8
Guidelines on Top Down Approach As you learn more about C, you can see clearer whether an algorithm step is expressible in C with simple statement(s) If it is not possible, break it down further to simpler sub-problems To learn a programming language is to know: The expressive power
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 42

L3 - Functions - CS1010E Programming Methodology Functions...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online