Anda di halaman 1dari 25

BAB I

TUJUAN PRAKTIKUM
1. Mengerti konsep data array dalam bahasa C.
2. Mengerti cara melakukan pemrograman sederhana dengan menggunakan
array.
3.

Menjelaskan tentang array berdimensi satu

4.

Menjelaskan tentang array berdimensi dua

5.

Menjelaskan tentang array berdimensi banyak

6.

Menjelaskan tentang inisialisasi array tak berukuran.

7.

Menjelaskan array sebagai parameter fungsi

BAB II
DASAR TEORI
Dalam beberapa literatur, array sering disebut (diterjemahkan) sebagai larik.
Array adalah kumpulan dari nilai-nilai data bertipe sama dalam urutan tertentu
yang menggunakan sebuah nama yang sama.

Nilai-nilai data di suatu array

disebut dengan elemen-elemen array. Letak urutan dari elemen-elemen array


ditunjukkan oleh suatu subscript atau indeks.
Array bisa berupa array berdimensi satu, dua, tiga atau lebih.

Array

berdimensi satu (one-dimensional array) mewakili bentuk suatu vektor. Array


berdimensi dua (two-dimensional array) mewakili bentuk dari suatu matriks atau
tabel. Array berdimensi tiga (three-dimensional array) mewakili bentuk suatu
ruang.

1.

Array Berdimensi Satu

1.1. Mendeklarasikan Array


Suatu array berdimensi satu dideklarasikan dalam bentuk umum berupa :
tipe_data nama_var[ukuran];
dengan :

tipe_data : untuk menyatakan tipe dari elemen array, misalnya int, char,
float.

nama_var : nama variabel array

ukuran

: untuk menyatakan jumlah maksimal elemen array.

Contoh pendeklarasian array :


float nilai_tes[5];
menyatakan bahwa array nilai_tes mengandung 10 elemen bertipe float.

2.

Array Berdimensi Dua


Data seperti yang disajikan pada Tabel 6-1, dapat disimpan pada sebuah

array berdimensi dua. Dimensi pertama dari array digunakan untuk menyatakan
kode program kursus dan dimensi kedua untuk menyatakan tahun kursus.

Tabel 1. Data Kelulusan Siswa Pada Sebuah Kursus Komputer


Tahun

1998

1999

2000

80
15
8
10

540
83
12
129

1032
301
15
257

Program
1. (INTRO)
2. (BASIC)
3. (PASCAL)
4. (C)

3. Array Berdimensi Banyak.


C memungkinkan untuk membuat array yang dimensinya lebih dari dua.
Bentuk umum pendeklarasian array berdimensi banyak :
tipe nama_var[ukuran 1][ukuran2}[ukuranN];
sebagai contoh :
int data_huruf[2][8][8];
merupakan pendeklarasian array data_huruf sebagai array berdimensi tiga.
Sama halnya dengan array berdimensi satu atau dua, array berdimensi
banyak juga bisa diinisialisasi.

BAB III
PERCOBAAN

3.1.

Percobaan 1 : Mengisi dan menampilkan beberapa variabel array

Program :

Hasil eksekusi :

Analisa :
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

3.2. Percobaan 2 : Mencetak 15 angka pertama dalam deret Fibonacci.


Program :

Hasil eksekusi :

Analisa :
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

3.3. Percobaan 3 : Menginisialisasi elemen array bertipe integer


Program :

Hasil eksekusi :

Analisa :
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

3.4. Percobaan 4 : Menginisialisasi elemen array bertipe karakter


Program :

Hasil eksekusi :

10

Analisa :
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

11

3.5.

Percobaan 5 : Menentukan nilai maksimum dari sederetan nilai yang


sudah diinisialisasi dan disimpan dalam array; mengirim array
sebagai parameter sebuah fungsi.

Program :

Hasil eksekusi :

12

Analisa :
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

13

3.6.

Percobaan 6 : Menentukan nilai minimum dari sederetan nilai


masukan yang disimpan dalam array; mengirim array sebagai
parameter sebuah fungsi.

Program :

Hasil eksekusi :

14

Analisa :
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

15

3.7. Percobaan 7 : Menjumlahkan dua buah array yang ; mengirim array


sebagai parameter sebuah fungsi.

Program :

Hasil eksekusi :

16

Analisa :
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

17

BAB IV
LATIHAN
1. Membuat program untuk menghitung nilai rata-rata sbb :
Input

: jumlah data (n), data ke-1 s/d data ke-n

Output : nilai maks, nilai min, nilai rata-rata


Program :

Hasil eksekusi :

18

Analisa :
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

19

2. Mengubah program di atas sehingga mendapatkan tampilan seperti di bawah


ini:
No. Nilai

Total

Rata-rata

1.

85

85

85.00

2.

95

180

90.00

3.

60

240

80.00

4.

80

320

80.00

5.

50

370

74.00

6.

100

470

78.33

Nilai maks

= 100

Nilai min

= 50

Nilai rata-rata = 78.33


Nilai terbaik adalah nilai ke = 6
Program :

20

Hasil eksekusi :

Analisa :
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

21

3. Membuat program untuk menampilkan deret Fibonacci sbb :


Input
: jumlah deret
Output

: deret[i] = deret[i-1] + deret[i-2]

Contoh : jumlah deret = 10


0

13

21

34

Program :

Hasil eksekusi :

22

Analisa :
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

23

4. Buat program untuk mengurutkan data masukan dengan urutan naik


(ascending)
Input : jumlah data (n), data ke-1 s/d data ke-n
Output : Tampilan data dalam keadaan terurut
Contoh :
Jumlah data = 3
Data ke-1 = 50
Data ke-2 = 43
Data ke-3 = 45
Data terurut naik:
Data ke-1 = 43
Data ke-2 = 45
Data ke-3 = 50
Program :

Hasil eksekusi :

24

Analisa :
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

25