algorithm and programming (record)

55
Adam Mukharil Bachtiar English Class Informatics Engineering 2011 Algorithms and Programming Record

Upload: adam-mukharil-bachtiar

Post on 20-Mar-2017

37 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Algorithm and Programming (Record)

Adam Mukharil Bachtiar

English Class

Informatics Engineering 2011

Algorithms and Programming

Record

Page 2: Algorithm and Programming (Record)

Steps of the Day

Let’s Start

Definition of

Record Application of

Record

Array of Record

Page 3: Algorithm and Programming (Record)

Definition of Record

All About Record

Page 4: Algorithm and Programming (Record)

Bac

kgro

un

d o

f R

eco

rd

I need a program that similar with array

program but can be composed with

different data types.

Page 5: Algorithm and Programming (Record)

Wh

at is

Re

cord

Data structure that contains of several fields

(more than one) which has different data types.

Page 6: Algorithm and Programming (Record)

Lecturer 1 Lecturer 2

NIP Name Address NIP Name Address

Ilu

stra

tio

n o

f R

eco

rd

Records were named Lecturer 1 and Lecture 2,

consists of 3 fields each of its.

Field

Name of Record

Page 7: Algorithm and Programming (Record)

Lecturer 1 Lecturer 2

NIP Name Address NIP Name Address

Ilu

stra

tio

n o

f R

eco

rd

Field

Name of Record

If you want to access NIP from Lecturer 1, yu

can do with Lecturer1.NIP

Page 8: Algorithm and Programming (Record)

Application of Record

Definition and Structures of Record

Page 9: Algorithm and Programming (Record)

Ste

ps

in R

eco

rd

• Declare record

• Initialize record

• Accessing record (input, operate,

and output)

Page 10: Algorithm and Programming (Record)

Record Declaration (Algorithm)

Kamus:

type

TipeRecord = record

< field_1 : TipeData_1,

field_2 : TipeData_2,

..

field_n :TipeData_n >

endrecord

NamaRecord : TipeRecord

Page 11: Algorithm and Programming (Record)

Example of Record Declaration (Algorithm)

Kamus:

type

RecordDosen = record

< NIP : integer,

Nama : string,

Gaji : real >

endrecord

Dosen : RecordDosen

Page 12: Algorithm and Programming (Record)

Record Declaration (PASCAL)

type

TipeRecord = record

field_1 : TipeData_1;

field_2 : TipeData_2;

..

field_n :TipeData_n;

end;

var

NamaRecord : TipeRecord;

Page 13: Algorithm and Programming (Record)

Example of Record Declaration (PASCAL)

type

RecordDosen = record

NIP : longint;

Nama : string;

Gaji : double;

end;

var

Dosen: RecordDosen;

Page 14: Algorithm and Programming (Record)

Record Initialization (Algorithm)

Format:

NamaRecord.NamaField DefaultValue

Example:

Dosen.NIP 0

Dosen.Nama ‘’

Dosen.Gaji 0

Page 15: Algorithm and Programming (Record)

Record Initialization (Pascal)

Format:

NamaRecord.NamaField := DefaultValue;

Example:

Dosen.NIP := 0;

Dosen.Nama := ‘’;

Dosen.Gaji := 0;

Page 16: Algorithm and Programming (Record)

Input Value to Record (Algorithm)

Format:

input(NamaRecord.NamaField)

Example:

input(Dosen.NIP)

input(Dosen.Nama)

input(Dosen.Gaji)

Page 17: Algorithm and Programming (Record)

Input Value to Record (Pascal)

Format:

readln(NamaRecord.NamaField);

Example:

readln(Dosen.NIP);

readln(Dosen.Nama);

readln(Dosen.Gaji);

Page 18: Algorithm and Programming (Record)

Output Value from Record (Algorithm)

Format:

output(NamaRecord.NamaField)

Example:

output(Dosen.NIP)

output(Dosen.Nama)

output(Dosen.Gaji)

Page 19: Algorithm and Programming (Record)

Output Value from Record (Pascal)

Format:

writeln(NamaRecord.NamaField);

Example:

writeln(Dosen.NIP);

writeln(Dosen.Nama);

writeln(Dosen.Gaji);

Page 20: Algorithm and Programming (Record)
Page 21: Algorithm and Programming (Record)

Example of Record (Algorithm)

1

2

3

4

5

6

7

8

9

10

11

12

13

Algoritma RecordDosen

{I.S.: Dideklarasikan dua buah record dosen}

{F.S.: Menampilkan isi record}

Kamus:

type

RecordDosen = record

< NIP : integer,

Nama : string,

Gaji : real >

endrecord

Dosen1,Dosen2 : RecordDosen

Page 22: Algorithm and Programming (Record)

