Selasa, 24 Mei 2011

ARRAY AND RECORD


Have you ever imagined when we want to make a fruit shop program. If there are 3 types of fruits (melon, and mango duku) means that we must have three variables for each type of fruit. But how difficult if the store has 10 or 20 different types. The solution is to use arrays. Array that is the type of structured data that is useful to store a number of the same type of data. How does it work? namely by using the index (sort of marker). Consider an example:

1: Program Tutorial5_Array;
2: var fruits: array [1 .. 3] of string;
3: i: integer;
4: Begin
5: For i: = 1 to 3 do
6: begin
7: write ('Enter the fruits to', i, ':'); readln (fruit [i]);
8: end;
9: Writeln; Writeln ('Content fruits that entered earlier is:');
10: For i: = 1 to 3 do
11: Writeln ('fruit to', i, ':', fruit [i]);
12: End.

Look at the line to 7. By using only one variable name that is the fruit, we can save three different values. Suppose that we enter is a melon, and mango duku. the fruit [1] value melons, fruit [2] value duku and fruits [3] value of mango. To print the contents of variables that have been stored in the array was the way can be seen in row 11.

How, gampangkan? after learning how to use arrays, we now learn to use the records. Unlike arrays, the record is a type of structured data type that contains some data, each of which can be different types. Consider an example:

1: Program Tutorial5_Record;
2: The type of fruit = record
3: name: string;
4: price: longint;
5: end;
6: var data: fruit;
7: Begin
8: write ('fruit name:'); readln (data.nama);
9: write ('the price of fruit:'); readln (data.harga);
10: write ('display');
11: write ('name'); readln (data.nama);
12: Writeln ('price'); readln (data.harga);
13: End.

What can you take the conclusion from the above program? Confused? So gini, using the record (here named fruit) we can create data types that contain several different data types (names with string type, and price with the type of long integer) which is called a field.

Note the line to 6, there is declared that the variable data type of fruit. Automatic owned fruit (name and price) is owned also by the data. After that see the line to 8 and 9. The name of the fruit is stored in a field name and the price of fruit stored in price field. To be able to store data in the field, the order must variabel.field (eg here data.nama or data.harga). So also if we want to display the contents of these fields.

This type of data like this is very useful in the future loohh ... Especially when combined with the array.

Tidak ada komentar:

Posting Komentar