Anda di halaman 1dari 91

Modul ke: Algoritma dan

07 Fakultas
Pemrograman
Ilmu
Komputer
Program Studi
Teknik
Informatika Muhammad Rifqi, S.Kom, M.Kom

Pembuka Daftar Pustaka Akhiri Presentasi


Memilih bilangan yang
nilainya terbesar atau
terkecil dari dua buah
bilangan yang ada
Soal-8.
Susun algoritma yang ditulis dalam
Bahasa C++, untuk menginput dua buah
bilangan dan mencetak salah satu
bilangan yang nilainya terbesar
7
5
cout
cin 7 5 7
A B
dapat ditulis setelah

dan konsep atau draf


secara manual sudah
dibuat
Draf
KONSEP penyelesaian MENCARI YANG TERBESAR

Misal yang diinput nilai : 7 dan 5

7 + 5 = 12 12 + 2 = 14 14 / 2 = 7
7–5=2 |2|=2
7 terbesar

Misal yang diinput nilai : 4 dan 9

4 + 9 = 13 13 + 5 = 18 18 / 2 = 9
4–9=-5 |-5|=5
9 terbesar
Draf
KONSEP penyelesaian MENCARI YANG TERKECIL

Misal yang diinput nilai : 7 dan 5

7 + 5 = 12 12 - 2 = 10 10 / 2 = 5
7–5=2 |2|=2
5 terkecil

Misal yang diinput nilai : 4 dan 9

4 + 9 = 13 13 - 5 = 8 8/2=4
4–9=-5 |-5|=5
4 terkecil
Draf
KONSEP penyelesaian MENCARI YANG TERBESAR

A 5
C
C=A + B
14 F
1
F = C+E
18
4
E = abs(D)
D=A - B 4
-4
2 3 5 G=F/2
D E
B 9 1 C=A+B
G 9
2 D=A–B
3 E = abs( D )
4 F=C+E terbesar
5 G=F/2
A 5 copy dari halaman sebelumnya 1 C=A+B
2 D=A–B
C
C=A+B F 3 E = abs( D )
1 14 F = C+E 4 F=C+E
18 5 G=F/2
4
D=A- B E = abs(D)
-4 4 5 G=F/2
2 3
D E G 9
B 9

disingkat terbesar

A 5
C
1 C=A+B
C=A+B G
14 2 E = abs( A-B )
1 G = ( C+E ) / 2
9 3 G = (C+E) / 2
3
E = abs( A – B ) 4 terbesar
2
E
B 9
copy dari halaman sebelumnya
A 5
C
C=A+B G
14
G = ( C+E ) / 2
9
E = abs( A – B ) 4 terbesar

E 1 C=A+B
B 9 2 E = abs( A-B )
disingkat 3 G = (C+E) / 2

A 5

G
G = ( (A+B) + abs( A – B ) ) / 2
9
terbesar

9 G = ( (A+B) + abs( A – B ) ) / 2
B
Algoritma yang
ditulis dalam suatu
Bahasa
Pemrograman
disebut PROGRAM
A 5 1 C=A+B
2 D=A–B
C
C=A+B F 3 E = abs( D )
1 14 F = C+E 4 F=C+E
18 5 G=F/2
4
D=A- B E = abs(D)
-4 4 5 G=F/2
2 3
D E G 9
B 9
#include <iostream.h>
#include<math.h> terbesar
void main()
{ int A,B,C,D,E,F,G;
cin >> A >> B;
C = A + B; Karena menggunakan fungsi
D = A – B; matematik abs( )
E = abs( D ); maka file math.h yang
F = C + E; mengandung fungsi abs( )
G = F / 2; harus di-include-kan
cout << G;
}
A 5
C
C=A+B G
14
1 G = ( C+E ) / 2
9
3
E = abs( A – B ) 4 terbesar
2
E 1 C=A+B
B 9 2 E = abs( A-B )
#include <iostream.h> 3 G = (C+E) / 2
#include<math.h>
void main()
{ int A,B,C,E,G;
cin >> A >> B;
C = A + B;
E = abs( A - B );
G = ( C + E ) / 2;
cout << G;
}
A 5

G
G = ( (A+B) + abs( A – B ) ) / 2
9
terbesar

9 G = ( (A+B) + abs( A – B ) ) / 2
B

