Anda di halaman 1dari 14

PRAKTIKUM ALGORITMA PEMROGRAMAN

Oleh:
KELOMPOK I
1.

Ahmad Afifi

(1304505064)

2.

Ni Made Surasmitha Dewi

(1404505022)

3.

I Gusti Ngurah Yoga Pawitra

(1404505052)

4.

Pande Nengah Purnawan

(1404505064)

5.

Ni Ketut Pradani Gayatri

(1404505101)

JURUSAN TEKNOLOGI INFORMASI


FAKULTAS TEKNIK UNIVERSITAS UDAYANA
2015

KATA PENGANTAR
Om Swastyastu,
Puji syukur penulis haturkan kehadirat Tuhan Yang Maha Esa yang telah
melimpahkan rahmat dan karunia-Nya kepada penulis sehingga penulis dapat
menyelesaikan laporan algoritma pemrograman ini tepat pada waktunya. Laporan
algoritma pemrograman ini memberikan gambaran umum mengenai penggunaan
dasar bahasa pemrograman, fungsi dan prosedur, array dan pointer, struktur dan
kelas, serta operasi file pada setiap program yang dipraktikkan.
Terima kasih penulis sampaikan kepada Bapak Dr.Eng I Putu Bayupati,
S.T., M.T. selaku Dosen Pengampu Mata Kuliah Praktikum Algoritma
Pemrograman dan para asisten dosen praktikum algoritma pemrograman yang
membimbing penulis dalam penyusunan laporan akhir praktikum. Terima kasih
juga penulis sampaikan kepada semua pihak yang telah berperan dalam
penyusunan laporan algoritma pemrograman ini dari awal sampai akhir.
Penulis juga memohon maaf kepada seluruh pihak yang terkait bilamana
dalam menjalankan tugas dan pembuatan laporan praktikum ini terdapat banyak
kesalahan dan kekurangan. Penulis berharap laporan praktikum basis data ini
dapat bermanfaat bagi semua kalangan pembaca khususnya mahasiswa program
studi Teknologi Informasi Udayana.
Om Santih, Santih, Santih Om.

Badung, 6 Juni 2015

Penulis Laporan

DAFTAR ISI
KATA PENGANTAR .............................................................................................ii
DAFTAR ISI ..........................................................................................................iii
DAFTAR GAMBAR ..............................................................................................v
DAFTAR KODE PROGRAM ...............................................................................ix
DAFTAR TABEL .................................................................................................xii
DAFTAR TRACE ................................................................................................xiii
BAB I PENDAHULUAN ......................................................................................1
1.1
1.2
1.3
1.4
1.5
1.6

Latar Belakang ...............................................................................................1


Rumusan Masalah ..........................................................................................2
Tujuan ............................................................................................................2
Manfaat ..........................................................................................................3
Batasan Masalah ............................................................................................3
Sistematika Penulisan ....................................................................................4

BAB II TINJAUAN PUSTAKA ............................................................................5


2.1 Bahasa Pemrograman .....................................................................................5
2.2 Bahasa Pemrograman C++ ........................................................................... 5
2.3 Tipe Data dalam Bahasa C++ ....................................................................... 6
2.4 Flowchart ...................................................................................................... 9
2.5 Selection ........................................................................................................ 8
2.6 Repetition .....................................................................................................11
2.7 Variabel Global dan Variabel Lokal .............................................................14
2.8 Prosedur dan Fungsi .....................................................................................14
2.9 Pass by Value dan Pass by Reference ..........................................................15
2.10........................................................................................Rekursif dan Iteratif
.......................................................................................................................16
2.11...............................................................................................................Array
.......................................................................................................................17
2.12.............................................................................................................Pointer
.......................................................................................................................17
2.13.............................................................................................................Sorting
.......................................................................................................................17
2.14........................................................................................................Searching
.......................................................................................................................19
2.15...........................................................................................................Struktur
.......................................................................................................................20
2.16...............................................................................................................Kelas
.......................................................................................................................20
3

2.17..............................................................................................Tipe Data String


