Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
1
Dasar Pemrograman Record
2
Record Record merupakan salah satu tipe data terstruktur yang terdiri atas sekumpulan variabel data Record memungkinkan untuk memanipulasi sekumpulan elemen data dengan tipe yang tidak sama, yang seolah-olah merupakan satu obyek Variabel data penyusun record disebut field
3
Record Record sering digunakan dalam program terutama yang berhubungan dengan file. Misalnya, sebuah record tentang data seseorang, yang terdiri dari nama, alamat, umur dan pekerjaan. Semua data tersebut dihimpun dalam satu record dengan nama, alamat, umur dan pekerjaan sebagai field- fieldnya
4
Deklarasi Record (1) Format: Type Name of record = record
name of field (1) : type of field (1); name of field (2) : type of field (2); name of field (3) : type of field (3); : : : name of field (n) : type of field (n); end;
5
Declaring Records (2) Example: Type Person = record name : string;
age : integer; height : real; weight : real; end;
6
Declaring Variables That Are Records: What You Get
jMichaelMoore name age height weight Format: name of variable : name of record; Example: var jMichaelMoore : Person; var bartSimpson : Person; bartSimpson name age height weight
7
Using Record Variables (1)
Example: Declaring the record and instances of the record type Person = record name : string; age : integer; height : real; weight : real; end; (* Declaration of a Person *) begin var jMichaelMoore : Person; var bartSimpson : Person; : : : end.
8
Using Record Variables (2)
Assignment (field basis): e.g., bartSimpson.name := 'Bart'; bartSimpson.age := 10; bartSimpson.height := 48; bartSimpson.weight := 80; Assignment (all fields are copied – if the records are declared to be the same type) jMichaelMoore := bartSimpson;
9
Assignment Between Different Record Types Cannot Be Performed
Example: Cat = record name : string [NAME_LENGTH]; end; (* Declaration of a Cat *) Dog = record end; (* Declaration of a Dog *) begin var aCat : Cat; var aDog : Dog; aCat := aDog; end. Problem: Cat <> Dog Each has been declared to be a different type of variable.
10
Assignment Between The Same Type Of Record Can Be Performed
Example: type Pet = record name : string; end; (* Declaration of a Pet *) begin var aCat : Pet; var aDog : Pet; aCat := aDog; end. OK: Cat and Dog are of the same type
11
Contoh Lain
12
Mengakses Record
13
Contoh Program
14
Contoh Program (lanjutan)
15
A Shortcut For Referencing All The Fields Of A Record: With-Do
Allows you to refer to the fields of a record without having to constantly refer to the name of the record variable. Format: with name of record variable do body Example: with bartSimspon do begin writeln('Personal information:'); writeln('Name: ':8, name); writeln('Age: ':8, age); writeln('Height: ':8, height); writeln('Weight: ':8, weight); end; (* With do for Bart Simpson *)
16
Record Berisi Array program arrayRecord; uses wincrt; const MAX = 100; type Mhs = record NIM : string[8]; Nama: string[25]; Nilai: array[1..3] of real; End; TKumpulanMhs = array[1..MAX] of TMhs; Var A:KumpulanMhs; I, N: integer; NA: real;
17
Begin Write (‘Masukkan jumlah siswa yang akan diisikan: ’); Readln (N); Writeln;
18
{memasukkan data} Write (‘Memasukkan data’); Write (‘ ’); For i:= 1 to N do Begin Writeln (‘Data siswa ke-’, i); With A[i] do Write (‘ NIM : ’); readln(NIM); Write (‘ Nama : ’); readln(Nama); Write (‘ Nilai’); Write (‘ Nama ke-1 (Kuis) : ’); readln(Nilai[1]); Write (‘ Nama ke-2 (UTS) : ’); readln(Nilai[2]); Write (‘ Nama ke-2 (UAS) : ’); readln(Nilai[3]); End; Writeln;
19
{menampilkan data} Writeln(‘DAFTAR NILAI MAHASISWA’); Writeln(‘ ’); Writeln(‘NIM’:8, ‘’:2, ‘Nama’:25, ‘’:2, ‘NILAI’); For i:=1 to N do Begin With A[i] do NA := (0.2*Nilai[1]) + (0.3*Nilai[2]) + (0.5*Nilai[3]); Write (NIM:8, ‘’:2, NAMA:25, ‘’:2, NA:0:2); End; Writeln; Readln; End.
20
Array Berisi Record program Welcome; uses WinCrt; Const maks = 100; type TSiswa = record nim : string[8]; nama: string[25]; end; TKumpulanSiswa = array[1..maks] of TSiswa; var i, n : integer; A: TKumpulanSiswa;
21
begin clrscr; Writeln ('Jumlah Siswa: '); readln (n); writeln; {memasukkan data dalam array} writeln (' '); for i:= 1 to n do writeln ('Data Siswa ke-', i); write ('Nim : '); readln (a[i].nim); write ('Nama : '); readln (a[i].nama); end;
22
{menampilkan data dari array} writeln ('DAFTAR SISWA'); writeln (' '); writeln ('NIM ', ' ':2, 'NAMA'); for i:= 1 to n do begin writeln (a[i].nim, ' ':2, a[i].nama); end; end.
23
Review Dengan bantuan variabel array, simpan dan tampilkan sepuluh jenis musik ini: pop, dangdut, jazz, keroncong, reggae, acapella, rock, heavy metal, instrumental, blues Dengan array dan record simpan dan tampilkan data barang berikut: Nama: sari apel, kualitas: A, harga: 550 Nama: jenang apel, kualitas B, harga: 750 Nama: buah apel, kualitas C, harga: 950
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.