IT - Programming
Date : 10-06-2020
Good morning Grade 11 IT Learners
Today we like to start out new section called an Array. Please read the notes below to understand about Array and also read the page number 182 from your Delphi text book to understand better about this chapter called Array.
I will send you some activities on Array by tomorrow. Please complete those activities by next week Monday(15-06-2020) and send back to the code only copied in Ms-word or any text form (copy only the Delphi code and past in Ms-word or notepad page) to me via email.
Email address: dchoudhury@alexhigh.org.za
Mr D Choudhury
1. What is the array?
Arrays are the limited and ordered set of the values, which are the same type. Every single value is called the item (component) of array.
2. What data types can be the array items?
The array type can be any, adopted in Delphi, except for the file type. This type is a base type.
3. How to define the one-dimensional array?
According to Delphi syntax, you can define the array by two ways. First way – in the “var” section. The second way – by using his own type definition in the “type” section.
Way 1. Definition of one-dimensional array in the var section of variables definition.
var
m1:array [1..100] of integer; // an array of 100 integers
m2:array [1..50] of real; // an array of 50 real numbers
m3:array [-20..30] of char; // an array of 51 characters
m4:array [5..5] of boolean; // an array that contains 1 item of "boolean" type
Way 2. Definition of one-dimensional array by using the “type” section.
type
TM1 = array [1..100] of integer; // an array of 100 integers
TM2 = array [1..50] of real; // an array of 50 real numbers
TM3 = array [-20..30] of char; // an array of 51 characters
TM4 = array [5..5] of boolean; // an array that contains 1 item of boolean type
var
m1:TM1;
m2:TM2;
m3:TM3;
m4:TM4;
4. How get the access to the items of one-dimensional array?
Suppose we have the following description:
var
m1:array [1..100] of integer; // an array of 100 integers
m2:array [1..50] of real; // an array of 50 real numbers
m3:array [-20..30] of char; // an array of 51 characters
m4:array [5..5] of boolean; // an array, that contains 1 item of boolean type
Then, to assign the value to a concrete item of array, you need to write:
m1[15] := 289; // is entered the number 289 in the 15-th element of the array
m2[50] := -3.85; // is entered the number -3.85 in the 50-th element of array
m3[-8] := 'k';
m4[5] := true;
5. Example the code snippet of zeroing array called “M“, containing 100 integers.
var
M:array [1..100] of integer;
i:integer;
begin
...
for i := 1 to 100 do
M[i]:=0;
...
end;
6. Example of sum calculation of array Mas items, containing 50 real numbers.
var
Mas:array [1..50] of real; // the array definition
i:integer;
sum:real;
begin
...
sum := 0; // zeroing of sum
for i := 1 to 50 do // searching the sum
sum := sum + Mas[i];
...
end;
7. Example of searching the maximum value in the array of 100 numbers.
var
Mas:array [1..100] of integer;
i:integer;
max:integer; // max - maximum value
begin
...
max:=Mas[1];
for i:=2 to 100 do
if max<Mas[i] then
max:=Mas[i];
...
end;
8. An example of determining the presence of a given element in the array of 100 integers.
In the example below, the array is saved in the variable Mas. The item, which you need to find, is saved in the variable num.
var
Mas:array [1..100] of integer;
i:integer;
num:integer; // the desired item
f_is:boolean;
begin
...
f_is:=false;
for i:=1 to 100 do
if num=Mas[i] then
f_is:=true;
...
end;