lecture 1 computing and algorithms

39
Introduction to Computational Thinking Module 1 : Computing and Algorithms Asst Prof ChiWing FU, Philip Office: N402c104 email: cwfu[at]ntu.edu.sg

Upload: alvin567

Post on 12-May-2015

491 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Lecture 1  computing and algorithms

1 of 39Module 1 : Computing and Algorithms

Introduction to             Computational Thinking

Module 1 :                                      Computing and Algorithms

Asst Prof Chi‐Wing FU, PhilipOffice: N4‐02c‐104

email: cwfu[at]ntu.edu.sg

Page 2: Lecture 1  computing and algorithms

2 of 39Module 1 : Computing and Algorithms

Topics

• What is Computational Thinking?• What is an Algorithm• How to Express an Algorithm?

• Flowcharts• Nassi-Schneiderman Diagrams• Pseudo-code

Page 3: Lecture 1  computing and algorithms

3 of 39Module 1 : Computing and Algorithms

What 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 a

computer to work it out• Automation• Efficiency• …

Page 4: Lecture 1  computing and algorithms

4 of 39Module 1 : Computing and Algorithms

What 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!

Page 5: Lecture 1  computing and algorithms

5 of 39Module 1 : Computing and Algorithms

What 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

Page 6: Lecture 1  computing and algorithms

6 of 39Module 1 : Computing and Algorithms

What is a problem?

• So… we have to find out the last 4 digits in n!

Input: nOutput: 4 digits: could be 0000, 0001, …, 9999

Computational thinking is the problem-solving process to devise a method to compute the solution

Page 7: Lecture 1  computing and algorithms

7 of 39Module 1 : Computing and Algorithms

Let’s do Problem Solving!!!

• Now, let me give you a minute to think about how to solve this problem?

…….

Don’t look at the next slides…

Think about it yourself first!!!!!!!

Page 8: Lecture 1  computing and algorithms

8 of 39Module 1 : Computing and Algorithms

Method 1

Method 1: (Straight-forward)

Step 1: First, ask the user to input or tell you nStep 2: We can compute n! by iteratively doing

the multiplication (like using a calculator)

Accumulator initialized to nAccumulator ← Accumulator x (n-1)Accumulator ← Accumulator x (n-2)……

Page 9: Lecture 1  computing and algorithms

9 of 39Module 1 : Computing and Algorithms

Method 1

……Accumulator ← Accumulator x 3Accumulator ← Accumulator x 2Accumulator ← Accumulator x 1

Step 3: Lastly, output last 4 digits in Accumulator

Page 10: Lecture 1  computing and algorithms

10 of 39Module 1 : Computing and Algorithms

Any issue?

• How if we use a calculator to compute it!!!If n is large, say 50, the accumulator will soon be too large, and not representable and displayable with the finite precision on your calculator: 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!

Page 11: Lecture 1  computing and algorithms

11 of 39Module 1 : Computing and Algorithms

Method 2

Home exercise: your problem solving time…

• Hints:• Since we only need the last 4 digits as output…• The computation ONLY involves a series of

multiplication…• Whatever we multiply two numbers, we only

need to keep …… (think about this …)• And any trick to speed up?

• It can be more efficient and independent of the number representation precision!!!

Page 12: Lecture 1  computing and algorithms

12 of 39Module 1 : Computing and Algorithms

Computational Thinking!!

Computational thinking is a mental process aiming at solving a problem by formulating the solution into a procedure/method that the computer (like a calculator) can work on

This is the focus on this course!!!!!!!Note!!! Generally, problem-solving is independent of the choice of programming language!!!

Page 13: Lecture 1  computing and algorithms

13 of 39Module 1 : Computing and Algorithms

Computational Thinking!!

And…Computers and programming languages are basically tools (like calculators) for you to formulate your solution (algorithm/procedure) into a program that a computer can run

Note: What is computer, hardware, software, …you should learn these in course “Introduction to Computing Systems” (which runs in parallel)

Page 14: Lecture 1  computing and algorithms

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-code

