Sabtu, Juli 11, 2015

Introduction to Array On Pascal Programming (Array 1 Dimension)

**Example we have 5 integer variables:
var a1,a2,a3,a4,a5:integer;
begin
a1:=2;
a2:=3;
a3:=5;
a4:=7;
a5:=11;
writeln(a1);
writeln(a2);
writeln(a3);
writeln(a4);
writeln(a5);
readln
end.
If the program is run then will be appear:
2
3
5
7
11

Writing for program above can be written simpler using a varible typed "array":
var a:array[1..5] of integer;
begin
a[1]:=2;
a[2]:=3;
a[3]:=5;
a[4]:=7;
a[5]:=11;
writeln(a[1]);
writeln(a[2]);
writeln(a[3]);
writeln(a[4]);
writeln(a[5]);
readln
end.
If the program is run then will be appear:
2
3
5
7
11

**Example we have program to input 5 integers using 5 integers variables:
var a1,a2,a3,a4,a5:integer;
begin
write('input number to-1! ');
readln(a1);
write('input number to-2! ');
readln(a2);
write('input number to-3! ');
readln(a3);
write('input number to-4! ');
readln(a4);
write('input number to-5! ');
readln(a5);
writeln('number to-1 is inputted is: ',a1);
writeln('number to-2 is inputted is: ',a2);
writeln('number to-3 is inputted is: ',a3);
writeln('number to-4 is inputted is: ',a4);
writeln('number to-5 is inputted is: ',a5);
readln
end.
If the program is run then will be appear:
input number to-1! 7
input number to-2! 1000
input number to-3! 23
input number to-4! 114
input number to-5! 789
number to-1 is inputted is: 7
number to-2 is inputted is: 1000
number to-3 is inputted is: 23
number to-4 is inputted is: 114
number to-5 is inputted is: 789

Writing for program above can be written simpler using a variable typed "array" and a variable typed integer;
var
  a:array[1..5] of integer;
  b:integer;
begin
for b:=1 to 5 do
begin
  write('input number to-',b,'! ');
  readln(a[b]);
end;
for b:=1 to 5 do
  writeln('number to-',b,' is inputted is: ',a[b]);
readln
end.
If the program is run then will be appear:
input number to-1! 7
input number to-2! 1000
input number to-3! 23
input number to-4! 114
input number to-5! 789
number to-1 is inputted is: 7
number to-2 is inputted is: 1000
number to-3 is inputted is: 23
number to-4 is inputted is: 114
number to-5 is inputted is: 789


With array we can write program in simpler code.. :)

0 komentar: