facebook rss daftar isi halaman depan

Laman

Sabtu, 06 November 2010

array

Arrays

Arrays are an important part of Pascal. You can think of them like a catalogue of information. Arrays are declared under the type section. They are a collection of a number of variables, arranged in the form of a table.

type

integerArray = array[1..30] of integer;
integerTable = array[1..25,1..25] of integer;
stringArray = array[1..100] of string[15];

.....

This is an example of declaring an array in the type block. The array called 'integerArray' is like a list of integers, if seen on paper it might look like this...

1.______20
2.______145
3.______39
4.______2708
5.______25
6.______260
-
-
-
30._____300

The array called integerTable can be represented by a table. It is a two dimensional array. You can have three,four or even five dimensions in an array if you want.
Accessing Arrays

To access an entry of an array during the program you must go...arrayname[entrynumber]
or to access a multi-dimensional array you go...arrayname[x,y,z] (3d array)

OK HERE COMES AN EXAMPLE PROGRAM

Program classTest;
{An example program demostration arrays}
{Takes scores from a class test and tells people if they passed or not}

uses Crt;

type

testScores = array[1..10] of integer;

var

i : integer;
marks : testScores;
passOrFail : string[6];

begin

clrscr;
for i := 1 to 10 do
begin write ('Please enter test score number ',i,': ');
readln(marks[i]);
end;
for i := 1 to 10 do
begin
if (marks[i] < 50) then
passOrFail := 'Failed';
else
passOrFail := 'Passed';
writeln ('Student no.',i,' ',passOrFail);
end;

while not keypressed do;

end.

Arrays can also be declared in the variables section without making a type. However it is better to use a type if you plan to use the same type of array in multiple places. So this code in the last program...

type

testScores = array[1..10] of integer;

var

marks : testScores;

Can also be written like this:

var

marks : array[1..10] of integer;

This would do exactly the same thing, but does not declare a type of 'testScores'.
Previous Lesson(flow control) Main Menu Next Lesson(records)

Tidak ada komentar:

Posting Komentar

terimakasih aras kunjungan anda di blog mesujiraya
komentarnya saya tunggu mas bro & mbak sist