Sie sind auf Seite 1von 39

Introduction to

Computational Thinking
Module 1 :
Computing and Algorithms

Module 1 : Computing and Algorithms

Assoc Prof Chi-Wing FU, Philip


Office: N4-02c-104
1 of 39
email: cwfu[at]ntu.edu.sg

Topics
What is Computational Thinking?
What is an Algorithm
How to Express an Algorithm?
Flowcharts
Nassi-Schneiderman Diagrams
Pseudo-code

Module 1 : Computing and Algorithms

2 of 39

What is Computational Thinking?


It is a problem solving process that involves
Analysis and modeling of the problem and the data
Understand of how computers work
Acquire logic and procedural design concepts
Formulate a computational solution that one
can use a computer to resolve the problem
Automation
Efficiency

Module 1 : Computing and Algorithms

3 of 39

What is a problem?
First you may wonder
What is a problem?
It is a question proposed for a solution.
Lets start with a simple example:
Find the last 4 digits in factorial n or n!

Module 1 : Computing and Algorithms

4 of 39

What is a problem?
Before that What is a factorial?
0! = 1
1! = 1
2! = 2 x 1! = 2 x 1
3! = 3 x 2! = 3 x 2 x 1 = 6
n! = n x (n-1)! = = n x (n-1) x (n-2) x x 2 x 1

Module 1 : Computing and Algorithms

5 of 39

What is a problem?
So we have to find out the last 4 digits in n!
Input: n
Output: 4 digits: could be 0000, 0001, , 9999
Computational thinking is the problem-solving
process to devise a method to compute the solution

Module 1 : Computing and Algorithms

6 of 39

Lets do Problem Solving!!!


Now, let me give you a minute to think about
how to solve this problem?
.
Dont look at the next slides

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

Module 1 : Computing and Algorithms

7 of 39

Accumulator

Method 1
Method 1: (Straight-forward)
Step 1: First, ask the user to input or tell you n
Step 2: We can compute n! by iteratively doing
the multiplication (like using a calculator)
Accumulator initialized to n
Accumulator Accumulator x (n-1)
Accumulator Accumulator x (n-2)

Module 1 : Computing and Algorithms

8 of 39

Method 1

Accumulator Accumulator x 3
Accumulator Accumulator x 2
Accumulator Accumulator x 1
Step 3: Lastly, output last 4 digits in Accumulator

Module 1 : Computing and Algorithms

9 of 39

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.0414x1064.
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!
Module 1 : Computing and Algorithms

10 of 39

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!!!
Module 1 : Computing and Algorithms

11 of 39

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!!!
Module 1 : Computing and Algorithms

12 of 39

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,
See course Introduction to Computing Systems
(which runs in parallel)
http://en.wikipedia.org/wiki/Human_computer
Module 1 : Computing and Algorithms

13 of 39

Topics
What is Computational Thinking?
What is an Algorithm?
How to Express an Algorithm?
Flowcharts
Nassi-Schneiderman Diagrams
Pseudo-code

Module 1 : Computing and Algorithms

14 of 39

What is an Algorithm?
Just like the example you saw
When you formulate a method/procedure for
solving a problem, it has to be computable;
such a procedure 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
Module 1 : Computing and Algorithms

15 of 39

What is an Algorithm?
Problem

Algorithm

Computational
Thinking

Programming

Program

Run on

So Algorithm and computational thinking are


both independent of the programming
language we choose to implement the program

Module 1 : Computing and Algorithms

16 of 39

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 may take in the future:
CZ2001 Algorithm in 2nd year)
Module 1 : Computing and Algorithms

17 of 39

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

Module 1 : Computing and Algorithms

18 of 39

Aspects of an Algorithm
Example: Making scrambled eggs
1. Beating the eggs for 20 to 35 seconds in a bowl
2. Heating a frying pan over a medium-low heat
3. Melt some butter in the frying pan
4. Cook eggs on the pan and stir eggs while cooking
5. Add other ingredients
6. Serve the scrambled eggs

Module 1 : Computing and Algorithms

19 of 39

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


Example: Making scrambled eggs

Source: http://whatscookingamerica.net/Eggs/ScrambledOmelette.htm
Module 1 : Computing and Algorithms

20 of 39

Topics
What is Computational Thinking?
What is an Algorithm?
How to Express an algorithm?
Flowcharts
Nassi-Schneiderman diagrams
Pseudo-code

Module 1 : Computing and Algorithms

21 of 39

How to express an Algorithm?


Algorithms is basically Sequential
(step after step)
Sequence:

