Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Ā
Algoritma Dan Flowchart HOW DO WE SOLVE PROBLEMS
1. 04/10/2014
1
Algoritma dan Flowchart
Angga Akbar Fanani
1
Problem
ā¢ Pertanyaan atau tugas yang kita cari jawaban
dan/atau penyelesaiannya
2
We "just do" Guesswork-
and-luck
Experience
(possibly
someoneelse's)
Trial-and-error Scientifically"
HOW DO WE SOLVE PROBLEMS?
2. 04/10/2014
2
The Problem-solving Process
3
Problemspecification
Algorithm
Program
Executable (solution)
Design
Implementation
Compilation
Analysis
The Problem-solving Process
Problem specification
Algorithm
Program
Executable (solution)
Analysis
Design
Implementation
Compilation
āDoctor, my head hurtsā
Patient has elevated pressure in
anterior parietal lobe
1. Sterilize cranial saw
2. Anaesthetize patient
3. Remove top of skull
4. Get the big spoon...
5. etc., etc.
sterilize(saw,alcohol);
raise_hammer();
lower hammer(fast);
start(saw);
/* etc. etc. */
0100111010110010101010101001010101010100
11001010101010100101101001110101010101
00100101110100111101010101111101010100
0110100001101...
4
3. 04/10/2014
3
5
Definisi Algoritma
ā¢ Algoritma adalah urutan langkah logis tertentu
untuk memecahkan suatu masalah
ā« Urutan langkah logis, yang berarti algoritma harus
mengikuti suatu urutan tertentu, tidak boleh
melompat-lompat
ā¢ Alur pemikiran dalam menyelesaikan suatu
pekerjaan yang dituangkan secara tertulis.
ā« Alur pikiran, sehingga algoritma seseorang dapat
juga berbeda dari algoritma orang lain
ā« Tertulis, yang artinya dapat berupa kalimat,
gambar, atau tabel tertentu
6
4. 04/10/2014
4
Definisi Algoritma
ā¢ Deretan langkah-langkah komputasi yang
mentransformasikan data masukan menjadi
keluaran
ā¢ Deretan instruksi yang jelas untuk memecahkan
masalah, yaitu untuk memperoleh keluaran yang
diinginkan dari suatu masukan dalam jumlah waktu
yang terbatas
ā¢ Prosedur komputasi yang terdefinisi dengan baik
yang menggunakan beberapa nilai sebagai masukan
dan menghasilkan beberapa nilai yang disebut
keluaran.
7
Sejarah Algoritma
ā¢ Asal kata algoritma berasal dari nama Abu Jaāfar
Mohammed Ibn Musa al-Khowarizmi
ā« Ilmuan Persia yang menulis kitab al jabar wal-
muqabala (the book of restoration and reduction)
ā« Tahun 825 M
ā¢ Perubahan kata
ā« Algorism ļ proses menghitung dengan angka Arab
ā« Algorist ļ orang yang menggunakan angka Arab
ā« Algorithm ļ metode perhitungan secara umum
ā¢ Tahun 1950 ļ euclidās algorithm
ā« Langkah-langkah menemukan pembagi bersama
terbesar (common greatest divisor = gcd)
8
5. 04/10/2014
5
Ciri Algoritma (Donald E. Knuth)
ā¢ Finiteness: Algoritma harus berhenti setelah
mengerjakan sejumlah langkah terbatas
ā¢ Definiteness: Setiap langkah harus didefinisikan
secara tepat dan tidak berarti-dua (ambiguous)
ā¢ Algoritma memiliki nol atau lebih masukan (input)
ā¢ Algoritma memiliki nol atau lebih keluaran (output)
ā¢ Effectiveness: Algoritma harus efektif, setiap
langkah harus sederhana sehingga dapat dikerjakan
dalam sejumlah waktu yang masuk akal
9
Algorithm -- Examples
ā¢ A cooking recipe
ā¢ Assembly instructions for a model
ā¢ The rules of how to play a game
ā¢ VCR instructions
ā¢ Description of a martial arts technique
ā¢ Directions for driving from A to B
ā¢ A knitting pattern
ā¢ A car repair manual
10
6. 04/10/2014
6
Almond and Honey Slice
1/2 quantity Shortcrust Pastry
185 g unsalted butter
100 g castor sugar
5 tablespoonshoney
50 ml cream
50 ml brandy or any other
liqueuror spirit
300 g flaked almonds
Preheat oven for 200Ā° C
Line a 30 cm ļ“ 20 cm baking tray
with baking paper, and then with
pastry
Bake blind for 20 minutes, then
remove weights and foil
Turn oven up to 220Ā° C.
Bring remaining ingredients to a
boil, stirring.
Spread evenly over pastry.
Bake until topping is bubblingand
has caramelised evenly, about 15
minutes.
Cool before cuttinginto fingers or
squares.
11
From:Stephanie Alexander, The
Cookās Companion, Viking/Penguin,
Ringwood, Victoria,1996, p. 349.
Almond and Honey Slice
1/2 quantity Shortcrust Pastry
185 g unsalted butter
100 g castor sugar
5 tablespoonshoney
50 ml cream
50 ml brandy or any other
liqueuror spirit
300 g flaked almonds
Preheat oven for 200Ā° C
Line a 30 cm ļ“ 20 cm baking tray
with baking paper, and then with
pastry
Bake blind for 20 minutes, then
remove weights and foil
Turn oven up to 220Ā° C.
Bring remaining ingredients to a
boil, stirring.
Spread evenly over pastry.
Bake until topping is bubblingand
has caramelised evenly, about 15
minutes.
Cool before cuttinginto fingers or
squares.
12
From:Stephanie Alexander, The
Cookās Companion, Viking/Penguin,
Ringwood, Victoria,1996, p. 349.
Instructions are given
in the order in which
they are performed
(āexecutedā)
9. 04/10/2014
9
Correct algorithm
17
Values
ā¢ Represent quantities, amounts or measurements
ā¢ May be numerical or alphabetical
(or other things)
ā¢ Often have a unit related to their purpose
ā¢ Example:
ā« Recipe ingredients
18
10. 04/10/2014
10
Variables
ā¢ Are containers for values ā places to store
values
ā¢ Example:
19
This jar
can contain
10 cookies
50 grams of sugar
3 slices of cake
etc.
Values
Variable
Restrictions on Variables
ā¢ Variables may be restricted to contain a specific
type of value
20
11. 04/10/2014
11
Instructions (Primitives)
ā¢ Some action that is
ā« simple
ā« unambiguous
ā« that the system knows about...
ā« ...and should be able to actually do
21
Instructions ā Examples
ā¢ Take off your shoes
ā¢ Count to 10
ā¢ Cut along dotted line
ā¢ Knit 1
ā¢ Purl 2
ā¢ Pull rip-cord firmly
ā¢ Sift 10 grams of arsenic
22
Directions to perform
specific actions on values
and variables
12. 04/10/2014
12
Instructions -- Application
ā¢ Some instructions can only be applied to a
specific type of values or variables
ā¢ Examples:
23
Instructions (Primitives) --
Recommendations
ā¢ When writing an algorithm, make each
instruction simple and unambiguous
ā¢ Example:
24
Cut chicken into pieces and
brown the pieces on all
sides in a casserole dish
in hot olive oil.
Cut chicken into pieces.
Heat olive oil in a casserole
dish.
Brown the chicken pieces in
the casserole dish.
13. 04/10/2014
13
Instructions (Primitives)
ā¢ When writing an algorithm, make each
instruction simple and unambiguous
ā¢ Example:
25
Cut chicken into pieces and
brown the pieces on all
sides in a casserole dish
in hot olive oil.
Cut chicken into pieces.
Heat olive oil in a casserole
dish.
Brown the chicken pieces in
the casserole dish.
A āsequenceā of
simple instructions
Function
ā¢ A named sequence of instructions
ā¢ So that you can
ā« Refer to it collectively (by name)
ā« ...instead of individually (by each instruction in the
sequence)
ā¢ Example:
ā« Drive_To_Uni
26
14. 04/10/2014
14
Function -- Example
function Drive_To_Uni
{
1. find car keys
2. disable car alarm
3. open car door
4. get in car
5. shut car door
6. put keys in ignition
7. start car
8. back car out of
driveway
9. drive to end of street
10. turn right
11. drive to end of street
12. turn left
...etc...etc...etc
27
...etc...etc...etc...
52. find parking space
53. pull into parking
space
54. turn off engine
55. remove keys from
ignition
56. open car door
57. get out
58. shut car door
59. lock car door
60. enable alarm
}
Function ā Example
function Do_Wednesday
{
Wake_up
Have_Shower
Eat_Breakfast
Drive_To_Uni
Sit_1301_Lecture
...etc...etc...etc...
Drive_From_Uni
...etc...etc...etc...
}
function Do_Week
{
Do_Monday
Do_Tuesday
Do_Wednesday
Do_Thursday
...etc...etc...etc...
}
28
15. 04/10/2014
15
Components of an Algorithm
ā¢ Variables and values
ā¢ Instructions
ā« Sequences
ā« Functions
ā« Selections
ā« Repetitions
Also required: Documentation
29
Struktur dasar algoritma
Struktur Urut (Sequence),
Struktur Pemilihan
(Selection),
Struktur Pengulangan
(Repetition).
30
16. 04/10/2014
16
Sequence
ā¢ A series of instructions
ā¢ ...to be carried out one after the other...
ā¢ ...without hesitation or question
ā¢ Example:
ā«How to cook a Gourmet MealTM
31
Sequence -- Example
1. Open freezer door
2. Take out Gourmet Mealā¢
3. Close freezer door
4. Open microwave door
5. Put Gourmet Mealā¢ on carousel
6. Shut microwave door
7. Set microwave on high for 5 minutes
8. Start microwave
9. Wait 5 minutes
10.Open microwave door
11.Remove Gourmet Mealā¢
12.Close microwave door
32
17. 04/10/2014
17
Struktur Urut (Sequence)
ā¢ suatu strukturprogram dimana setiap
baris program akan dikerjakan secara urut
dari atas ke bawah sesuai dengan urutan
penulisannya.
ā¢ instruksibaris program 2 akan dikerjakan
jika instruksi baris program 1 telah selesai
dikerjakan.
33
Selection
ā¢ An instruction that decides which of two
possible sequences is executed
ā¢ The decision is based on a single true/false
condition
ā¢ Examples:
ā« Car repair
ā« Reciprocals
34
18. 04/10/2014
18
Selection Example ā Car Repair
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburettor
}
else
{
CheckDistributor
CheckIgnitionCoil
}
35
Selection Example ā Car Repair
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburettor
}
else
{
CheckDistributor
CheckIgnitionCoil
}
36
Should be a
true or false
condition
19. 04/10/2014
19
Selection Example ā Car Repair
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburettor
}
else
{
CheckDistributor
CheckIgnitionCoil
}
37
Sequence if
the condition
is true
Selection Example ā Car Repair
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburettor
}
else
{
CheckDistributor
CheckIgnitionCoil
}
38
Sequence if the
condition is
false
20. 04/10/2014
20
Struktur Pemilihan (Selection),
ā¢ Menghasilkan nilai benar (true) atau nilai salah
(false).
ā¢ IF sederhana
ļ IF <syarat> THEN
<instruksi>
ā¢ Pada bentuk IF sederhana ini,
intruksi akan dikerjakan jika
syarat yangdiuji benilai benar (true). Jika syarat
yang diuji benilai salah (false) maka tidak ada
instruksi yang dikerjakan.
39
IF ā¦ THEN ā¦ ELSE ā¦
ā¢ IF <syarat> THEN
<instruksi1>
ELSE
<instruksi2>
ā¢ Pada bentuk ini terdapat dua
kemungkinan pilihan yang
akan dikerjakan berdasarkan
hasil pengujian, jika syarat
yang diuji benilai benar maka instruksi1 yang
dikerjakan, dan jika syarat yang diuji bernilai
salah maka instruksi2 yang dikerjakan.
40
21. 04/10/2014
21
IF Bersarang (Nested IF)
ā¢ IF <syarat1> THEN
<instruksi1>
ā¢ ELSE IF <syarat2> THEN
<instruksi2>
ā¢ ELSE IF <syarat3> THEN
<instruksi3>
ā¢ ELSE IF <syaratm> THEN
<instruksim>
ā¢ ELSE
<Instruksin>
41
Repetition
ā¢ Repeat an instruction...
ā« ...while (or maybe until) some true or
false condition occurs
ā« Test the condition each time before
repeating the instruction
ā¢ Also known as iteration or loop
ā¢ Example:
ā« Algorithm for getting a date
42
22. 04/10/2014
22
Repetition ā Example
function AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time,
"?")
ListenToReply ( )
start begging count at zero
while (reply is "No" and begging count < 100)
{
Say("Oh please!")
add 1 to begging count
ListenToReply ( )
}
}
43
Repetition ā Example (cont)
function AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time,
"?")
ListenToReply ( )
start begging count at zero
while ( reply is "No" and begging count < 100 )
{
Say("Oh please!")
add 1 to begging count
ListenToReply ( )
}
}
44
Condition is tested
before sequence
23. 04/10/2014
23
Repetition ā Example (cont)
function AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time, "?")
ListenToReply ( )
start begging count at zero
while (reply is "No" and begging count < 100)
{
Say("Oh please!")
add 1 to begging count
ListenToReply ( )
}
}
45
Sequence may not
get executed at all
Repetition ā Example (cont)
function AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time, "?")
ListenToReply ( )
start begging count at zero
while (reply is "No" and begging count < 100)
{
Say("Oh please!")
add 1 to begging count
ListenToReply ( )
}
}
46
Ensure initial
values of variables
used in the
conditions are set
correctly
24. 04/10/2014
24
Repetition ā Example (cont)
function AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time,
"?")
ListenToReply ( )
start begging count at zero
while (reply is "No" and begging count < 100)
{
Say("Oh please!")
add 1 to begging count
ListenToReply ( )
}
}
47
Ensure the variables
used in the conditions
are updated in each
repetition
Repetition ā Example (cont)
function AskOnDate ( name, time, location )
{
Phone(name)
Say("Hey", name, "it's your lucky day!")
Say("Wanna come to", location, "at", time,
"?")
ListenToReply ( )
start begging count at zero
while (reply is "No" and begging count < 100)
{
Say("Oh please!")
}
}
48
Infinite loop
ā¢ What if we donāt increment the begging count?
25. 04/10/2014
25
Struktur pengulangan repetition
ā¢ Struktur for
ā¢ Mengulangsatu baris
instruksiatau satu blok
instruksisampai jumlah
perulanganyang
disyaratkanterpenuhi.
ā¢ Ciri utama terdapatnilai
awaldan nilai akhir yang
menunjukkanbanyaknya
pengulanganyang akan
dilakukan.
49
Struktur while
ā¢ untukmengulangsatu baris instruksi atau
satu blok baris instruksi selama syarat
yang diberikan masih terpenuhi.
ā¢ Syarat akan diuji didepan.
ā¢ Jika syarat yang diuji bernilai benar maka
instruksi1,instruksi2, instruksi3 akan
dikerjakan, setelah mengerjakan
instruksi1, instruksi2, instruksi3 maka
syarat akan diuji lagi.
ā¢ pengulangan akan berhentijika syarat
yang diuji bernilai salah.
50
26. 04/10/2014
26
Struktur do ā¦ while
ā¢ mengulangsatu baris instruksi
atau satu blok baris instruksi
sampai syarat tidak terpenuhi.
ā¢ syarat akan diuji dibelakang,
sehinggabaris instruksi yang
masukdalam blok doā¦while
minimal akan dikerjakan satu
sekali.
51
Documentation
ā¢ Records what the algorithm does
ā¢ Describes how it does it
ā¢ Explains the purpose of each component of the
algorithm
ā¢ Notes restrictions or expectations
ā¢ Example:
ā« Getting a date (again)
52
27. 04/10/2014
27
Documentation -- Example
Think of something romantic to do
decide on time and location
Work through address book to look for a
person
initialise booking to āunsuccessfulā
until (successfully booked)
{
get next Name in little black book
AskOnDate(Name, Time, Location)
DetermineBookingSuccess
}
Assumes that I will find someone in the book
before it runs out
SighWithRelief
53
From Algorithms to Programs
54
Problem
Program
Program
Algorithm
Algorithm: A sequence of
instructions describing how to do a
task (or process)
28. 04/10/2014
28
The Software Development Process
ā¢ Define the problem clearly
ā¢ Analyse the problem thoroughly
ā¢ Design an algorithm carefully
ā¢ Code the algorithm efficiently
ā¢ Test the code thoroughly
ā¢ Document the system lucidly
55
Top-down Algorithm Design
ā¢ Write down what you have to do
ā¢ Break that into 3-7 smaller steps
ā¢ Break each step into 3-7 smaller steps
ā¢ Keeping subdividing until each individual
step is easy enough to do
ā¢ Example:
ā« Learning
56
29. 04/10/2014
29
Top-down Design -- Example
57
Learn
Prepare
Study
Reinforce
Read
Make notes
Prepare questions
Attend lecture
Listen and think
Complete prac
Attend tute
Record answers to
questions
Revise notes
Read lecture notes
Read textbook
Read exercise
Design algorithm
Code solution
Test and document
Record insights
Cara Penyajian Algoritma
ā¢ Uraian Deskriptif
ā« Menggunakan bahasa yang bisa di gunakan sehari-
hari
ā¢ Menggunakan flow chart (diagram alir)
ā¢ Menggunakan pseudo-code
ā« Ditulis dalam kode-kode yang disepakati yang
mempunyai arti
58
30. 04/10/2014
30
59
Algoritma dan Flowchart
ā¢ Algoritma merupakan urutan langkah logika
pemikiran detail yang diperlukan untuk
memecahkan masalah.
ā¢ Algoritma dapat direpresentasikan dalam format
grafis yang disebut flowchart atau diagram
alir.
ā¢ Flowchart juga dapat digunakan untuk
menggambarkan aliran urutan program
60
31. 04/10/2014
31
Bagan-bagan Flowchart
ā¢ Terminal
ā« Bentuk : rounded rectangle
ā« Mengindikasikan titik awal atau akhir
61
Bagan-bagan Flowchart
ā¢ Connector
ā« Bentuk : circle
ā« Mengindikasikan titik penghubung ke bagan lain yang
terputus dalam satu halaman karena terlalu jauh atau
memungkinkan memotong garis panah lain
62
32. 04/10/2014
32
Bagan-bagan Flowchart
ā¢ Connector
ā« Bentuk : pentagon
ā« Mengindikasikan titik penghubung ke bagan lain yang
terputus karena beda halaman
63
Bagan-bagan Flowchart
ā¢ Preparation atau initialization
ā« Bentuk : hexagon
ā« Mengindikasikan tahap awal pemrograman (misalnya
mendeklarasikan variabel, mengidentifikasikan nilai
awal konstanta atau variabel)
64
33. 04/10/2014
33
Bagan-bagan Flowchart
ā¢ Data
ā« Bentuk : parallelogram
ā« Mengindikasikan masukan atau keluaran data
65
Bagan-bagan Flowchart
ā¢ Process
ā« Bentuk : rectangle
ā« Mengindikasikan proses komputasi
66
34. 04/10/2014
34
Bagan-bagan Flowchart
ā¢ Predefined/Nested Process (Subroutine)
ā« Bentuk : rectangle
ā« Mengindikasikan proses komputasi
67
Bagan-bagan Flowchart
ā¢ Manual Operation
ā« Bentuk : trapezoid
ā« Mengindikasikan operasi yang dilakukan secara
manual
68
35. 04/10/2014
35
Bagan-bagan Flowchart
ā¢ Manual Input
ā« Bentuk : trapezoid
ā« Mengindikasikan masukan manual menggunakan
input device (misalnya keyboard, mouse, bar code
reader, imaging device)
69
Bagan-bagan Flowchart
ā¢ Card
ā« Bentuk : chamfered rectangle
ā« Mengindikasikan penggunaan card reader (misalnya
punched card reader ataupun memory card reader)
70
36. 04/10/2014
36
Bagan-bagan Flowchart
ā¢ Database atau Repository
ā« Bentuk : drum
ā« Mengindikasikan penggunaan media penyimpanan
basisdata
71
Bagan-bagan Flowchart
ā¢ Display
ā« Bentuk : CRT Symbol
ā« Mengindikasikan proses penampilan informasi untuk
dibaca atau dilihat user di layar monitor atau projector
72
37. 04/10/2014
37
Bagan-bagan Flowchart
ā¢ Report
ā« Bentuk : Sheet
ā« Mengindikasikan proses pencetakan laporan untuk
dibaca atau dilihat user menggunakan printer atau
plotter
73
Bagan-bagan Flowchart
ā¢ Annotation
ā« Bentuk : Arrow
ā« Mengindikasikan hubungan presedensi antar operasi
atau alur urutan operasi
74
38. 04/10/2014
38
Bagan-bagan Flowchart
ā¢ Decision
ā« Bentuk : diamond
ā« Mengindikasikan pencabangan alternatif operasi
dengan pengambilan keputusan berdasarkan kriteria
atau kondisi
75
Decision / Condition Structure
76
YES
NO
YES
NO
YES
NO
YES
NO
39. 04/10/2014
39
Repetition / Recursive Structure
77
YES
NO
YES
NO
Metodologi Pengembangan Program
78
Mulai
Analisis Domain
Masalah
Merumuskan
Algoritma
Semua
Skenario
Tertulis ?
Mapping
Flowchart
Struktur
Logika
Sesuai ?
Verify
Program Code
Identifier,
Syntax,
Semantic ?
Debug
Listing Program
Compile
Machine Code
Compile
Berhasil
?
Eksekusi
Program
Eksekusi
Berhasil
?
Keluaran
Program
Sesuai ?
Eksekusi
Program
Selesai
40. 04/10/2014
40
Contoh Algoritma
ļ¢Step 1: Turn the stereo on.
ļ¢Step 2: If the band is set to FM, switch it to AM.
ļ¢Step 3: If the station is set to a station greater
than 610, turn the station to the left until 610 is
reached.
ļ¢Step 4: If the station is set to a station less than
610, turn the station to the right until 610 is
reached.
ļ¢Step 5: Listen to the best sports talk station in
the country.
79
80
Contoh Flowchart