Example of Record (Algorithm)

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Algoritma:

{input record}

input(Dosen1.NIP)

input(Dosen1.Nama)

input(Dosen1.Gaji)

input(Dosen2.NIP)

input(Dosen2.Nama)

input(Dosen2.Gaji)

{Operasi field record}

Dosen1.Gaji Dosen1.Gaji + 1000000 {Tambah THR}

Dosen2.Gaji Dosen2.Gaji – 100000 (Karena telat}

Page 23: Algorithm and Programming (Record)

Example of Record (Algorithm)

28

29

30

31

32

33

34

35

{Output record}

output(Dosen1.NIP)

output(Dosen1.Nama)

output(Dosen1.Gaji)

output(Dosen2.NIP)

output(Dosen2.Nama)

output(Dosen2.Gaji)

Page 24: Algorithm and Programming (Record)

Example of Record (Pascal)

1

2

3

4

5

6

7

8

9

10

11

12

13

program RecordDosenIF;

uses crt;

type

RecordDosen=record

NIP:longint;

Nama:string;

Gaji:double;

end;

var

Dosen1,Dosen2:RecordDosen;

Page 25: Algorithm and Programming (Record)

Example of Record (Pascal)

14

15

16

17

18

19

20

21

22

23

24

25

26

27

{input record}

write('Masukkan NIP dosen pertama : ');

readln(Dosen1.NIP);

write('Masukkan Nama dosen pertama : ');

readln(Dosen1.Nama);

write('Masukkan Gaji dosen pertama : ');

readln(Dosen1.Gaji);

writeln();

write('Masukkan NIP dosen kedua : ');

readln(Dosen2.NIP);

write('Masukkan Nama dosen kedua : ');

readln(Dosen2.Nama);

write('Masukkan Gaji dosen kedua : ');

Page 26: Algorithm and Programming (Record)

Example of Record (Pascal)

28

29

30

31

32

33

34

35

37

38

39

40

readln(Dosen2.Gaji);

{Operasi pada field record}

Dosen1.Gaji:=Dosen1.Gaji+1000000; {karena THR}

Dosen2.Gaji:=Dosen2.Gaji-100000; {karena telat}

{output record}

writeln();

writeln('NIP dosen pertama = ',Dosen1.NIP);

writeln('Nama dosen pertama = ',Dosen1.Nama);

writeln('Gaji dosen pertama = ',Dosen1.Gaji:0:2);

Page 27: Algorithm and Programming (Record)

Example of Record (Pascal)

41

42

43

44

45

46

47

48

49

writeln();

writeln('NIP dosen kedua = ',Dosen2.NIP);

writeln('Nama dosen kedua = ',Dosen2.Nama);

writeln('Gaji dosen kedua = ',Dosen2.Gaji:0:2);

writeln();

write('Tekan sembarag tombol untuk menutup...');

readkey();

end.

Page 28: Algorithm and Programming (Record)

Example of Record (Pascal)

54

55

56

57

58

59

60

61

jumlah2:=jumlah2+bil2[i];

end;

writeln('Jumlah elemen array bil 2 = ',jumlah2);

writeln();

write('Tekan sembarang tombol untuk menutup...');

readkey();

end.

Page 29: Algorithm and Programming (Record)

Array of Record

Definition and Structures of Array of Record

Page 30: Algorithm and Programming (Record)

Bac

kgro

un

d o

f A

rray

of

Re

cord

I have lecturer’s record but i need

lots of variables to declare

lecturers in program.

Page 31: Algorithm and Programming (Record)

Wh

at is

Arr

ay o

f R

eco

rd Record that declare using array’s form.

It can be made using all ways of array’s

declaration (three ways).

Page 32: Algorithm and Programming (Record)

[1] [2]

NIP Name Address NIP Name Address

Ilu

stra

tio

n o

f A

rray

of

Re

cord

Had been declared an array that had Lecturer

type consists of 3 fields each of element.

To access this i call Lecturer [1].NIP

Lecturer

Page 33: Algorithm and Programming (Record)

Array of Record Declaration (Algorithm)

Kamus:

const

maks = value

type

TipeRecord = record

< field_1 : TipeData_1,

field_2 : TipeData_2,

..

field_n : TipeData_n >

endrecord

NamaArrayofRecord = array [1..maks] of TipeRecord

NamaRecord : NamaArrayofRecord

Page 34: Algorithm and Programming (Record)

Example of Array of Record Declaration (Algorithm)

Kamus:

const

maks = 20

type

DosenIF = record

< NIP : integer,

Nama : string,

Gaji : real >

endrecord

ArrayDosenIF = array [1..maks] of DosenIF

Dosen: ArrayDosenIF