#include <iostream.h>
#include<math.h>
void main()
{ int A,B,G;
cin >> A >> B;
G = ( (A+B) + abs( A – B ) ) / 2;
cout << G;
}
#include <iostream.h>
#include<math.h>
void main() #include <iostream.h>
{ int A,B,C,D,E,F,G; #include<math.h>
cin >> A >> B; void main()
C = A + B; { int A,B,C,E,G;
D = A – B; cin >> A >> B;
E = abs( D ); C = A + B;
F = C + E; E = abs( A - B );
G = F / 2; G = ( C + E ) / 2;
cout << G; cout << G;
} }

#include <iostream.h>
#include<math.h>
void main()
{ int A,B,G;
cin >> A >> B;
G = ( (A+B) + abs( A – B ) ) / 2;
cout << G;
}
Memilih bilangan
terbesar atau terkecil
dari dua buah bilangan
menggunakan

Control statement

if
Syntax Penulisan if statement:

if( condition )
Condition : adalah ungkapan atau pernyataan (expression)
yang mengandung nilai TRUE (benar) atau
FALSE (salah).

Contoh : 5 > 2 bernilai TRUE

5 > 9 bernilai FALSE

5 == 5 bernilai TRUE

Tanda : > dan == diatas


disebut Relational Operator
(Operator Hubungan)
Relational Operator
yang digunakan dalam Bahasa C/C++ dan Java

== Equal To (Sama Dengan)


> Greater Than (Lebih Besar Dari)
< Less Than (Lebih Kecil Dari)
>= Greater Than Or Equal To
(Lebih Besar Dari Atau Sama Dengan)
<= Less Than Or Equal To
(Lebih Kecil Dari atau Sama Dengan)
!= Not Equal To ( Tidak Sama Dengan)
Sebutkan TRUE atau FALSE nilai condition berikut ini :

5 == 2 contoh : if( 5 == 2 )
5>2
5<2
5 >= 2
5 <= 2
5 >= 5
5 <= 5
5 != 2
5 != 5
5<7
5 <= 7
5 >= 7
5 == 2 F
5>2 T
5<2 F
5 >= 2 T
5 <= 2 F
5 >= 5 T
5 <= 5 T
5 != 2 T
5 != 5 F
5<7 T
5 <= 7 T
5 >= 7 F
5 7 9 7 7 7
A B A B A B

akan akan akan


condition bernilai condition bernilai condition bernilai

(A == B) F (A == B) F (A == B) T

(A > B) F (A > B) T (A > B) F

(A < B) T (A < B) F (A < B) F

(A >= B) F (A >= B) T (A >= B) T

(A <= B) T (A <= B) F (A <= B) T

(A != B) T (A != B) T (A != B) F

T = TRUE
F = FALSE
Bentuk Umum - 1
biasa disebut : Bentuk IF-THEN Flowchart

if ( cond )
TRUE
{ - cond
- statements-true
-
- - statements-
- -
- true
} -
-
-
- next instruction
-
-
- next instruction
Cara-Kerja -
-
-
Bila nilai cond
- TRUE, maka kerjakan semua instruksi yang ada dalam statements-true
Setelah selesai, lanjutkan ke next-instruction

- FALSE, maka langsung ‘meloncat’ mengerjakan isnstruksi yang ada di


next-instruction
Flowchart
if ( cond )
{ -
- Statements
- true TRUE cond
- cond
-
} TRUE
-
-
- next -
- -
- - -
- instruction - -
-
-
-

Penulisan PROGRAM - -
- -
Terikat aturan / ketentuan - -
- -

Penggambaran FLOWCHART
bebas
Bentuk Umum - 2
biasa disebut : Bentuk IF-THEN-ELSE
Flowchart
if ( cond )
{ -
- statements-true
- FALSE TRUE
- cond
-
}
else - statements- - statements-
- -
- false - true
{ - -
-
-
-
- - -
- statements-false
-
-
} - next instruction
-
- next instruction -
- -
-

cond = condition
if ( cond ) Flowchart
{ -
- statements-true
-
-
} - FALSE TRUE
else cond
{ -
- statements-false
- -
-
statements- -
-
statements-
- - false - true
} -
- -
- -
- -
- next instruction
-
-
- next instruction
Cara-Kerja -
-
-
Bila nilai cond
- TRUE, maka kerjakan semua instruksi yang ada dalam statements-true
Setelah selesai, lanjutkan ke next-instruction

- FALSE, maka kerjakan semua instruksi yang ada dalam statements-false


Setelah selesai, lanjutkan ke next-instruction
Penulisan PROGRAM terikat aturan,
Penggambaran FLOWCHART bebas.

FALSE
TRUE FALSE cond
cond
TRUE
-
- - - -
- - - false
- true - false true - -
- - - -
- - - -
- - -
-