Topics

Page 15: Lecture 1  computing and algorithms

15 of 39Module 1 : Computing and Algorithms

What is an Algorithm?

• Just like the example you saw…When you formulate a method/procedure for solving a problem, it is something computableand it is also called an algorithm

Algorithm VS Program• An algorithm is a description of the procedure

on how one can follow to solve the problem• A program is an implementation of an algorithm

in a particular language for computers to run on

Page 16: Lecture 1  computing and algorithms

16 of 39Module 1 : Computing and Algorithms

What is an Algorithm?

• So… Algorithm and computational thinking are both independent of the programming language we choose to implement the program

Problem Algorithm Program

Run onComputational

ThinkingProgramming

Page 17: Lecture 1  computing and algorithms

17 of 39Module 1 : Computing and Algorithms

Why Algorithm?

• Hence, we can analyze the problem and derive the solution independent of programming

• Furthermore, we can also examine how easily or difficult a language allows us to realize the algorithm

• And how different computers impact the realization of an algorithm, e.g., efficiency (in a course that you will take in the future: “CZ2001 Algorithm” in 2nd year)

Page 18: Lecture 1  computing and algorithms

18 of 39Module 1 : Computing and Algorithms

Aspects 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

Page 19: Lecture 1  computing and algorithms

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 heat 3. Melt some butter in the frying pan4. Cook eggs on the pan and stir eggs while cooking5. Add other ingredients6. Serve the scrambled eggs

Aspects of an Algorithm

Page 20: Lecture 1  computing and algorithms

20 of 39Module 1 : Computing and Algorithms

• Example: Making scrambled eggs

More detail? it depends, e.g., audience

Source: http://whatscookingamerica.net/Eggs/ScrambledOmelette.htm

Page 21: Lecture 1  computing and algorithms

21 of 39Module 1 : Computing and Algorithms

Topics

• What is Computational Thinking?• What is an Algorithm?• How to Express an algorithm?

• Flowcharts• Nassi-Schneiderman diagrams• Pseudo-code

Page 22: Lecture 1  computing and algorithms

22 of 39Module 1 : Computing and Algorithms

How 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:

Page 23: Lecture 1  computing and algorithms

23 of 39Module 1 : Computing and Algorithms

Algorithm can have Branching

•Branching (make a selection)• E.g., if there are extra ingredients such as herbs

and cheese, then add them to the eggs before serving 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 heat 3. Melt some butter in the frying pan4. Cook eggs on the pan and stir eggs while cooking5. Add other ingredients6. Serve the scrambled eggs

Page 24: Lecture 1  computing and algorithms

24 of 39Module 1 : Computing and Algorithms

Algorithm can have Looping

• Looping (certain operations needed to be repeated again and again)• E.g., while the eggs do not look like what you

desire, keep gently stirring them in the pan

Example: Making scrambled eggs1. Beating the eggs for 20 to 35 seconds in a bowl2. Heating a frying pan over a medium-low heat 3. Melt some butter in the frying pan4. Cook eggs on the pan and stir eggs while cooking5. Add other ingredients6. Serve the scrambled eggs

Page 25: Lecture 1  computing and algorithms

25 of 39Module 1 : Computing and Algorithms

How to express an Algorithm?

• Three general (and very common) techniques to express algorithms:• Flowcharts•Nassi-Schneiderman diagrams•Pseudo-code

Page 26: Lecture 1  computing and algorithms

26 of 39Module 1 : Computing and Algorithms

General Notes

• No strict rules• Informal language - mix of English and

keywords• 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)

Page 27: Lecture 1  computing and algorithms

27 of 39Module 1 : Computing and Algorithms

#1. Flowcharts

• Represent an algorithm by a diagram for effective visualization

Symbol Name

Process

Decision

Input / Output

Terminal

Flowlines

Page 28: Lecture 1  computing and algorithms

28 of 39Module 1 : Computing and Algorithms

#1. FlowchartsBeating the eggs in a bowl

Heating a frying pan

