L3_-_Functions_4 - Lecture Overview Top Down Approach to...

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

View Full Document Right Arrow Icon
S1010E Programming Methodology CS1010E Programming Methodology Functions Get this job done for me! Lecture Overview Top Down Approach to Problem Solving Functions asic idea basic idea execution flow rototype and declaration prototype and declaration scoping rules C Standard Library ath Library Math Library [ CS1010E AY1112S1 Lecture 3 ] 2 Top Down Approach When solving large problems, it is hard to come up with a detailed solution right away pg y A common approach is: pecify the ajor steps f the algorithm 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 ] 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) ubtract 4/3 from I I I /3) 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 1

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

View Full DocumentRight Arrow Icon
Example 1(cont’d) even terms are negative ......... 9 4 7 4 5 4 3 4 1 4 dd terms are Denominator increases by 2 every term A more general algorithm: odd e s a e positive 1. Initialize PI 4 2. Denom 3 . Term 3. nTerm 2 4. while nTerm is <= 1000 i. Calculate new term ii. Update PI T T 1 5 iii. nTerm nTerm + 1 5. Show PI PI Algorithm a. Term 4 / Denom 1. Initialize PI 4 2. Denom 3 Term 3. nTerm 2 4. while nTerm is <= 1000 i. Calculate new term ii. Update PI Term even 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 own 6 down [ CS1010E AY1112S1 Lecture 3 ] Example 2: Anagrams Two phrases are anagrams if one is formed by rearranging the letters of the other, yg g , 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 o do you de e e o o ds a d are anagrams? 7 Example: Anagram Algorithm One possible solution using sorting 1. Read two words w1 and w2 from user Steps to calculate the ngth 2. len1 = length of w1 , len2 = length of w2 3. if len1 not equal len2 rint " ot Anagrams " and stop length teps to sort a word print Not Anagrams and stop 4. Sort w1 and w2 by alphabetical order 5. From position 0 to len1 -1 Steps to sort a word into alphabetical order If w1 position not equal w2 position print " Not Anagrams " and stop 8 6. print “ They are Anagrams " and Stop [ CS1010E AY1112S1 Lecture 3 ]
Background image of page 2
Guidelines on Top Down Approach As you learn more about C, you can see clearer whether an algorithm step is gp expressible in C with simple statement(s)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/13/2011 for the course ELECTRICAL CS1010E taught by Professor Henrytan during the Spring '11 term at National University of Singapore.

Page1 / 11

L3_-_Functions_4 - Lecture Overview Top Down Approach to...

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

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