- next
- next instruction - instruction
- -
- -
-
Beberapa contoh penulisan / penggunaan if(cond) bentuk IF-THEN-ELSE

int A=5, B=7;


1. if(A<B) 2. int A=5, B=7;
{ if(A<B)
cout << “Jakarta” ; {cout << “Jakarta” ;
} }
else else
{ {cout << “Bandung” ;
cout << “Bandung” ; }
} cout << “\nSelesai” ;
cout << “\nSelesai” ; Akan tercetak : Jakarta
Akan tercetak : Jakarta Selesai
Selesai

3. int A=5, B=7; int A=5, B=7;


if(A<B) 4.
if(A<B){cout << “Jakarta” ; }
{ cout << “Jakarta” ; }
else else {cout << “Bandung” ;}
{ cout << “Bandung” ; } cout << “\nSelesai” ;
cout << “\nSelesai”); Akan tercetak : Jakarta
Akan tercetak : Jakarta Selesai
Selesai
Soal Susun program dalam bahasa C++
untuk menginput 2 (dua) buah bilangan
bulat, kemudian mencetak salah satu
bilangan yang nilainya terbesar.
Soal-9
Susun program dalam bahasa C++
untuk menginput 2 (dua) buah bilangan bulat,
kemudian mencetak salah satu bilangan yang
nilainya terbesar.

MISAL NILAI YANG DIINPUT ADALAH 5 DAN 2

5
2
cin cout
5 2 5
A B
START
Algoritma Dasar
Read(A) Read A

Read(B)
IF A > B then Read B

Write(A)
Else False
A>B
True
Write(B)
End If Write B write A

END
Algoritma dalam Bahasa C++ START

Cara-1
input
#include <iostream.h> A

void main()
{ int A, B; input B
cin >> A;
cin >> B; AB False True AB
A>B
if (A > B)
{ cout << A;
print B ptint A
}
else
{ cout << B;
} END
}
FLOWCHART START
Cara-1

cin A

cin B

AB False True AB
A>B

cout B cout A

END
#include <iostream.h> if (A > B)
void main() { cout << A; }
{ int A, B; else
cin >> A; { cout << B; }
cin >> B; }
if (A > B)
{ cout << A; } if (A > B)
else cout << A;
{ cout << B; } else
} cout << B;
}

if (A > B) cout << A;


else cout << B;

if (A > B) cout << A; else cout << B;


if (A > B)
cout << A ;
else
cout << B ;

if (A < B)
cout << ….. ;
else
cout << ….. ;

if (B > A)
cout << …... ;
else
cout << …... ;

if (B < A)
cout << …... ;
else
cout << …... ;
if (A > B) TEKNIK
cout << A ; DASAR
else
cout << B ;

if (A < B)
cout << B ;
else
cout << A ;

if (B > A)
cout << …... ;
else
cout << …... ;

if (B < A)
cout << …... ;
else
cout << …... ;
if (A > B) TEKNIK
cout << A ; DASAR
else
cout << B ;

if (A < B)
cout << B ;
else
cout << A ;

if (B > A)
cout << B ;
else
cout << A ;

if (B < A)
cout << …... ;
else
cout << …... ;
if (A > B) TEKNIK
cout << A ; DASAR
else
cout << B ;

if (A < B)
cout << B ;
else
cout << A ;

if (B > A)
cout << B ;
else
cout << A ;

if (B < A)
cout << A ;
else
cout << B ;
TEKNIK
Cara-2 DASAR
#include <iostream.h> input A
void main()
{ int A, B, MAX; input B
cin >> A;
cin >> B; false true
A>B
if (A > B)
{ MAX = A; }
else MAX = B MAX = A
{ MAX = B; }
cout << MAX;
}
print MAX
Dengan cara menyimpan bilangan yang terbesar kedalam
sebuah variabel misal namanya MAX

5
2

cin
5 2

KEYBOARD A B cout
5 5
MAX

SCREEN
MEMORY
TEKNIK
Cara-3 DASAR
#include <iostream.h> input A
void main()
input
{ int A, B, MAX; B

cin >> A;
MAX = B
cin >> B;
MAX = B;
true
if (A > B) A>B

{ MAX = A; } MAX = A
cout << MAX;
} print MAX
START TEKNIK
Cara-4 DASAR
input A
#include <iostream.h>
void main() MAX = A

{ int A, B, MAX;
input B
cin >> A;
MAX = A True
B>MAX
cin >> B;
if (B > MAX) False MAX = B
{ MAX = B; }
cout << MAX;
}
print MAX