Melt butter and put eggs on pan

If eggs ok? Stir eggsN

Y

Any ingredient? Add ingredientsY

N

end

start

Decision blocks(make choices)

Can do repetition

Can do selectionServe Scrambled Eggs

Output!

Page 29: Lecture 1  computing and algorithms

29 of 39Module 1 : Computing and Algorithms

#2. Nassi-Schneiderman diagrams

• Similar to a flowchart but it is arrow-free and so more space-friendly

Beating the eggs in a bowl

Heating a frying pan

Melt butter and put eggs on pan

#1: Sequential operations: Just stack them up

Page 30: Lecture 1  computing and algorithms

30 of 39Module 1 : Computing and Algorithms

#2. Nassi-Schneiderman diagrams

#2: Selection operations: Make Branches

Any ingredient?N Y

Serve Scrambled Eggs

Add ingredient

Here is empty,meaning do nothingbut we may also putoperations here

#3: Looping operations: Repetition

Eggs not ok?

Stir eggsWhile condition is true,keep running theoperations inside

Join again

Page 31: Lecture 1  computing and algorithms

31 of 39Module 1 : Computing and Algorithms

#2. Nassi-Schneiderman diagrams

Beating the eggs in a bowl

Heating a frying pan

Melt butter and put eggs on pan

Putting them together:

Any ingredient?N Y

Serve Scrambled Eggs

Add ingredient

Egg not ok?

Stir eggs

Can do repetition

Can do selection

Page 32: Lecture 1  computing and algorithms

32 of 39Module 1 : Computing and Algorithms

#2. Nassi-Schneiderman diagrams

• One more example:Computing factorial

Can do selection(true or false)

Here I takes a value of 3 up to N in different iteration of this loop, which Is repeated with different I

And accumulate to NFACT

Page 33: Lecture 1  computing and algorithms

33 of 39Module 1 : Computing and Algorithms

#3. Pseudo-code

• How to pronounce?sy-ooooooooooooo-doh! code

• IDEA: directly use informal English to describe an algorithm step by step with one step per line

Page 34: Lecture 1  computing and algorithms

34 of 39Module 1 : Computing and Algorithms

#3. Pseudo-code

Example #1: Making Scrambled EggsBEAT the eggs for 20 to 35 seconds in a bowlHEAT a frying pan over a medium-low heat MELT some butter in the frying panPUT eggs on panWHILE eggs not okay

STIR eggs while cookingEND WHILEIF any ingredients

Add other ingredientsEND IFSERVE the scrambled eggs

Page 35: Lecture 1  computing and algorithms

35 of 39Module 1 : Computing and Algorithms

#3. Pseudo-code

Example #2: factorialREAD N from user inputIF N <= 1

nfact = 1ELSE

nfact = 2REPEAT I = 3 to N

nfact = nfact x IEND IFOUTPUT nfact

Page 36: Lecture 1  computing and algorithms

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-language

independent

Page 37: Lecture 1  computing and algorithms

37 of 39Module 1 : Computing and Algorithms

More detail onthese techniques later in this course

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

Page 38: Lecture 1  computing and algorithms

38 of 39Module 1 : Computing and Algorithms

Take Home Messages• Computational thinking is a mental process, aiming at

solving a problem by formulating the solution into a procedure/method that the computer can work on

• Problem-solving is independent of the choice of the programming language

• An algorithm is a description of a procedure on how one can follow to solve the problem, whereas a program is an implementation of an algorithm in a particular language to run on a computer

• Algorithms may not be sequential, they may include branching and looping; Three basic techniques to express algorithms: flowcharts, Nassi-Schneiderman diagrams, and pseudo-code.

Page 39: Lecture 1  computing and algorithms

39 of 39Module 1 : Computing and Algorithms

Reading Assignment • Textbook

Chapter 0: The Study of Computer Science0.1 to 0.4

Note: Though some material in textbook is not directly related to the lecture material, you can learn more from them.

• Exercise:Write down the algorithm for the problem on P.4.