.......................................................................................................................21
2.18...............................................................................................Konversi String
.......................................................................................................................22
2.19.....................................................................................................Operasi File
.......................................................................................................................23
BAB III PEMBAHASAN ....................................................................................25
3.1 Dasar Penggunaan Bahasa Pemrograman ....................................................25
3.2 Fungsi dan Prosedur .....................................................................................54
3.3 Array dan Pointer .......................................................................................114
3.4 Struktur dan Kelas ......................................................................................168
3.5 Operasi File ................................................................................................184
BAB IV PENUTUP ...........................................................................................205
4.1 Simpulan ....................................................................................................205
4.2 Saran ..........................................................................................................206
DAFTAR PUSTAKA ..........................................................................................207
LAMPIRAN

DAFTAR GAMBAR
Gambar 2.1

Flowchart selection if .....................................................................9

Gambar 2.2

Flowchart if-else ...........................................................................10

Gambar 2.3

Flowchart switch ...........................................................................11

Gambar 2.4

Flowchart for ................................................................................12

Gambar 2.5

Flowchart while ............................................................................13

Gambar 2.6

Flowchart do-while .......................................................................13

Gambar 3.1

Flowchart program utama menghitung akar-akar persamaan


kuadrat ...........................................................................................26

Gambar 3.2

Flowchart prosedur akar() ............................................................26

Gambar 3.3

Hasil compile program menghitung akar-akar persamaan


kuadrat pada Dev C++ ..................................................................30

Gambar 3.4

Flowchart program mengonversi bilangan desimal ke biner


dan sebaliknya ...............................................................................31

Gambar 3.5

Hasil compile program mengonversi bilangan desimal ke biner


dan sebaliknya pada Dev C++ ......................................................38

Gambar 3.6

Flowchart program menentukan zodiak bintang ..........................40

Gambar 3.7

Hasil compile program menentukan zodiak bintang pada Dev C+


+ ....................................................................................................43

Gambar 3.8

Flowchart program menghitung nilai akhir mata kuliah ..............45

Gambar 3.9

Hasil compile program menghitung nilai akhir mata kuliah


pada Dev C++ ...............................................................................49

Gambar 3.10 Flowchart program menampilkan deret bilangan prima ..............50


Gambar 3.11 Hasil compile program menampilkan deret bilangan prima ........54
Gambar 3.32 Flowchart program utama menentukan deret bilangan
fibonacci ........................................................................................55
Gambar 3.13 Flowchart fungsi fibo_rekursif() ..................................................56
Gambar 3.14 Flowchart prosedur fibo_iteratif() ................................................56
Gambar 3.15 Hasil compile program menentukan deret bilangan fibonacci
pada Dev C++ ...............................................................................61
Gambar 3.16 Flowchart program utama menghitung langkah minimum pada
menara hanoi .................................................................................62
5

Gambar 3.17 Flowchart prosedur hanoi_rek() ...................................................63


Gambar 3.18 Flowchart prosedur hanoi_ite() ....................................................65
Gambar 3.19 Flowchart fungsi genap() ..............................................................66
Gambar 3.20 Flowchart fungsi counter() ...........................................................66
Gambar 3.21 Flowchart fungsi pilihan() ............................................................67
Gambar 3.22 Flowchart fungsi pil_disk() ..........................................................68
Gambar 3.23 Hasil compile menghitung langkah minimum pada menara
hanoi pada Dev C++ .....................................................................79
Gambar 3.24 Flowchart program utama menentukan faktor persekutuan
terbesar (FPB) ...............................................................................80
Gambar 3.25 Flowchart fungsi FPB_rek() .........................................................81
Gambar 3.26 Flowchart prosedur FPB_ite() ......................................................82
Gambar 3.27 Hasil compile program menentukan faktor persekutuan
terbesar (FPB) pada Dev C++ .......................................................86
Gambar 3.28 Flowchart program utama menghitung luas dan keliling
bangun datar ..................................................................................88
Gambar 3.29 Flowchart fungsi luas_per_panjang() ...........................................89
Gambar 3.29 Flowchart fungsi kll_per_panjang() .............................................89
Gambar 3.31 Flowchart fungsi luas_segi_sem() ................................................89
Gambar 3.32 Flowchart fungsi kll_segi_sem() ..................................................90
Gambar 3.33 Flowchart fungsi luas_lingkaran() ................................................90
Gambar 3.34 Flowchart fungsi luas_lingkaran() ................................................91
Gambar 3.35 Flowchart fungsi luas_trapesium() ...............................................91
Gambar 3.36 Flowchart fungsi kll_trapesium() .................................................91
Gambar 3.37 Flowchart fungsi luas_jajargenjang() ...........................................92
Gambar 3.38 Flowchart fungsi kll_jajargenjang() .............................................92
Gambar 3.39 Hasil compile program luas dan keliling bangun datar pada
Dev C++ ......................................................................................101
Gambar 3.40 Flowchart program utama menghitung volume dan luas
permukaan bangun ruang ............................................................103
Gambar 3.41 Flowchart prosedur balok() ........................................................104
Gambar 3.42 Flowchart prosedur bola() ..........................................................104
6