END
Cara-4 START TEKNIK
#include <iostream.h> DASAR
input A
void main() Teknik
{ int A, MAX; inilah yang
MAX = A
cin >> A; mendasari
algoritma untuk
MAX = A mencari nilai yang
cin >> A; input A terbesar atau
if (A > MAX) terkecil dari
True sejumlah nilai
{ MAX = A; } A>MAX
yang diinput
cout << MAX;
False MAX = A
}

A MAX

Hanya ada 2 variabel. print


A spesial menampung input, MAX
MAX spesial menampung nilai
terbesar
END
Urutan pelaksanaan : misal nilai yang
A MAX diinput adalah
5 dan 2
1) int A,MAX;

2) cin >> A; 5

3) MAX = A; 5 5

4) cin A; 2 5

5) if (A>max)
MAX = A; 2 5

6) cut << MAX; 5


Urutan pelaksanaan : misal nilai yang
A MAX diinput adalah
A
5 dan 9
1) int A,MAX;

2) cin >> A; 5

3) MAX = A; 5 5

4) cin A; 9 5

5) if (A>max)
MAX = A; 9 9

6) cut << MAX; 9


Susun program untuk menginput 3 (tiga)
Soal
buah bilangan bulat, (dianggap ketiga
buah bilangan tersebut nilainya tidak
sama satu dengan yang lain), kemudian
mencetak salah satu bilangan yang
nilainya terbesar.
Susun program untuk menginput 3 (tiga) buah bilangan
Soal bulat, (dianggap ketiga buah bilangan tersebut nilainya
tidak sama satu dengan yang lain), kemudian mencetak
salah satu bilangan yang nilainya terbesar.

5
8
17

cin cout
5 17 8 17

A B C
KEYBOARD
SCREEN

MEMORY
5
8
17

cin cout
17 5 8 17

A B C
KEYBOARD
SCREEN

MEMORY
5
8
17

cin cout
5 8 17 17

A B C
KEYBOARD
SCREEN

MEMORY
START

input A
A B C
Menggunakan 3 variabel
input B

input C

False True
?

Apa yang dibandingkan


untuk pertama kali :
A dengan B
B dengan A
A dengan C
C dengan A
B dengan C
C dengan B
START

input A
A B C
Menggunakan 3 variabel
input B

input C

False True
?

A>B B>A C>A Pilih yang mudah


A<B B<A C<A
A>C B>C C>B
A<C B<C C<B
START

input A
A B C
Menggunakan 3 variabel
input B

input C

AB AB
False True
A>B
START

input A
A B C
Menggunakan 3 variabel
input B

input C

AB AB
False True
A>B

Mencetak
atau
masih membandingkan
?
START

input A
A B C
Menggunakan 3 variabel
input B

input C

AB AB
False True
A>B

False True
?
Pilih yang A>C
mana C>A

?
B>C
C>B
START

input A
A B C
Menggunakan 3 variabel
input B

input C

AB AB
False True
A>B

False True
?
Pilih yang mana saja
tidak mendatangkan A>C
error. Hanya saja C>A
secara logika, mana B>C
yang tepat untuk dipilih C>B
START

input A Dari ilustrasi ini


A B C terlihat jelas
Menggunakan 3 variabel bahwa A yang
input B harus
dibandingkan
dengan C untuk
input C mencari nilai
yang terbesar
AB AB
False True
A>B

False True
?
A>C
C>A
B>C
C>B
START

input A
A B C
Menggunakan 3 variabel
input B

input C

AB AB
False True
A>B

False True
A>C

Mencetak
atau
masih
membandingkan
?
START

input A
A B C
Menggunakan 3 variabel
input B

input C
Kondisi bila
AB AB A > C true
False True
A>B

atau
False True
A>C ABC ABC

Mencetak
atau
masih
membandingkan
?
START

input A
A B C
Menggunakan 3 variabel
input B

input C
Kondisi bila
AB AB A > C true
False True
A>B

atau
False True
A>C ABC ABC

Bisakah diambil
kesimpulan
bahwa A yang
terbesar
?
START

input A
A B C
Menggunakan 3 variabel
input B

input C
Kondisi bila
AB AB A > C true
False True
A>B

atau
False True
A>C ABC ABC

print A
START

input A
A B C
Menggunakan 3 variabel
input B

input C

AB AB
False True
A>B

False True
A>C

print A
START

input A
A B C
Menggunakan 3 variabel
input B

input C

AB AB
False True
A>B

False True
A>C

print A

Atau
Perlukah
Dapatkah
membandingk
diambil
an B dengan
kesimpulan
C disini
bahwa C
?
adalah yang
START

