Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 48

Pseudocode & Flowchart

Algorithm & Programming


PHASE IN PROGRAMMING

 Type tugas pemrograman dapat dibagi menjadi dua


tahap :
 Desain / fase Pemecahan masalah
 memerintahkan untuk menghasilkan urutan langkah-
langkah yang menggambarkan solusi dari masalah
 urutan langkah-langkah yang disebut algoritma
 Tahap implementasi
 melaksanakan program dalam beberapa bahasa
pemrograman
Problem Solving Coding
Steps in Problem Solving

 Pertama menghasilkan algoritma umum (dapat


menggunakan pseudocode)
 Kembangkan algoritma berturut-turut untuk mendapatkan
langkah demi langkah algoritma rinci yang sangat dekat
dengan bahasa komputer.
 Pseudocode adalah bahasa buatan dan informal yang
membantu programmer mengembangkan algoritma.
 Pseudocode sangat mirip dengan bahasa Inggris sehari-hari.
Tidak ada aturan tata bahasa (grammar) dalam pseudocode.
 Pseudocode not case sensitive.
Pseudocode

Algorithm to Convert base-10 number to other bases

While ( the quotient is not zero )


Divide the decimal number by the new base
Make the remainder the next digit to the left in the answer
Replace the original decimal number with the quotient

5
Following Pseudocode
While ( the quotient is not zero )
Divide the decimal number by the new base
Make the remainder the next digit to the left in the answer
Replace the original decimal number with the quotient

What is 93 in base 8?
93/8 gives 11 remainder 5
11/8 gives 1 remainder 3
1/ 8 gives 0 remainder 1
answer 135

6
Pseudocode for Complete
Computer Solution
Write "Enter the new base"
Read newBase
Write "Enter the number to be converted"
Read decimalNumber
Set quotient to 1
While (quotient is not zero)
Set quotient to decimalNumber DIV newBase
Set remainder to decimalNumber REM newBase
Make the remainder the next digit to the left in the answer
Set decimalNumber to quotient
Write "The answer is "
Write answer
7
Pseudocode Functionality
Variables
Names of places to store values
quotient, decimalNumber, newBase

Assignment
Storing the value of an expression into a
variable
Set quotient to 64
quotient <-- 64
8
quotient <-- 6 * 10 + 4
Pseudocode Functionality
Output
Printing a value on an output device
Write, Print
Input
Getting values from the outside word and storing them into
variables
Get, Read

9
Pseudocode Functionality

Repetition
Repeating a series of statements
Set count to 1
While ( count < 10)
Write "Enter an integer number"
Read aNumber
Write "You entered " + aNumber
Set count to count + 1

How many values were read?

10
Pseudocode Functionality

Selection
Making a choice to execute or skip a statement (or
group of statements)
Read number
If (number < 0)
Write number + " is less than zero."
or
Write "Enter a positive number."
Read number
If (number < 0)
Write number + " is less than zero."
11
Write "You didn't follow instructions."
Pseudocode Functionality

Selection
Choose to execute one statement (or group of
statements) or another statement (or group of
statements)
If ( age < 12 )
Write "Pay children's rate"
Write "You get a free box of popcorn"
else If ( age < 65 )
Write "Pay regular rate"
else
Write "Pay senior citizens rate"
12
Pseudocode & Algorithm

 Example 1: Tulis algoritma untuk menentukan nilai


akhir siswa dan menunjukkan apakah itu lulus atau
gagal. Nilai akhir dihitung sebagai rata-rata dari empat
Komponen
Pseudocode & Algorithm

Pseudocode:
 Input a set of 4 komponen
 Hitung rata-rata mereka dengan menjumlahkan
dan membaginya dengan 4
 if rata2 dibawah 50
Print “GAGAL”
else
Print “LOLOS”
Pseudocode & Algorithm

 Detailed Algorithm
 Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
The Flowchart

 (Dictionary) Sebuah representasi skematis dari urutan


operasi, seperti dalam proses manufaktur atau program
komputer.
 (Technical) Sebuah representasi grafis dari urutan operasi
dalam suatu sistem informasi atau program. Diagram alur
sistem informasi menunjukkan bagaimana data mengalir
dari dokumen sumber melalui komputer untuk distribusi
akhir kepada pengguna. Flowchart Program menunjukkan
urutan instruksi dalam satu program atau subroutine.
Simbol yang berbeda digunakan untuk setiap jenis
flowchart.
The Flowchart

A Flowchart
 menunjukkan logika algoritma
 menekankan langkah-langkah individu dan interkoneksi
secara keseluruhan
 Misal : aliran kontrol dari satu tindakan ke depan
Flowchart Symbols
Basic
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program


Example
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<50

PRINT PRINT
“PASS” “FAIL”

STOP
Example 2

 Tulis algoritma dan menggambar flowchart untuk


mengkonversi panjang 1 kaki untuk sentimeter.
Pseudocode:
 Masukkan panjang 1 kaki (Lft)
 Hitung panjang dalam cm (Lcm) dengan mengalikan
LFT dengan 30
 Panjang Cetak dalam cm (LCM)
Example 2
Flowchart

START

Algorithm
Input
 Step 1: Input Lft Lft

 Step 2: Lcm  Lft x 30


Lcm  Lft x 30
 Step 3: Print Lcm
Print
Lcm

STOP
Example 3

Tulis algoritma dan gambar flowchart yang akan


membaca dua sisi persegi panjang dan
menghitung wilayahnya.
Pseudocode :
 Masukan lebar (W) dan Panjang (L) persegi