Page 35: Algorithm and Programming (Record)

Array of Record Declaration (Pascal)

const

maks = value;

type

TipeRecord = record

field_1 : TipeData_1;

field_2 : TipeData_2;

..

field_n : TipeData_n;

end;

NamaArrayofRecord = array [1..maks] of TipeRecord;

var

NamaRecord : NamaArrayofRecord;

Page 36: Algorithm and Programming (Record)

Example of Array of Record Declaration (Pascal)

const

maks = 20;

type

DosenIF = record

NIP : longint;

Nama : string;

Gaji : double;

end;

ArrayDosenIF = array [1..maks] of DosenIF;

var

Dosen: ArrayDosenIF;

Page 37: Algorithm and Programming (Record)

Record Initialization (Algorithm)

Format:

NamaRecord[indeks].NamaField DefaultValue

Example:

Dosen[1].NIP 0

Dosen[1].Nama ‘’

Dosen[1].Gaji 0

Page 38: Algorithm and Programming (Record)

Record Initialization (Pascal)

Format:

NamaRecord[indeks].NamaField := DefaultValue;

Example:

Dosen[1].NIP := 0;

Dosen[1].Nama := ‘’;

Dosen[1].Gaji := 0;

Page 39: Algorithm and Programming (Record)

Input Value to Array of Record (Algorithm)

Format:

input(NamaRecord[indeks].NamaField)

Example:

input(Dosen[1].NIP)

input(Dosen[1].Nama)

input(Dosen[1].Gaji)

Page 40: Algorithm and Programming (Record)

Input Value to Array of Record (Pascal)

Format:

readln(NamaRecord[indeks].NamaField);

Example:

readln(Dosen[1].NIP);

readln(Dosen[1].Nama);

readln(Dosen[1].Gaji);

Page 41: Algorithm and Programming (Record)

Output Value from Array from Record (Algorithm)

Format:

output(NamaRecord[indeks].NamaField)

Example:

output(Dosen[1].NIP)

output(Dosen[1].Nama)

output(Dosen[1].Gaji)

Page 42: Algorithm and Programming (Record)

Output Value from Array from Record (Pascal)

Format:

writeln(NamaRecord[indeks].NamaField);

Example:

writeln(Dosen[1].NIP);

writeln(Dosen[1].Nama);

writeln(Dosen[1].Gaji);

Page 43: Algorithm and Programming (Record)
Page 44: Algorithm and Programming (Record)

Example of Array of Record (Algorithm)

1

2

3

4

5

6

7

8

9

10

11

12

13

Algoritma ArrayRecordMakananMinuman

{I.S : didefinisikan dua array of record food and drink}

{F.S : menampilkan array of record beserta operasinya}

const

maks=3;

type

RecordMakanan = record

< KodeMakanan:integer,

NamaMakanan:string,

HargaMakanan:real,

DiskonMakanan:real >

endrecord

Page 45: Algorithm and Programming (Record)

Example of Array of Record (Algorithm)

14

15

16

17

18

19

20

21

22

23

24

25

26

27

RecordMinuman = record

< KodeMinuman:integer,

NamaMinuman:string,

HargaMinuman:real,

DiskonMinuman:real >

endrecord

{array of record}

ArrayMakanan = array [1..maks] of RecordMakanan;

ArrayMinuman = array [1..maks] of RecordMinuman;

Makanan:ArrayMakanan;

Minuman:ArrayMinuman;

TotalHarga:real;

i:integer;

Page 46: Algorithm and Programming (Record)

Example of Array of Record (Algorithm)

28

29

30

31

32

33

34

35

37

38

39

40

41

42

Algoritma:

{input record}

for i 1 to maks do

input(Makanan[i].KodeMakanan)

input(Makanan[i].NamaMakanan);

input(Makanan[i].HargaMakanan)

input(Makanan[i].DiskonMakanan)

endfor

for i 1 to maks do

input(Minuman[i].KodeMinuman)

input(Minuman[i].NamaMinuman)

input(Minuman[i].HargaMinuman)

input(Minuman[i].DiskonMinuman)

endfor

Page 47: Algorithm and Programming (Record)

Example of Array of Record (Algorithm)

43

44

45

46

47

48

49

50

51

52

53

{perhitungan total harga}

TotalHarga 0

for i 1 to maks do

TotalHarga TotalHarga+(Makanan[i].HargaMakanan

(Makanan[i].HargaMakanan*Makanan[i].DiskonMakanan))

+(Minuman[i].HargaMinuman-

(Minuman[i].HargaMinuman*Minuman[i].DiskonMinuman))

endfor

{output record}

for i 1 to maks do

output(Makanan[i].KodeMakanan)

output(Makanan[i].NamaMakanan)