Gambar 3.43 Flowchart prosedur limas_seg_sama_sisi () ...............................105


Gambar 3.44 Flowchart prosedur prisma_segitiga() ........................................105
Gambar 3.45 Flowchart prosedur tabung() ......................................................106
Gambar 3.46 Hasil compile program menghitung volume dan luas
permukaan bangun ruang pada Dev C++ ....................................113
Gambar 3.47 Flowchart program utama sorting dan searching .......................116
Gambar 3.48 Flowchart prosedur fillArray() ...................................................117
Gambar 3.49 Flowchart prosedur inArray() .....................................................117
Gambar 3.50 Flowchart prosedur outArray() ...................................................118
Gambar 3.51 Flowchart prosedur insertionSort() .............................................118
Gambar 3.52 Flowchart prosedur bubbleSort() ................................................119
Gambar 3.53 Flowchart prosedur quickSort() ..................................................120
Gambar 3.54 Flowchart prosedur sequentialSearch() ......................................121
Gambar 3.55 Flowchart prosedur binarySearch() ............................................122
Gambar 3.56 Hasil compile program sorting dan searching pada Dev C++ ....136
Gambar 3.57 Flowchart program utama operasi matriks .................................137
Gambar 3.58 Flowchart prosedur input_matriks() ...........................................138
Gambar 3.59 Flowchart prosedur output_matriks() .........................................139
Gambar 3.60 Flowchart prosedur penjumlahan() .............................................140
Gambar 3.61 Flowchart prosedur transpose() ..................................................141
Gambar 3.62 Flowchart prosedur perkalian() ..................................................142
Gambar 3.63 Hasil compile program operasi matriks pada Dev C++ ..............150
Gambar 3.64 Flowchart program menghitung mean, median, dan modus ......151
Gambar 3.65 Flowchart prosedur bubbleSort() ................................................152
Gambar 3.66 Flowchart fungsi Mean() ............................................................153
Gambar 3.67 Flowchart fungsi Median() .........................................................153
Gambar 3.68 Flowchart prosedur Modus() ......................................................154
Gambar 3.69 Hasil compile program menghitung mean, median, dan modus
pada Dev C++ .............................................................................160
Gambar 3.70 Flowchart program utama perbandingan sorting melalui cara
biasa dan cara pointer .................................................................161

Gambar 3.71 Hasil compile program perbandingan sorting melalui cara


biasa dan cara pointer .................................................................168
Gambar 3.72 Flowchart program utama menghitung gaji harian ....................169
Gambar 3.73 Hasil compile program menghitung gaji harian pada Dev C++ . 172
Gambar 3.74 Flowchart program utama tamia .................................................173
Gambar 3.75 Flowchart fungsi setDaya() ........................................................174
Gambar 3.76 Flowchart fungsi setUkuranRoda() ............................................174
Gambar 3.77 Flowchart fungsi setLama() ........................................................174
Gambar 3.78 Flowchart fungsi setJarakTempuh() ...........................................175
Gambar 3.79 Flowchart fungsi setKecepatan() ................................................175
Gambar 3.80 Flowchart fungsi getDaya() ........................................................176
Gambar 3.81 Flowchart fungsi getUkuranRoda() ............................................176
Gambar 3.82 Flowchart fungsi getLama() .......................................................176
Gambar 3.83 Flowchart fungsi getJarakTempuh() ...........................................176
Gambar 3.84 Flowchart fungsi getKecepatan() ...............................................177
Gambar 3.85 Hasil compile program tamia pada Dev C++ ..............................183
Gambar 3.86 Flowchart program menentukan kata palindrom ........................186
Gambar 3.87 Hasil compile program menentukan kata palindrom pada Dev C+
+ ..................................................................................................192
Gambar 3.88 Flowchart program menyimpan data mahasiswa .......................193
Gambar 3.89 Hasil compile program menyimpan data mahasiswa pada Dev

