1 / 8

Arrays

Arrays. Foundation Studies Course. 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). Definition.

Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays Foundation Studies Course Foundation Studies Course M.Montebello

  2. 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). Foundation Studies Course M.Montebello

  3. 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. Foundation Studies Course M.Montebello

  4. 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. Foundation Studies Course M.Montebello

  5. 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. Foundation Studies Course M.Montebello

  6. 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. Foundation Studies Course M.Montebello

  7. 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. Foundation Studies Course M.Montebello

  8. 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. Garfield Asterix Taz … 79312444 99453552 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. Foundation Studies Course M.Montebello

More Related