input A
A B C
Menggunakan 3 variabel
input B

input C

AB AB
False True
A>B

Kondisi bila
A > C False False True
A>C
Bila A > C False
artinya
A<C print A
Atau C > A
ABC

Atau
Perlukah
Dapatkah
membandingk
diambil
an B dengan
kesimpulan
C disini
bahwa C
?
adalah yang
START

input A
A B C
Menggunakan 3 variabel
input B

input C

AB AB
False True
A>B

False True
A>C

print A
ABC

Atau
Perlukah
Dapatkah
membandingk
diambil
an B dengan
kesimpulan
C disini
bahwa C
?
adalah yang
START

input A
A B C
Menggunakan 3 variabel
input B

input C

atau
AB AB
False True
A>B ABC ABC

False True
A>C

print A
ABC

Atau
Perlukah
Dapatkah
membandingk
diambil
an B dengan
kesimpulan
C disini
bahwa C
?
adalah yang
START

input A
A B C
Menggunakan 3 variabel
scanf B

input C

atau
AB AB
False True
A>B
ABC ABC

False True
A>C

print C print A
ABC
START

input A
A B C
Menggunakan 3 variabel
input B

input C

atau
AB AB
False True
A>B
ABC ABC

False True
A>C

ABC
B>C print A
Pasti
Kesini
B>C pasti False
Tidak
Membandingkan sesuatu yang pasti Pasti Pernah
False atau True adalah pekerjaan yang False kesini
tidak bermanfaat
START

input A
A B C
Menggunakan 3 variabel
input B

input C

atau
AB AB
False True
A>B
ABC ABC

False True
A>C

print C print A
ABC
START

input A

input B

input C

atau
AB AB
False True
A>B
ABC ABC

False True
A>C

print C print A
ABC
START

input A

input B

input C

atau
AB AB
False True
A>B
ABC ABC

False True
A>C

print C print A
Mencetak
atau
masih membandingkan
?
START

input A

input B

input C

atau
AB AB
False True
A>B
ABC ABC

False True False True


? A>C

A>C print C print A


C>A
B>C
C>B
START

input A

input B

B yang besar, input C


jadi B yang
dibandingkan atau
dengan C AB AB
False True
A>B
ABC ABC

False True False True


B>C A>C

print C print A
START

input A

input B

input C

atau
AB AB
False True
A>B
ABC ABC

False True False True


B>C A>C

print B print C print A


START

input A

input B

input C

atau
AB AB
False True
A>B
ABC ABC

False True False True


B>C A>C

ABC
print B print C print A
START

input A

input B

input C

atau
AB AB
False True
A>B
ABC ABC

False True False True


B>C A>C

ABC
print C print B print C print A
START

input A

input B

input C

atau
AB AB
False True
A>B
ABC ABC

False True False True


B>C A>C

ABC
print C print B print C print A
START

input A

input f B

input C

atau
AB AB
False True
A>B
ABC ABC

False True False True


B>C A>C

ABC
print C print B print C print A
AB AB
False True
A>B

False True False True


B>C A>C

print C print B print C print A

atau atau

ABC ABC ABC ABC ABC ABC


Cara-1
START
#include <iostream.h>
void main()
{ input
int A,B,C; A
cin >> A;
cin >> B);
cin >> C); input B
if(A > B)
{
if(A > C) input C
cout << A ;
else false true
cout << C ; A>B
}
else false true false true
B>C A>C
{
if(B > C)
cout << B ;
else
cout << C ; print C print B print C print A
}
}

A B C
Menggunakan 3 variabel END
Karena setiap blok instruksi hanya terdiri dari satu
instruksi, maka program diatas dapat ditulis menjadi :

#include <iostream.h>
void main()
{
int A,B,C; A B C
cin >> A ;
cin >> B ;
cin >> C ; Menggunakan 3 variabel

if (A > B)
if (A > C)
cout << A ;
else
cout << C ;
else
if (B > C)
cout << B ;
else
cout << C);
}
Daftar Pustaka
Thomas Connoly, Carolyn Begg. 2010, “Database System: APractical
Approach to Design Implementation and Management”, 4th Edition,
Addison Wesley
S.K. Singh. 2011. “Database System: Concepts, Design and Application”
Abraham Silberschatz, Henry F. Korth. Database System Concepts 3rd
Edition. 1999.
Raghu Ramakrisnan, Gherke. Database Management System, 3rd Edition.
McGraw-Hill.2001
Terima Kasih
Terima Kasih
Muhammad Rifqi, S.Kom, M.Kom

Anda mungkin juga menyukai