Step 1

Step 2

Step 3

But may include


Branching (making a selection)
Looping (repeating certain operations)

Module 1 : Computing and Algorithms

22 of 39

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 eggs
1. Beating the eggs for 20 to 35 seconds in a bowl
2. Heating a frying pan over a medium-low heat
3. Melt some butter in the frying pan
4. Cook eggs on the pan and stir eggs while cooking
5. Add other ingredients
6. Serve the scrambled eggs

Module 1 : Computing and Algorithms

23 of 39

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 eggs
1. Beating the eggs for 20 to 35 seconds in a bowl
2. Heating a frying pan over a medium-low heat
3. Melt some butter in the frying pan
4. Cook eggs on the pan and stir eggs while cooking
5. Add other ingredients
6. Serve the scrambled eggs

Module 1 : Computing and Algorithms

24 of 39

How to express an Algorithm?


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

Module 1 : Computing and Algorithms

25 of 39

General Notes
No strict rules
Informal language - mix of English and
keywords
Common keywords: IF, ELSE, WHILE, etc.
Other keywords: READ, PRINT, SET,
INITIALIZE, COMPUTE, ADD, SUBTRACT, etc.
Usually start an operation sentence with a verb
(description should be concise and precise)

Module 1 : Computing and Algorithms

26 of 39

#1. Flowcharts
Represent an algorithm by a diagram for
effective visualization
Symbol

Name
Process

Decision

Input / Output

Terminal

Flowlines
Module 1 : Computing and Algorithms

27 of 39

#1. Flowcharts
start

Beating the eggs in a bowl


Heating a frying pan

Can do repetition
Melt butter and put eggs on pan

Decision blocks
(make choices)

If eggs ok?

Stir eggs

Any ingredient?

Output!

Add ingredients

Serve Scrambled Eggs

Can do selection

end
Module 1 : Computing and Algorithms

28 of 39

#2. Nassi-Schneiderman diagrams


Similar to a flowchart but it is arrow-free
and so more space-friendly
#1: Sequential operations: Just stack them up
Beating the eggs in a bowl
Heating a frying pan
Melt butter and put eggs on pan

Module 1 : Computing and Algorithms

29 of 39

#2. Nassi-Schneiderman diagrams


#2: Selection operations: Make Branches
Any ingredient?

Here is empty,
meaning do nothing
but we may also put
operations here

Y
Add ingredient

Serve Scrambled Eggs

Join again

#3: Looping operations: Repetition


Eggs not ok?

While condition is true,


keep running the
operations inside

Stir eggs

Module 1 : Computing and Algorithms

30 of 39

#2. Nassi-Schneiderman diagrams


Putting them together:
Beating the eggs in a bowl
Heating a frying pan
Melt butter and put eggs on pan
Egg not ok?

Stir eggs

Can do repetition
Can do selection

Any ingredient?

Y
Add ingredient

Serve Scrambled Eggs


Module 1 : Computing and Algorithms

31 of 39

#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
[skip if N is smaller than 3]
And accumulate to NFACT
Module 1 : Computing and Algorithms

32 of 39

#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

Module 1 : Computing and Algorithms

33 of 39

#3. Pseudo-code
Example #1: Making Scrambled Eggs
BEAT the eggs for 20 to 35 seconds in a bowl
HEAT a frying pan over a medium-low heat
MELT some butter in the frying pan
PUT eggs on pan
WHILE eggs not okay
STIR eggs while cooking
END WHILE
IF any ingredients
Add other ingredients
END IF
SERVE the scrambled eggs
Module 1 : Computing and Algorithms

34 of 39

#3. Pseudo-code
Example #2: factorial
READ N from user input
IF N <= 1
nfact = 1
ELSE
nfact = 2
REPEAT I = 3 to N
nfact = nfact x I
END IF
OUTPUT nfact

Module 1 : Computing and Algorithms

35 of 39

#3. Pseudo-code
Guidelines:
Write only one statement per line
Capitalize the keywords
Indent to show hierarchy
End multi-line structures
Keep statements programming-language
independent

Module 1 : Computing and Algorithms

36 of 39

Expressing an Algorithm
Important Note:

More detail on
these techniques
later in this course

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)
Module 1 : Computing and Algorithms

37 of 39

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.
Module 1 : Computing and Algorithms

38 of 39

Reading Assignment
Textbook
Chapter 0: The Study of Computer Science
0.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.
Module 1 : Computing and Algorithms

39 of 39

Das könnte Ihnen auch gefallen