C+

+ ..................................................................................................197
Gambar 3.90 Flowchart program mengurutkan nilai .......................................199
Gambar 3.91 Hasil compile program mengurutkan nilai pada Dev C++ .........203

DAFTAR KODE PROGRAM


Kode Program 2.1 Sintaks umum dari if ..............................................................8
Kode Program 2.2 Sintaks umum dari if-else .......................................................9
Kode Program 2.3 Sintaks umum switch ...........................................................10
Kode Program 2.4 Sintaks umum for .................................................................11
Kode Program 2.5 Sintaks umum while .............................................................12
Kode Program 2.6 Sintaks umum do-while ........................................................13
Kode Program 2.7 Contoh penggunaan variabel global .....................................14
Kode Program 2.8 Contoh penggunaan variabel lokal .......................................14
Kode Program 2.9 Sintaks umum dari prosedur ................................................15
Kode Program 2.10 Sintaks umum dari fungsi ....................................................15
Kode Program 2.11 Sintaks umum dari pass by value .........................................15
Kode Program 2.12 Sintaks umum dari pass by reference ...................................16
Kode Program 2.13 Sintaks umum array .............................................................17
Kode Program 2.14 Sintaks umum pointer ..........................................................17
Kode Program 2.15 Sintaks umum struct ............................................................20
Kode Program 2.16 Sintaks umum class ..............................................................20
Kode Program 2.17 Sintaks umum fungsi strlwr() ...............................................21
Kode Program 2.18 Sintaks umum fungsi strupr() ...............................................21
Kode Program 2.19 Sintaks umum dari all to string ............................................23
Kode Program 2.20 Sintaks umum untuk membuka file ......................................23
Kode Program 2.21 Sintaks umum untuk menulis file .........................................24
Kode Program 2.22 Sintaks umum untuk menutup file ......................................24
Kode Program 3.1 Pseudocode program menghitung akar-akar persamaan
kuadrat ..................................................................................27
Kode Program 3.2 Program menghitung akar-akar persamaan kuadrat .............28
Kode Program 3.3 Pseudocode program mengonversi bilangan desimal ke
biner dan sebaliknya ............................................................33
Kode Program 3.4 Program mengonversi bilangan desimal ke biner dan
sebaliknya ............................................................................35
Kode Program 3.5 Trace program menentukan zodiak bintang .........................41
Kode Program 3.6 Program menentukan zodiak bintang ...................................43
9

Kode Program 3.7 Pseudocode program menghitung nilai akhir mata kuliah. . .46
Kode Program 3.8 Program menghitung nilai akhir mata kuliah .......................47
Kode Program 3.9 Pseudocode program menampilkan deret bilangan prima ...51
Kode Program 3.10 Program menampilkan deret bilangan prima .......................52
Kode Program 3.11 Pseudocode program menentukan deret bilangan
fibonacci ...............................................................................57
Kode Program 3.12 Program menentukan deret bilangan fibonacci ....................59
Kode Program 3.13 Pseudocode program menghitung langkah minimum
pada menara hanoi ...............................................................71
Kode Program 3.14 Program menghitung langkah minimum pada menara
hanoi .....................................................................................74
Kode Program 3.15 Pseudocode program menentukan faktor persekutuan
terbesar (FPB) ......................................................................83
Kode Program 3.16 Program menentukan faktor persekutuan terbesar (FPB) ....85
Kode Program 3.17 Pseudocode program menghitung luas dan keliling
bangun datar .........................................................................94
Kode Program 3.18 Program menghitung luas dan keliling bangun datar ..........99
Kode Program 3.19 Pseudocode program menghitung volume dan luas
permukaan bangun ruang ...................................................107
Kode Program 3.20 Program menghitung volume dan luas permukaan
bangun ruang ......................................................................111
Kode Program 3.21 Pseudocode program sorting and searching ......................125
Kode Program 3.22 Program sorting dan searching ..........................................130
Kode Program 3.23 Pseudocode program operasi matriks ................................144
Kode Program 3.24 Program operasi matriks ....................................................148
Kode Program 3.25 Pseudocode program menghitug mean, median, dan
modus .................................................................................156
Kode Program 3.26 Program menghitung mean, median, dan modus ...............158
Kode Program 3.27 Pseudocode program perbandingan sorting melalui cara
biasa dan cara pointer ........................................................163
Kode Program 3.28 Program perbandingan sorting melalui cara biasa dan
cara pointer ........................................................................165
10

