Professional Documents
Culture Documents
Bab 3 Komponen Algoritma - Compressed
Bab 3 Komponen Algoritma - Compressed
TUI1E3
PENGANTAR ALGORITMA
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
Sequence (of instructions)
Procedure (involving instructions)
Selection (between instructions)
Repetition (of instructions)
Documentation (beside instructions)
Sequence
A series of instructions
...to be carried out one after the other...
...without hesitation or question
Example:
How to cook a Gourmet Meal TM
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
Deitel
4 & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
5
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
Sequence (of instructions)
Procedure (involving instructions)
Selection (between instructions)
Repetition (of instructions)
Documentation (beside instructions)
Procedure
Deitel
6 & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
7
Procedure --Example
procedure Drive_To_Uni
{
1. find car keys
...etc...etc...etc...
2. disable car alarm
52. find parking space
3. open car door
53. pull into parking
4. get in car
space
5. shut car door
54. turn off engine
6. put keys in ignition
55. remove keys from
7. start car
ignition
8. back car out of
56. open car door
driveway
57. get out
9. drive to end of street
58. shut car door
10. turn right
59. lock car door
11. drive to end of street
60. enable alarm
12. turn left
}
...etc...etc...etc
Deitel
7 & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
8
Deitel
8 & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
9
Deitel
9 & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
10
procedure Do_Wednesday
{
Wake_up
Have_Shower We use brackets to
Eat_Breakfast mark the
Drive_To_Uni beginning and end
Sit_1301_Lecture of a sequence.
...etc...etc...etc...
Drive_From_Uni
...etc...etc...etc...
}
Deitel
10 & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
11
Deitel
11 & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
12
Procedure
A procedure may have a set of parameters
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
Sequence (of instructions)
Procedure (involving instructions)
Selection (between instructions)
Repetition (of instructions)
Documentation (beside instructions)
Selection
Examples:
Q. Give an
algorithm for Reciprocal of 2: 1/2
computing the
reciprocal of a
Reciprocal of -3/4:
number. 1/(-3/4) = -4/3
Reciprocal of 0:
“undefined”
19
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Selection Example – 20
Reciprocals (cont)
Algorithm:
input Num
if (Num is not equal 0)
Q. Give an then
algorithm for {
computing the output 1/Num
reciprocal of a }
number. else
{
output "infinity"
}
20
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Selection Example-- Reciprocals 21
Algorithm:
input Num
if (Num is not equal 0)
Num is a variable
whose value then
depends on the {
actual number the output 1/Num
user provides. }
else
{
output "infinity"
}
21
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Selection Example – 22
Reciprocals (cont)
Algorithm:
input Num
if (Num is not equal 0)
Condition then
depends on the {
value of Num output 1/Num
}
else
{
output "infinity"
}
22
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Selection Example – 23
Reciprocals (cont)
Algorithm:
input Num
if (Num is not equal 0)
then
{
For a given value of
output 1/Num
Num, only one of
these two sequences }
can be executed else
{
output "infinity"
}
23
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Selection Example – 24
Reciprocals (cont)
Algorithm:
input Num
if (Num is not equal 0)
then
{
Executed if Num is output 1/Num
not equal to 0 }
else
{
output "infinity"
}
24
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Selection Example – 25
Reciprocals (cont)
Algorithm:
input Num
if (Num is not equal 0)
then
{
output 1/Num
}
else
Executed if Num is {
equal to 0 output "infinity"
}
25
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
26
Selection -- Exercise
Will the following algorithms produce the same output?
Algorithm 1: Algorithm 2:
input Num input Num
if (Num is not equal 0) if (Num is not equal 0)
then then
{ {
output 1/Num output 1/Num
} }
else
{ output "infinity"
output "infinity"
}
26
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
27
27
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Solution 1
28
if ( today is Wednesday )
then Often called a
{
if ( the time is 10.00am ) “nested selection”
then
{
Go to CSE1301 Lecture
}
}
else
...etc...etc...etc...
Solution 2
28
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
29
29
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Selection – 30
if ( button pressed is 1 ) {
then PayBills
{ }
CheckAccountBalance else
} {
else if ( button pressed is 4 )
{ then
if ( button pressed is 2 ) {
then ExitPhoneBanking
{ }
TransferFunds else
} {
else say “Invalid option”
{ }
if ( button pressed is 3 ) }
then } Form 1
}
32
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Selection – 33
33
Form 1
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Selection – 34
Form 1
then }
34
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Selection – 35
37
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
Selection – 38
38
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
39
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
Sequence (of instructions)
Procedure (involving instructions)
Selection (between instructions)
Repetition (of instructions)
Documentation (beside instructions)
Repetition
• Repeat an instruction...
• ...while (or maybe until) some true or false condition
occurs
• Test the condition each time before repeating the
instruction
Repetition -- Example
procedure 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 ( )
}
}
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
41
42
Repetition – Variation
decide on Time and Location
initialise booking to “unsuccessful”
while ( not successfully booked )
{
get next Name in little black book
AskOnDate(Name, Time, Location)
DetermineBookingSuccess
}
SighWithRelief
Repetition – Post-Tested
Loop
decide on Time and Location
initialise booking to “unsuccessful”
do Sequence is
{ executed at
get next Name in little black book
least once
AskOnDate(Name, Time, Location)
DetermineBookingSuccess
} while ( not successfully booked )
post-tested loop
SighWithRelief
Deitel
50 & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
51
SighWithRelief
Deitel
51 & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
52
Repetition – Variations
decide on Time and Location
initialise booking to “unsuccessful”
loop
{
get next Name in little black book
AskOnDate(Name, Time, Location)
DetermineBookingSuccess
if ( successfully booked )
then
{
break loop
}
}
SighWithRelief
Deitel
52 & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
53
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
Sequence (of instructions)
Procedure (involving instructions)
Selection (between instructions)
Repetition (of instructions)
Documentation (beside instructions)
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)
Documentation
Think of something romantic to do
decide on time and location
Assumes that I will find someone in the book before it runs out
SighWithRelief
Deitel & Deitel, C: How to program Chapter 3, Sections 3.8 to 3.13
55
56
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
Sequence (of instructions)
Procedure (involving instructions)
Selection (between instructions)
Repetition (of instructions)
Documentation (beside instructions)
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
STRUKTUR PEMILIHAN
Struktur Pemilihan adalah suatu statement untuk
pengambilan keputusan berdasarkan kondisi
tertentu.
IF
If
If else
If, else if, else
Switch Case
59
IF
Bentuk If adalah yang paling sederhana,
mengandung suatu pernyataan tunggal yang
dieksekusi jika ekspresi bersyarat adalah benar.
Sintaks dasar:
60
IF
Proses penyeleksian berdasarkan kondisi relasi
tertentu.
Bentuk if Tunggal
IF,ELSE
Untuk melakukan beberapa operasi yang berbeda
jika salah satu ekspresi kondisional bernilai salah,
maka digunakan statement else. Bentuk if-else
memungkinkan dua alternatif operasi pemrosesan.
Sintaks dasar:
62
CONTOH
66
SWITCH
Switch adalah pernyataan yang digunakan untuk
menjalankann salah satu pernyataan dari beberapa
kemungkinan statement untuk dieksekusi,
berdasarkan nilai dari sebuah ungkapan dan nilai
penyeleksi. Setiap ungkapan diungkapkan dengan
sebuah nilai integral konstan, seperti sebuah nilai
dengan tipe byte, short, int atau char.
67
SWITCH
Proses penyeleksian berdasarkan kondisi nilai suatu variabel.
Instruksi yang dijalankan sesuai
dengan nilai pada case yang telah
ditentukan. Apabila tidak ada, maka
intruksi pada default yang akan
dijallankan.
68