Senin, Agustus 27, 2018

Make Set On Pascal Programming

Processing...

For example we have set of integer with member: (2,3,4,5), that can be written on pascal programming as follows:
var
a:set of 2..5;

begin
a:=[2,3,4,5];
end.

If we want to show the member, then syntax of our program will become:
var
a:set of 2..5;
a1:integer;

begin
a:=[2,3,4,5];
for a1 in a do
begin
writeln(a1);
end;
readln;
end.
If program beside is run, then will shown:
2
3
4
5
a: set of 2..5 variable means, on body of program can be defined a set with member include in range 2≤a≤5. On body of program, a set is defined has member (2,3,4,5). The four member of the set will be defined cause they are include in range 2≤a≤5.

But if on body of program, we define a set has member (1,2,3,4,5,6), so that syntax of our program be as follows:
var
a:set of 2..5;
a1:integer;

begin
a:=[1,2,3,4,5,6];
for a1 in a do
begin
writeln(a1);
end;
readln;
end.
If the program beside is run, then will shown:
2
3
4
5
1 and 6 member will not shown when program is run, because 1 and 6 member is not defined because 1 and 6 member is not include in range 2≤a≤5.

a1 variable is variable show member of set which value of a1 always changed corresponding with number of repetition, number of repetition certain as many as members of set included in range of set when defined on variable
Example, we add n variable to know number of iteration:
var
a:set of 2..5;
a1,n:integer;

begin
a:=[1,2,3,4,5,6];
for a1 in a do
begin
writeln(a1);
n:=n+1;
writeln('This is iteration-',n);
writeln
end;
readln;
end.
If program beside is run, then will be shown:
2
This is iteration-1

3
This is iteration-2

4
This is iteration-3

5
This is iteration-4


Writing of define of a set on body of program a:=[1,2,3,4,5,6] can be changed with a:=[1..6], so that syntax of our program become:
var
a:set of 2..5;
a1:integer;

begin
a:=[1..6];
for a1 in a do
begin
writeln(a1);
end;
readln;
end.
If program beside is run then will be shown:
2
3
4
5

0 komentar: