Download DDC 1012 Slide Topic 2 Powerpoint presentation - Elearning-KL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Approximations of π wikipedia , lookup

Big O notation wikipedia , lookup

Halting problem wikipedia , lookup

Algorithm characterizations wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Transcript
DDC1012: PROGRAMMING
CHAPTER 2: ALGORITHM
Prepared By:
Pn. Nik Maria Nik Mahamood
Reference:
Joyce Farrell. Programming logic and Design,Fifth Edition, Course Technology Cengage
Learning.
Marini Abu Bakar, Pengaturcaraan C, Prentice Hall, 2002. (Call Number: QA76. 73.C15 S93
2002)
INSPIRING CREATIVE AND INNOVATIVE MINDS
ALGORITHM
2.1
2.2
2.3
2.4
2.5
Introduction to Algorithm
Representation of algorithm: Pseudocode and
Flowchart
Steps in Algorithms
Selection Structure
Repetition Structure
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.1 INTRODUCTION TO ALGORITHM
What is an algorithm?
• The term Algorithm - pronounced AL-go-rith-um
• An algorithm is:
"An effective procedure for solving a problem
in a finite number of steps.“
• The algorithm is part of the blueprint or plan for the
computer program.
MORE INFO:
The word derives from the name of the mathematician, Mohammed ibn-Musa alKhwarizmi.
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.1 INTRODUCTION TO ALGORITHM
• It is effective, which means that
– an answer is found and it finishes
– it has a finite number of steps.
• A well-designed algorithm will always provide an answer.
• A well-designed algorithm is also guaranteed to
terminate.
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.1 INTRODUCTION TO ALGORITHM
• Specifically, an algorithm has the following properties:
– The actions to be executed
– It must result in a finite series of actions.
– The order in which these actions are to be executed.
– The sequence terminates with a solution
MORE INFO:
- An algorithm is a finite list of instructions on how to perform a task
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.1 INTRODUCTION TO ALGORITHM
• What is wrong with this logic for making a cake?
Stir
Add two eggs
Bake at 350 degrees for 45 minutes
Add three cups of flour
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.1 INTRODUCTION TO ALGORITHM
• Logic for multiplying a number by 2 (includes input,
processing, and output statements)
Get Number
Result = Number * 2
Print Result
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
• Characteristic of algorithm:
– conceptual or abstract.
• Because of that, we need a specific methods to
represent algorithm in order to deliver for people
and computers.
• Two elements: syntax and semantic needed in
representing algorithms.
• Algorithm can be represented using
– Pseudocode
– Flowchart
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Flowchart
• A flowchart is a graphical representation of an algorithm
or of a portion of an algorithm.
• Flowcharts are drawn using certain special-purpose
symbols such as
– Rectangles
– Diamonds
– Oval and small circles
• These symbols are connected by arrows called flow lines.
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Syntax
Semantic
Begin/End
Process
Input/Output
Decision
Connector
Flowlines
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Description of Flowchart’s Language
• The major symbols are the DECISION and the PROCESS
symbols.
• The BEGIN and END symbols are called the terminals.
• The INPUT and OUTPUT symbols are used to read input
and display output.
• Flow lines connect the steps
– Show the sequence of statements
– Have arrows to show the direction
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
•
There are some important rules concerning the symbols
and these rules apply also to other ways of stating
algorithms:
– Processes have only one entry point and one exit
point.
– Decisions have only one entry point, one TRUE exit
point and one FALSE exit point.
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Understanding the Three Basic Structures
• Structure: basic unit of programming logic
• Any program can be constructed from only three basic
types of structures
– Sequence
• Perform actions in order
• No branching or skipping any task
– Selection (decision)
• Ask a question, take one of two actions
• Dual-alternative or single-alternative
– Loop
• Repeat actions based on answer to a question
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
1. Sequence structure:
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
2. Selection structure: two selections using if ..else
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
•
Example of selection structure : single selection using if
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
•
Example of selection structure: multiple selection using
if..else if
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
•
Example of selection structure: multiple selection using
case
Decision
Process 1
Process 2
Process 3
INSPIRING CREATIVE AND INNOVATIVE MINDS
Process 4
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
3. Loop structure:
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Pseudocode
• Pseudocode is an artificial and informal language that
helps programmers to develop algorithm.
• The pseudocode is particular useful for developing
algorithms that will be converted to structured C
programs.
• Pseudocode is similar to everyday English;
– it is convenient
– user-friendly although it is not an actual computer
program language.
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
• Example: Write a pseudocode to multiply a number by 2
Start
Get Number
Result = Number * 2
Print Result
End
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Input, output, iteration, decision and processing in pseudocode
Group
Keyword used
Input/Output
INPUT, READ
Used to get values from a data
source, a keyboard for instance.
Example:
READ num
DISPLAY
Used to output values to a data
sink, a screen or printer for
instance
Example:
DISPLAY num
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Group
Keyword used
Decision
IF <condition> THEN
statement
IF <condition> THEN
statement_1
ELSE
statement_2
IF <condition> THEN
statement_1
ELSE IF <condition> THEN
statement_2
ELSE
statement_3
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Group
Keyword used
Decision
IF <condition> THEN
IF <condition> THEN
statement_1
ELSE
statement_2
ENDIF
IF <condition> THEN
statement_1
statement_2
ENDIF
ELSE
statement_4
statement_5
END ELSE
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Group
Keyword used
Iteration
REPEAT
statement
UNTIL (condition)
The REPEAT loop executes the statement until the condition becomes true.
WHILE (condition)
statement
END WHILE
The WHILE loop executes the statement while the <condition> is true
FOR (var = startValue; testValue ; var = stop value)
statement
ENDFOR
The FOR loop iterates for a fixed number of steps.
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Group
Keyword used
Processing
ADD, SUBTRACT, COMPUTE, SET
Example of operators :
+ add
- subtract
* multiply
/ divide
= assign
Example of statements:
count  count + 22
x  count / 12
sum  sum - count
x  count * sum
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Relational Operators
 Relational operators are used
