Presentation is loading. Please wait.

Presentation is loading. Please wait.

Foundation Studies Course M.Montebello Arrays Foundation Studies Course.

Similar presentations


Presentation on theme: "Foundation Studies Course M.Montebello Arrays Foundation Studies Course."— Presentation transcript:

1 Foundation Studies Course M.Montebello Arrays Foundation Studies Course

2 Foundation Studies Course M.Montebello Introduction An array is a structure which holds many variables, all of the same data type. The array consists of so many elements, each element of the array capable of storing one piece of data (ie, a variable).

3 Foundation Studies Course M.Montebello Definition An array is defined as follows: array_name = ARRAY [lower..upper] of data_type ; Lower and Upper define the boundaries for the array. Data_type is the type of variable which the array will store, eg, type int, char etc.

4 Foundation Studies Course M.Montebello Declaring Arrays A typical declaration follows: VAR marks = ARRAY [1..20] of integer; This creates a definition for an array of integers called marks, which has 20 separate locations numbered from 1 to 20. Each of these positions (called an element), holds a single integer.

5 Foundation Studies Course M.Montebello Assigning Values To assign a value to an element of an array, use marks[2] := 10; This assigns the integer value 10 to element 2 of the marks array. The value or element number (actually its called an index) is placed inside the square brackets. To assign the value stored in an element of an array to a variable, use: Var total:integer; total := marks[1]+marks[2]+ … + marks[10]; This takes the integer stored in element 1 to 10 of the array marks, and makes the integer total equal to it.

6 Foundation Studies Course M.Montebello Example program Simple; var name : string; maths,english,maltese,total: integer; average: real; begin Write('Enter name of student '); readln(names); write('Enter marks for Maths --> '); readln(maths); write('Enter marks for English --> '); readln(english); write('Enter marks for Maltese --> '); readln(maltese); total:= maths + english + maltese; average:=total/3; end.

7 Foundation Studies Course M.Montebello Example program Simple_Arrays; var index : integer; names : array [1..10] of string; maths,english,maltese,total: array [1..10] of integer; average: array [1..10] of real; begin for index := 1 to 10 do begin Write('Enter name of student ',Index,': '); readln(names[index]); write('Enter marks for Maths --> '); readln(maths[index]); write('Enter marks for English --> '); readln(english[index]); write('Enter marks for Maltese --> '); readln(maltese[index]); total[index]:= maths[index] + english[index] + maltese[index]; average[index]:=total[index]/3; end; end.

8 Foundation Studies Course M.Montebello Lab In order to store the telephone numbers of his friends, a programmer decided to make use of two 1-dimensional arrays to store the names and the corresponding phone numbers. GarfieldAsterixTaz … 7931244499453552 79266262… Write a short program to enter and store up to fifty names and telephone numbers. Ensure that the exact variable declarations are made as well as the correct program syntax in general is used.


Download ppt "Foundation Studies Course M.Montebello Arrays Foundation Studies Course."

Similar presentations


Ads by Google