Kode Program 3.28 Pseudocode program menghitung gaji harian ....................170


Kode Program 3.29 Program menghitung gaji harian ........................................171
Kode Program 3.30 Pseudocode program tamia ................................................178
Kode Program 3.31 Program tamia ....................................................................181
Kode Program 3.32 Pseudocode program menentukan kata palindrom ............187
Kode Program 3.33 Program menentukan kata palindrom ................................188
Kode Program 3.34 Pseudocode program menyimpan data mahasiswa ............194
Kode Program 3.35 Program menyimpan data mahasiswa ................................195
Kode Program 3.36 Pseudocode program mengurutkan nilai ...........................200
Kode Program 3.37 Program mengurutkan nilai ................................................201

11

DAFTAR TABEL
Tabel 1.1 Sistematika penulisan ............................................................................4
Tabel 2.1 Tabel jenis tipe data ..............................................................................6
Tabel 2.2 Tabel macam-macam tipe data bilangan atau angka .............................6
Tabel 2.3 Tabel simbol-simbol dalam flowchart ..................................................7
Tabel 3.1 Perbandingan waktu pemrosesan dari masing-masing metode
sorting ...............................................................................................134
Tabel 3.2 Perbandingan waktu pemrosesan dari masing-masing metode
searching ...........................................................................................135
Tabel 3.3 Perbandingan waktu pemrosesan dari masing-masing cara sorting . 167

12

DAFTAR TRACE
Trace 3.1

Program menghitung akar-akar persamaan kuadrat ..........................29

Trace 3.2

Program mengonversi bilangan desimal ke biner .............................36

Trace 3.3

Program mengonversi bilangan biner ke desimal .............................37

Trace 3.4

Program menentukan zodiak bintang ................................................37

Trace 3.5

Program menghitung nilai akhir mata kuliah ....................................48

Trace 3.6

Program menampilkan deret bilangan prima ....................................53

Trace 3.7

Program menentukan deret bilangan fibonacci .................................61

Trace 3.8

Program menghitung langkah minimum pada menara hanoi ............78

Trace 3.9

Program menentukan faktor persekutuan terbesar (FPB) .................85

Trace 3.10 Program menghitung luas dan keliling persegi panjang ...................99
Trace 3.11 Program menghitung luas dan keliling segitiga sembarang ..............99
Trace 3.12 Program menghitung luas dan keliling lingkaran ............................100
Trace 3.13 Program menghitung luas dan keliling trapesium ...........................100
Trace 3.14 Program menghitung luas dan keliling jajargenjang .......................100
Trace 3.15 Program menghitung volume dan luas permukaan balok ................111
Trace 3.16 Program menghitung volume dan luas permukaan bola .................112
Trace 3.17 Program menghitung volume dan luas permukaan limas segitiga
sama sisi ...........................................................................................112
Trace 3.18 Program menghitung volume dan luas permukaan prisma segitiga 112
Trace 3.19 Program menghitung volume dan luas permukaan tabung .............112
Trace 3.20 Program sorting ...............................................................................133
Trace 3.21 Program searching ...........................................................................134
Trace 3.22 Program operasi penjumlahan matriks ............................................148
Trace 3.23 Program operasi transpose matriks .................................................149
Trace 3.24 Program operasi perkalian matriks ..................................................149
Trace 3.25 Program menghitung mean, median, dan modus ............................159
Trace 3.26 Program perbandingan sorting melalui cara biasa dan cara
pointer .............................................................................................166
Trace 3.27 Program menghitung gaji harian .....................................................172
Trace 3.28 Program tamia .................................................................................182
Trace 3.29 Program menentukan kata palindrom ..............................................191
13

Trace 3.30 Program menyimpan data mahasiswa .............................................196


Trace 3.31 Program mengurutkan nilai .............................................................203

14