Algprog 7
Algprog 7
07 Fakultas
Pemrograman
Ilmu
Komputer
Program Studi
Teknik
Informatika Muhammad Rifqi, S.Kom, M.Kom
7 + 5 = 12 12 + 2 = 14 14 / 2 = 7
7–5=2 |2|=2
7 terbesar
4 + 9 = 13 13 + 5 = 18 18 / 2 = 9
4–9=-5 |-5|=5
9 terbesar
Draf
KONSEP penyelesaian MENCARI YANG TERKECIL
7 + 5 = 12 12 - 2 = 10 10 / 2 = 5
7–5=2 |2|=2
5 terkecil
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).
5 == 5 bernilai TRUE
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
(A == B) F (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
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
TRUE FALSE cond
cond
TRUE
-
- - - -
- - - false
- true - false true - -
- - - -
- - - -
- - -
-
- next
- next instruction - instruction
- -
- -
-
Beberapa contoh penulisan / penggunaan if(cond) bentuk IF-THEN-ELSE
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 << ….. ;
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
2) cin >> A; 5
3) MAX = A; 5 5
4) cin A; 2 5
5) if (A>max)
MAX = A; 2 5
2) cin >> A; 5
3) MAX = A; 5 5
4) cin A; 9 5
5) if (A>max)
MAX = A; 9 9
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
?
input A
A B C
Menggunakan 3 variabel
input B
input C
False True
?
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
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
input A
input B
print C print A
START
input A
input B
input C
atau
AB AB
False True
A>B
ABC ABC
input A
input B
input C
atau
AB AB
False True
A>B
ABC ABC
ABC
print B print C print A
START
input A
input B
input C
atau
AB AB
False True
A>B
ABC ABC
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
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
ABC
print C print B print C print A
AB AB
False True
A>B
atau atau
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