This document discusses records in Pascal programming. It defines records as a complex data type that allows combining simple variables. Examples of records include a person record with fields for name, age, etc. and an employee record with fields for address, salary, and a person subrecord. Procedures are provided to input, output, combine, and calculate the absolute value of complex number records. Homework involves writing a program to input, output, sort, and update employee records for a company with n employees.
3. Data Structure:
Data Structure are divided into :
Complex types
Simple types
Records
•(char,integer,Boolean .... )
•User design type :
•Type mytype=1.. 100 ;
var x : mytype;
4. Definition
Records are complex type which enables us to combine a set of simple variables in one Data structure named Record ....
Example: ( car’s information ) type
engine
production year
Example(2): date (year.. Month... Day).....
How we can define Records ?
5. Ex:
• Type person=record
fname: string ;
lname: string;
age: integer;
end;
Note : we can define a record in one of its fields
•Type employee=record
address: string;
salary: real;
per: person;
end ;
7. •var Emp :array[1..100] of employee
Define variable of type record
•Note : you can define a matrix records
person
employee
•var y:employee
•var x:person
8. Using records :
Read record’s data :
readln(x);
Readln(y);
{ True }
readln(x.fname);
readln( y.address);
readln(y.per.age);
{ False }
9. Read matrix of records
•for i=1 to 50 do
•begin
readln(Emp[i].salary); readln(Emp[i].per.fname); ………………………………. ………………………………. end;
•Var Emp : array[1..100] of employee;
10. Write record’s data
{↔ writeln(y.per.fname);}
<<<Using with >>>
With y do Begin writeln(address); writeln(per.name); with per do begin writeln(fname); writeln(age); writeln(lname); end; end;
11. In matrix of records
For i:=1 to 75 do
with Emp[i] do
begin
readln(address);
writeln(per.age);
………………………………. ……………………………….
end;
salary:=50000;
12. Exercise :
We have (record) of complex number :
1.Write procedure to input this number
2.Write procedure to print this number
3.Write procedure to combine two numbers
4.Write function to calculate Abs of number
Let’s go
....
13. Program prog2_bytes
Uses wincrt
Type complex=record
re,Img : real;
end;
Procedure Inputcom(var c:complex);
Begin
with c do
Begin
writeln(‘enter the real part ‘);
readln(re);
writeln(‘enter the Img part ‘);
readln(Img);
end;
End;
14. Procedure printcom( c: complex);
Begin
With c do
If Img<>0 then
Writeln(‘z= ‘,re:5:2,Img:5:2,’i’);
Else writeln(‘z=‘,re:5:2);
end;
End;
Procedure sumcom( a , b: complex ; var c:complex);
Begin
with c do
begin
re:=a.re+b.re;
Img:=a.Img+b.Img;
end;
End;
15. Function Abscom(c : complex):real;
Begin
Abscom:=sqrt(sqr(c.re)+sqr(c.Img));
End;
Var x,y,z:complex;
Begin
Inputcom(x);
Inputcom(y);
Sumcom(x ,y ,z );
Printcom(z);
Writeln(‘|z|=‘,Abscom(z:5:2));
End.
18. المطلوب كتابة إجرائية و تابع لحساب المضاعف
المشترك الأصغر والقاسم المشترك الأكبر لعددين
program prog_2bytesteam;
procedure Lcm(a,b:integer; var c:integer);
var i,L,temp : integer;
begin
if (a>b) then
begin
temp:=a;
a:=b;
b:=temp;
end;
i:=1;
L:=a;
while( L mod b <> 0) do
begin
i:=i+1;
L:=a*i;
end;
c:=L;
end;
19. program prog_2bytesteam;
function Lcm(a,b:integer):integer;
var i,L,temp:integer;
begin
if (a>b) then
begin
temp:=a;
a:=b;
b:=temp;
end;
i:=1;
L:=a;
while( L mod b <> 0) do
begin
i:=i+1;
L:=a*i;
end;
Lcm:=L;
end;
20. program prog_2bytesteam;
procedure gcd(a,b:integer; var c:integer);
begin
while( a <> b ) do
if (a>b) then
a:=a-b
else
b:=b-a;
c:=a;
end;