panjang
 Hitung luas (A) dengan mengalikan L dengan W
 Cetak A
Example 3
Algorithm START
 Step 1: Input W,L
 Step 2: AL x W Input
W, L
 Step 3: Print A
ALxW

Print
A

STOP
Example 4
 Tulis algoritma dan gambar flowchart yang akan
menghitung akar persamaan kuadrat
ax 2  bx  c  0
 Hint: d = sqrt (b 2  4ac),
and the roots are:
x1 = (–b + d)/2a and
x2 = (–b – d)/2a
Example 4

Pseudocode:
 Input the coefficients (a, b, c) of the quadratic equation
 Calculate d
 Calculate x1
 Calculate x2
 Print x1 and x2
Example 4
START

 Algorithm:
 Step 1: Input a, b, c Input
a, b, c
 Step 2: d  sqrt ( b  b  4 ) a  c
 Step 3: x1  (–b + d) / (2 x a) d  sqrt(b x b – 4 x a x c)
 Step 4: x2  (–b – d) / (2 x a)
x1 (–b + d) / (2 x a)
 Step 5: Print x1, x2
X2  (–b – d) / (2 x a)

Print
x1 ,x2

STOP
DECISION STRUCTURES

 Ekspresi A> B adalah ekspresi logika


 menggambarkan suatu kondisi yang kita ingin uji
 jika A> B adalah true (jika A lebih besar dari B)
kita mengambil tindakan di sebelah kiri
 mencetak nilai A
 jika A> B adalah false (jika A tidak lebih besar
dari B) kita mengambil tindakan di sebelah kanan
 mencetak nilai B
DECISION STRUCTURES

Y N
is
A>B

Print A Print B
IF–THEN–ELSE STRUCTURE

 The structure is as follows


If condition then
true alternative
else
false alternative
endif
IF–THEN–ELSE STRUCTURE

 The algorithm for the flowchart is as follows:


If A>B then
Y N
print A is
else A>B

print B
endif Print A Print B
Relational Operators
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
 Greater than or equal to
 Less than or equal to
 Not equal to
Example 5

 Tulis algoritma yang membaca dua nilai, menentukan


nilai terbesar dan mencetak nilai terbesar dengan
pesan yang dapat mengidentifikasi.
 ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
endif
Step 3: Print “The largest value is”, MAX
Example 5
START

Input
VALUE1,VALUE2

Y is
N
VALUE1>VALUE2

MAX  VALUE1 MAX  VALUE2

Print
“The largest value is”, MAX

STOP
NESTED IFS

 Salah satu alternatif dalam pernyataan IF-THEN-ELSE


 mungkin melibatkan lebih lanjut pernyataan IF-THEN-
ELSE
Example 6

 Write an algorithm that reads three numbers and


prints the value of the largest number.
Example 6
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX  N1 [N1>N2, N1>N3]
else
MAX  N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX  N2 [N2>N1, N2>N3]
else
MAX  N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
Example 6

 Flowchart: Draw the flowchart of the above


Algorithm.
Example 7
 Write and pseudo code and draw a flowchart to
a) read an employee name (NAME), overtime hours worked
(OVERTIME), hours absent (ABSENT) and
b) determine the bonus payment (PAYMENT).
Example 7
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

>40 hours $50


>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT  50
else if (OVERTIME–(2/3)*ABSENT > 30) then
PAYMENT  40
else if (OVERTIME–(2/3)*ABSENT > 20) then
PAYMENT  30
else if (OVERTIME–(2/3)*ABSENT > 10) then
PAYMENT 20
else
PAYMENT  10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
Example 7

 Flowchart: Draw the flowchart of the above


algorithm?
Example 8

 Dalam suatu perhitungan nilai P=X+Y. Jika P positif,


maka Q=X*Y, sedangkan jika negatif maka nilai Q=X/Y.
 Buatlah flowchart untuk mencari nilai P dan Q!
Example 9

 Buatlah Flowchart untuk menampilkan bilangan


ganjil dan genap, dengan inputannya adalah bilangan
yang akan dicek !
Example 10

 Sebuah usaha fotokopi mempunyai aturan sebagai berikut :


 Jika yang fotokopi statusnya adalah langganan, maka berapa
lembar pun dia fotokopi, harga perlembarnya Rp. 75,-
 Jika yang fotokopi bukan langganan, maka jika dia fotokopi
kurang dari 100 lembar harga perlembarnya Rp. 100,-.
Sedangkan jika lebih atau sama dengan 100 lembar, maka
harga perlembarnya Rp. 85,-.
 Buat flowchart untuk menghitung total harga yang harus
dibayar jika seseorang memfotokopi sejumlah X lembar.
Example 11

 Buatlah Flowchart untuk menentukan nilai ratusan,


puluhan dan satuan dari sebuah bilangan.
 Misalnya bilangan yg dimasukkan : 125
 Maka outputnya :
 1 ratusan
 2 puluhan
 5 satuan
Example 12

 Tentukanlah bilangan prima dari bilangan 1-100

 Tentukanlah bilangan ganjil dari 1-100

 Tentukanlah bilangan genap dari 1 - 100


Example 13

 Buatlah flowchart untuk mengkonversi nilai mata


uang asing (dari rupiah) dengan pilihan sebagai
berikut :
 Pilihan 1 : mata uang Dollar (1$ = 13.696 rupiah)
 Pilihan 2 : mata uang Jepang (1 yen = 131 rupiah)
 Pilihan 3 : mata uang (1 Won = 12,9 rupiah)

You might also like