output(Makanan[i].HargaMakanan)

output(Makanan[i].DiskonMakanan)

endfor

Page 48: Algorithm and Programming (Record)

Example of Array of Record (Algorithm)

54

55

56

57

58

59

60

61

for i 1 to maks do

output(Minuman[i].KodeMinuman)

output(Minuman[i].NamaMinuman)

output(Minuman[i].HargaMinuman)

output(Minuman[i].DiskonMinuman)

endfor

output(TotalHarga);

Page 49: Algorithm and Programming (Record)

Example of Array of Record (Pascal)

1

2

3

4

5

6

7

8

9

10

11

12

13

program MenuMakananMinuman;

uses crt;

const

maks=3;

type

RecordMakanan = record

KodeMakanan:integer;

NamaMakanan:string;

HargaMakanan:real;

DiskonMakanan:real;

end;

Page 50: Algorithm and Programming (Record)

Example of Array of Record (Pascal)

14

15

16

17

18

19

20

21

22

23

24

25

26

27

RecordMinuman = record

KodeMinuman:integer;

NamaMinuman:string;

HargaMinuman:real;

DiskonMinuman:real;

end;

{array of record}

ArrayMakanan=array [1..maks] of RecordMakanan;

ArrayMinuman=array [1..maks] of RecordMinuman;

var

Makanan:ArrayMakanan;

Minuman:ArrayMinuman;

TotalHarga:real;

i:integer;

Page 51: Algorithm and Programming (Record)

Example of Array of Record (Pascal)

28

29

30

31

32

33

34

35

37

38

39

40

41

begin

{input record}

for i:=1 to maks do

begin

write('Masukkan kode makanan ',i,' : ');

readln(Makanan[i].KodeMakanan);

write('Masukkan nama makanan ',i,' : ');

readln(Makanan[i].NamaMakanan);

write('Masukkan harga makanan ',i,' : ');

readln(Makanan[i].HargaMakanan:0:2);

write('Masukkan diskon makanan ',i,' : ');

readln(Makanan[i].DiskonMakanan:0:2);

end;

Page 52: Algorithm and Programming (Record)

Example of Array of Record (Pascal)

42

43

44

45

46

47

48

49

50

51

52

53

54

writeln();

for i:=1 to maks do

begin

write('Masukkan kode Minuman ',i,' : ');

readln(Minuman[i].KodeMinuman);

write('Masukkan nama Minuman ',i,' : ');

readln(Minuman[i].NamaMinuman);

write('Masukkan harga Minuman ',i,' : ');

readln(Minuman[i].HargaMinuman:0:2);

write('Masukkan diskon Minuman ',i,' : ');

readln(Minuman[i].DiskonMinuman:0:2);

end;

Page 53: Algorithm and Programming (Record)

Example of Array of Record (Pascal)

55

56

57

58

59

60

61

62

63

{perhitungan total harga}

TotalHarga:=0;

for i:=1 to maks do

TotalHarga:=TotalHarga+(Makanan[i].HargaMakanan

(Makanan[i].HargaMakanan*Makanan[i].DiskonMakanan))

+(Minuman[i].HargaMinuman-

(Minuman[i].HargaMinuman*Minuman[i].DiskonMinuman));

{output record}

clrscr();

for i:=1 to maks do

begin

writeln('Kode makanan ',i,' adalah ',Makanan[i].KodeMakanan);

writeln('Nama makanan ',i,' adalah ',Makanan[i].NamaMakanan);

Page 54: Algorithm and Programming (Record)

Example of Array of Record (Pascal)

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

writeln('Harga makanan ',i,' adalah ',Makanan[i].HargaMakanan:0:2);

writeln('Diskon makanan ',i,' adalah ',Makanan[i].DiskonMakanan:0:2);

end;

writeln();

for i:=1 to maks do

begin

writeln('Kode Minuman ',i,' adalah ',Minuman[i].KodeMinuman);

writeln('Nama Minuman ',i,' adalah ',Minuman[i].NamaMinuman);

writeln('Harga Minuman ',i,' adalah ',Minuman[i].HargaMinuman);

writeln('Diskon Minuman ',i,' adalah ',Minuman[i].DiskonMinuman);

end;

writeln();

writeln('Total harga yang harus dibayar adalah : Rp. ',TotalHarga:0:2);

writeln();

write('Tekan sembarang tombol untuk menutup...');

readkey();

end.

Page 55: Algorithm and Programming (Record)

Contact Person: Adam Mukharil Bachtiar

Informatics Engineering UNIKOM Jalan Dipati Ukur Nomor. 112-114 Bandung 40132

Email: [email protected] Blog: http://adfbipotter.wordpress.com

Copyright © Adam Mukharil Bachtiar 2011