relationship between variables
== equal to (a pair of equals signs)
< less than
<= less than or equal to
> greater than or equal to
>= greater than or equal to
!= not equal to
to
evaluate
INSPIRING CREATIVE AND INNOVATIVE MINDS
the
2.2 ALGORITHM: PSEUDOCODE AND FLOWCHART
Example of using Relational Operators
IF count == 32 THEN ...
This decision states that if the variable count contains 32
execute the statement following THEN.
WHILE count <= 50
...
END WHILE
The statements in the while loop will execute while the value
of count is 50 or less.
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.3 STEPS IN ALGORITHM
Guides to develop algorithm?
• Recognizes input and output
• Recognizes and lists all the sub-problems. Each must be
solved to get the right decision.
• For each sub-problem, you must recognize and list the
steps required to solve it.
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.3 STEPS IN ALGORITHM
Test algorithm:
• Process of testing can be done by execute the steps in
algorithm.
• This process known as desk-checking
• Testing – technique used to evaluate algorithm logic.
• This process is important before we write a coding.
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.4 SELECTION STRUCTURES
•
•
This structure is used to choose among alternative
actions
There are three form of selection structures:
– Single selection
– Two selections
– Multi selections
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.4 SELECTION STRUCTURES
Single selection
• Action is provided for only one outcome.
• Example:
START
Read mark
IF (mark >= 50) THEN
display “Pass”
END IF
STOP
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.4 SELECTION STRUCTURES
START
Read Mark
Mark >= 50
Yes
Display “Pass”
STOP
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.4 SELECTION STRUCTURES
Two selections
• Provides an action for each of two possible outcomes
Example:
START
Read mark
IF (mark >= 50) THEN
Display “Pass”
ELSE
Display “Fail”
END IF
STOP
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.4 SELECTION STRUCTURES
START
Read Mark
No
Mark >= 50
Display “Fail”
Yes
Display “Pass”
STOP
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.4 SELECTION STRUCTURES
Multi selections
• Provides an action for each of a series of alternatives outcomes
Example:
BEGIN
Read mark
IF (mark >= 80) THEN
Grade  ‘A’
ELSE_IF (mark >= 70 AND mark < 80) THEN
Grade  ‘B’
ELSE_IF (mark >= 50 AND mark < 70) THEN
Grade  ‘C’
ELSE
Grade  ‘F’
END IF
Print Grade
END
INSPIRING CREATIVE AND INNOVATIVE MINDS
START
Read Mark
Mark >= 80
Yes
Grade  ‘A’
No
Mark >= 70
And Mark < 80
Yes
Grade  ‘B’
Display “Grade
obtained is “ Grade
No
Mark >= 50
And Mark < 70
Yes Grade  ‘C’
No
Grade  ‘F’
INSPIRING CREATIVE AND INNOVATIVE MINDS
STOP
START
Read Mark
Yes
Mark >= 80
No
Grade  ‘A’
No
Yes
Mark >= 70
And Mark < 80
Grade  ‘B’
Yes
Mark >= 50
And Mark < 70
Grade  ‘C’
No
Grade  ‘F’
Display “Grade obtained is “ Grade
STOP
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.5 REPETITION STRUCTURES
•
A repetition/loop structures allows the programmers to
specify that an action is to be repeated while some
condition remains true.
– Write one set of instructions to operate on multiple,
separate sets of data
• Must control number of repetitions to avoid an infinite
loop
• Repetition controlled by
– Counter
– Sentinel
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.5 REPETITION STRUCTURES
• Three actions make a while loop end correctly:
1. Loop control variable is initialized
• Prior to entering the loop
2. Loop control variable is tested
• If result is true, loop body entered
3. Loop control variable must be updated in loop body
• while expression eventually evaluates to false
• Loop control variables updated by:
– Incrementing
– Decrementing
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.5 REPETITION STRUCTURES
Example of counter - controlled repetition
Design an algorithm (pseudocode / flowchart) that calculates and prints the sum of
five numbers user inputs.
BEGIN
Bil  0
Sum  0
WHILE (Bil < 5)
Read num
Sum  Sum + num
Bil  Bil + 1
END WHILE
Display Sum
END
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.5 REPETITION STRUCTURES
Example of sentinel - controlled repetition
Design an algorithm (pseudocode / flowchart) that calculates and prints the sum for
a series of numbers user inputs. Use a sentinel value of -1 for the numbers value to
terminate a loop.
BEGIN
Sum  0
Read num
WHILE (num != -1)
Sum  Sum + num
Read num
END WHILE
Display Sum
END
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.5 REPETITION STRUCTURES
Design an algorithm (pseudocode / flowchart) that checks and prints either a number
entered is positive or negative. Use a counter loop to ensure that the loop repeats five
times. The algorithm should generate a display of prompts, inputs, and final output
that looks like this:
Sample of output:
Enter a number: 2
The number entered is positive.
Enter a number: 3
The number entered is negative.
Enter a number: 4
The number entered is positive.
Enter a number: 6
The number entered is positive
Enter a number: 7
The number entered is negative.
INSPIRING CREATIVE AND INNOVATIVE MINDS