Upcoming SlideShare
×

# Lecture 1 computing and algorithms

1,200 views
895 views

Published on

Published in: Technology
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

Views
Total views
1,200
On SlideShare
0
From Embeds
0
Number of Embeds
49
Actions
Shares
0
19
0
Likes
3
Embeds 0
No embeds

No notes for slide

### Lecture 1 computing and algorithms

1. 1. 1 of 39Module 1 : Computing and AlgorithmsIntroduction to             Computational ThinkingModule 1 :                                      Computing and AlgorithmsAsst Prof Chi‐Wing FU, PhilipOffice: N4‐02c‐104email: cwfu[at]ntu.edu.sg
2. 2. 2 of 39Module 1 : Computing and AlgorithmsTopics• What is Computational Thinking?• What is an Algorithm• How to Express an Algorithm?• Flowcharts• Nassi-Schneiderman Diagrams• Pseudo-code
3. 3. 3 of 39Module 1 : Computing and AlgorithmsWhat is Computational Thinking?• It is a problem solving process that involves• Analysis and modeling of data• Understanding of how computers work• Logic and procedure design• Formulating a solution that we can use acomputer to work it out• Automation• Efficiency• …
4. 4. 4 of 39Module 1 : Computing and AlgorithmsWhat is a problem?• First… you may wonder…What is a problem?It is a question proposed for a solution.Let’s start with a simple example:Find the last 4 digits in factorial n or n!
5. 5. 5 of 39Module 1 : Computing and AlgorithmsWhat is a problem?Before that… What is a factorial?0! = 11! = 12! = 2 x 1! = 2 x 13! = 3 x 2! = 3 x 2 x 1 = 6n! = n x (n-1)! = … = n x (n-1) x (n-2) x … x 2 x 1
6. 6. 6 of 39Module 1 : Computing and AlgorithmsWhat is a problem?• So… we have to find out the last 4 digits in n!Input: nOutput: 4 digits: could be 0000, 0001, …, 9999Computational thinking is the problem-solvingprocess to devise a method to compute the solution
7. 7. 7 of 39Module 1 : Computing and AlgorithmsLet’s do Problem Solving!!!• Now, let me give you a minute to think abouthow to solve this problem?…….Don’t look at the next slides…Think about it yourself first!!!!!!!
8. 8. 8 of 39Module 1 : Computing and AlgorithmsMethod 1Method 1: (Straight-forward)Step 1: First, ask the user to input or tell you nStep 2: We can compute n! by iteratively doingthe multiplication (like using a calculator)Accumulator initialized to nAccumulator ← Accumulator x (n-1)Accumulator ← Accumulator x (n-2)……
9. 9. 9 of 39Module 1 : Computing and AlgorithmsMethod 1……Accumulator ← Accumulator x 3Accumulator ← Accumulator x 2Accumulator ← Accumulator x 1Step 3: Lastly, output last 4 digits in Accumulator
10. 10. 10 of 39Module 1 : Computing and AlgorithmsAny issue?• How if we use a calculator tocompute it!!!If n is large, say 50, the accumulator willsoon be too large, and not representable anddisplayable with the finite precision on yourcalculator: 50! = 3.0414…x1064.• Any better or more efficient method?Note: we only need to know the last 4 digits…Do we really need to know the whole value of n!
11. 11. 11 of 39Module 1 : Computing and AlgorithmsMethod 2Home exercise: your problem solving time…• Hints:• Since we only need the last 4 digits as output…• The computation ONLY involves a series ofmultiplication…• Whatever we multiply two numbers, we onlyneed to keep …… (think about this …)• And any trick to speed up?• It can be more efficient and independent of thenumber representation precision!!!
12. 12. 12 of 39Module 1 : Computing and AlgorithmsComputational Thinking!!Computational thinking is a mental processaiming at solving a problem by formulating thesolution into a procedure/method that thecomputer (like a calculator) can work onThis is the focus on this course!!!!!!!Note!!! Generally, problem-solving is independentof the choice of programming language!!!
13. 13. 13 of 39Module 1 : Computing and AlgorithmsComputational Thinking!!And…Computers and programming languages arebasically tools (like calculators) for you toformulate your solution (algorithm/procedure)into a program that a computer can runNote: What is computer, hardware, software, …you should learn these in course “Introduction toComputing Systems” (which runs in parallel)
14. 14. 14 of 39Module 1 : Computing and Algorithms• What is Computational Thinking?• What is an Algorithm?• How to Express an Algorithm?• Flowcharts• Nassi-Schneiderman Diagrams• Pseudo-codeTopics
15. 15. 15 of 39Module 1 : Computing and AlgorithmsWhat is an Algorithm?• Just like the example you saw…When you formulate a method/procedure forsolving a problem, it is something computableand it is also called an algorithmAlgorithm VS Program• An algorithm is a description of the procedureon how one can follow to solve the problem• A program is an implementation of an algorithmin a particular language for computers to run on
16. 16. 16 of 39Module 1 : Computing and AlgorithmsWhat is an Algorithm?• So… Algorithm and computational thinking areboth independent of the programminglanguage we choose to implement the programProblem Algorithm ProgramRun onComputationalThinkingProgramming
17. 17. 17 of 39Module 1 : Computing and AlgorithmsWhy Algorithm?• Hence, we can analyze the problem and derivethe solution independent of programming• Furthermore, we can also examine how easilyor difficult a language allows us to realize thealgorithm• And how different computers impact therealization of an algorithm, e.g., efficiency(in a course that you will take in the future:“CZ2001 Algorithm” in 2nd year)
18. 18. 18 of 39Module 1 : Computing and AlgorithmsAspects of an Algorithm• How detail should an algorithm be:• Provide enough detail to be implementable• Can be tricky to define completely:relies on “common sense” and the audience
19. 19. 19 of 39Module 1 : Computing and Algorithms• Example: Making scrambled eggs1. Beating the eggs for 20 to 35 seconds in a bowl2. Heating a frying pan over a medium-low heat3. Melt some butter in the frying pan4. Cook eggs on the pan and stir eggs while cooking5. Add other ingredients6. Serve the scrambled eggsAspects of an Algorithm
20. 20. 20 of 39Module 1 : Computing and Algorithms• Example: Making scrambled eggsMore detail? it depends, e.g., audienceSource: http://whatscookingamerica.net/Eggs/ScrambledOmelette.htm
21. 21. 21 of 39Module 1 : Computing and AlgorithmsTopics• What is Computational Thinking?• What is an Algorithm?• How to Express an algorithm?• Flowcharts• Nassi-Schneiderman diagrams• Pseudo-code
22. 22. 22 of 39Module 1 : Computing and AlgorithmsHow to express an Algorithm?• Algorithms is basically Sequential(step after step)• But may include• Branching (making a selection)• Looping (repeating certain operations)Step 1 Step 2 Step 3 ……Sequence:
23. 23. 23 of 39Module 1 : Computing and AlgorithmsAlgorithm can have Branching• Branching (make a selection)• E.g., if there are extra ingredients such as herbsand cheese, then add them to the eggs beforeserving the scrambled eggs; Else we skip this step.Example: Making scrambled eggs1. Beating the eggs for 20 to 35 seconds in a bowl2. Heating a frying pan over a medium-low heat3. Melt some butter in the frying pan4. Cook eggs on the pan and stir eggs while cooking5. Add other ingredients6. Serve the scrambled eggs
24. 24. 24 of 39Module 1 : Computing and AlgorithmsAlgorithm can have Looping• Looping (certain operations needed to berepeated again and again)• E.g., while the eggs do not look like what youdesire, keep gently stirring them in the panExample: Making scrambled eggs1. Beating the eggs for 20 to 35 seconds in a bowl2. Heating a frying pan over a medium-low heat3. Melt some butter in the frying pan4. Cook eggs on the pan and stir eggs while cooking5. Add other ingredients6. Serve the scrambled eggs
25. 25. 25 of 39Module 1 : Computing and AlgorithmsHow to express an Algorithm?• Three general (and very common)techniques to express algorithms:• Flowcharts• Nassi-Schneiderman diagrams• Pseudo-code
26. 26. 26 of 39Module 1 : Computing and AlgorithmsGeneral Notes• No strict rules• Informal language - mix of English andkeywords• Common keywords: IF, ELSE, WHILE, etc.• Other keywords: READ, PRINT, SET,INITILAIZE, COMPUTE, ADD, SUBTRACT, etc.• Usually start an operation sentence with a verb(description should be concise and precise)
27. 27. 27 of 39Module 1 : Computing and Algorithms#1. Flowcharts• Represent an algorithm by a diagram foreffective visualizationSym b o l N a m eProcessDecisionInput / OutputTerminalFlowlines
28. 28. 28 of 39Module 1 : Computing and Algorithms#1. FlowchartsBeating the eggs in a bowlHeating a frying panMelt butter and put eggs on panIf eggs ok? Stir eggsNYAny ingredient? Add ingredientsYNendstartDecision blocks(make choices)Can do repetitionCan do selectionServe Scrambled EggsOutput!
29. 29. 29 of 39Module 1 : Computing and Algorithms#2. Nassi-Schneiderman diagrams• Similar to a flowchart but it is arrow-freeand so more space-friendlyBeating the eggs in a bowlHeating a frying panMelt butter and put eggs on pan#1: Sequential operations: Just stack them up
30. 30. 30 of 39Module 1 : Computing and Algorithms#2. Nassi-Schneiderman diagrams#2: Selection operations: Make BranchesAny ingredient?N YServe Scrambled EggsAdd ingredientHere is empty,meaning do nothingbut we may also putoperations here#3: Looping operations: RepetitionEggs not ok?Stir eggsWhile condition is true,keep running theoperations insideJoin again
31. 31. 31 of 39Module 1 : Computing and Algorithms#2. Nassi-Schneiderman diagramsBeating the eggs in a bowlHeating a frying panMelt butter and put eggs on panPutting them together:Any ingredient?N YServe Scrambled EggsAdd ingredientEgg not ok?Stir eggsCan do repetitionCan do selection
32. 32. 32 of 39Module 1 : Computing and Algorithms#2. Nassi-Schneiderman diagrams• One more example:Computing factorialCan do selection(true or false)Here I takes a valueof 3 up to N in differentiteration of this loop, whichIs repeated with different IAnd accumulate to NFACT
33. 33. 33 of 39Module 1 : Computing and Algorithms#3. Pseudo-code• How to pronounce?sy-ooooooooooooo-doh! code• IDEA: directly use informal English todescribe an algorithm step by step withone step per line
34. 34. 34 of 39Module 1 : Computing and Algorithms#3. Pseudo-codeExample #1: Making Scrambled EggsBEAT the eggs for 20 to 35 seconds in a bowlHEAT a frying pan over a medium-low heatMELT some butter in the frying panPUT eggs on panWHILE eggs not okaySTIR eggs while cookingEND WHILEIF any ingredientsAdd other ingredientsEND IFSERVE the scrambled eggs
35. 35. 35 of 39Module 1 : Computing and Algorithms#3. Pseudo-codeExample #2: factorialREAD N from user inputIF N <= 1nfact = 1ELSEnfact = 2REPEAT I = 3 to Nnfact = nfact x IEND IFOUTPUT nfact
36. 36. 36 of 39Module 1 : Computing and Algorithms#3. Pseudo-codeGuidelines:• Write only one statement per line• Capitalize the keywords• Indent to show hierarchy• End multi-line structures• Keep statements programming-languageindependent
37. 37. 37 of 39Module 1 : Computing and AlgorithmsMore detail onthese techniqueslater in this courseExpressing an Algorithm• Important Note:• Must be unambiguous• Every step must be clear and precise• Specify the order of steps precisely[Sequence]• Consider all possible decision points[Branching and Looping]• Must terminate(No matter which representation you use)
38. 38. 38 of 39Module 1 : Computing and AlgorithmsTake Home Messages• Computational thinking is a mental process, aiming atsolving a problem by formulating the solution into aprocedure/method that the computer can work on• Problem-solving is independent of the choice of theprogramming language• An algorithm is a description of a procedure on how onecan follow to solve the problem, whereas a program is animplementation of an algorithm in a particular language torun on a computer• Algorithms may not be sequential, they may includebranching and looping; Three basic techniques to expressalgorithms: flowcharts, Nassi-Schneiderman diagrams,and pseudo-code.
39. 39. 39 of 39Module 1 : Computing and AlgorithmsReading Assignment• TextbookChapter 0: The Study of Computer Science0.1 to 0.4Note: Though some material in textbook is notdirectly related to the lecture material, you canlearn more from them.• Exercise:Write down the algorithm for the problem on P.4.