Records Foundation Studies Course Foundation Studies M Montebello
Records Foundation Studies Course Foundation Studies. M. Montebello Course
More than 1 datatype … • A record is a user defined data type suitable for grouping data elements together. • All elements of an array must contain the same data type. A record overcomes this by allowing us to combine different data types together. Foundation Studies Course M. Montebello
Where is it applicable? • Suppose we want to create a data record which holds a student name and mark. The student name is a packed array of characters, and the mark is an integer. • We could use two separate arrays for this, but a record is easier. The method to do this is, – define or declare what the new data group (record) looks like – create a working variable to be of that type Foundation Studies Course M. Montebello
TYPE RECORD_NAME = record Var 1 : data type; Var 2 : data type; Var 3 : data type END; Definition The first portion defines the composition of the record identified as RECORD_NAME. It consists of a number of parts (called fields). Each field will have: – Variable Name : – Data Type for the variable name; And terminates with the reserved word END; Foundation Studies Course M. Montebello
Declaring Records A typical declaration follows: TYPE studentinfo = RECORD name : string; age: integer; gender: char; registered: boolean END; VAR student 1 : studentinfo; Foundation Studies Course M. Montebello
Assigning Values Each of the individual fields of a record are accessed by using the DOT format, recordname. fieldname : = value or variable; e. g. student 1. name : = 'JOE BLOGGS '; student 1. age : = 21; student 1. gender : = ‘M’; student 1. registered : = TRUE; Foundation Studies Course M. Montebello
program prog 6; Example - Declarations uses CRT; type Student. Info = record name: string; age: integer; city: string; zip: integer; end; const Max=2; var number: integer; Students: array [1. . Max] of Student. Info; Foundation Studies Course M. Montebello
Begin Example - Input clrscr; {INPUT} writeln('Enter Student Details: '); for number: =1 to Max do begin write('Name ', number, ': '); readln(Students[number]. name); write('Age ', number, ': '); readln(Students[number]. age); write('City ', number, ': '); readln(Students[number]. city); write('Zip ', number, ': '); readln(Students[number]. zip); writeln; end; Foundation Studies Course M. Montebello
clrscr; Example - Output {OUTPUT} writeln('These are the Student Details you entered: '); for number: =1 to Max do begin write(Students[number]. name); write(' ', Students[number]. age); write(' ', Students[number]. city); writeln(' ', Students[number]. zip); end; End. Foundation Studies Course M. Montebello
Lab 1 Change Program 6 to be able to store 30 students with the following details: • • • Name Maths Name: Joe Borg English Maths: 55 English: 68 Maltese: 75 Maltese Average: 66. 0 Grade: B Average Mark Grade A (80 -100) B (60 -80) C (50 -60) D (40 -50) F (less than 40) Foundation Studies Course M. Montebello
Lab 2 The owners of a video shop decided to computerize their database and required a program in order to enter the data they already had stored on cards. Each card had details about a single video, namely: • Name of video, • Reference number, • Location - Shelf number, • Type – Love(L), Drama(D), Violent(V), • Available ? Write a program to enter 5 items into this database and display them in table form. Foundation Studies Course M. Montebello
- Slides: 11