Presentation is loading. Please wait.

Presentation is loading. Please wait.

ARRAYS 1.Basic Ideas 2.The Array Type 3.Processing Arrays 4.Parallel Arrays 5.Two-dimensional Array 6.Arrays as Parameters.

Similar presentations


Presentation on theme: "ARRAYS 1.Basic Ideas 2.The Array Type 3.Processing Arrays 4.Parallel Arrays 5.Two-dimensional Array 6.Arrays as Parameters."— Presentation transcript:

1

2 ARRAYS 1.Basic Ideas 2.The Array Type 3.Processing Arrays 4.Parallel Arrays 5.Two-dimensional Array 6.Arrays as Parameters

3 1.Basic Idea v using 5 variables to store 5 integers 1216321827 A B C D E v using an array to store five integers 12 16 32 18 27 A[1]A[2]A[3]A[4]A[5] ‘A sub 1’‘A sub 2’ 1 5 2 3 4 Block A AB C D E

4 2.The Array Type v An array –is a structured data type in which a collection of data items of the same type is stored –entire collection can be referred by using a single variable name v An element –is the individual item of an array –each can store a value of the declared type –identified by an index or a subscript

5 2.1.Declaring an array v an array type can be defined by the user v 2 types - index type (any ordinal type) –base type (any data type) array [index type] of base type e.g. Name : array [1..40] of string; Score : array [1..40] of integer; Grade: array [1..12] of char; Xcoordinate : array [-5..5] of real; –from index type, we know u the beginning & ending values u the size of the array

6 2.2.Accessing array elements v array name [index] v e.g. Num: array [1..5] of integer;  e.g. Num [1] := 15; Num [4] := Num[2] + Num [3]; readln (Num[1], Num[2], Num[3]); writeln (Num[2], Num[3], Num[4]); if Num[2] > Num[3] then writeln (Num[2], ‘ is larger ’ ); 1234512345 Num The array Num Num [2] Num [5]

7 2.3.The type definition section v used to - rename predefined types or - define new data types e.g. const MaxSize = 40; type NameType = string; ScoreType = integer; NamArray = array [1..MaxSize] of NameType; ScoreArray = array[1..MaxSize] of ScoreType; var Name : NameArray; Score : ScoreArray; is equivalent to var Name : array[1..40] of string; Score : array[1..40] of integer

8 2.3.[cont’d] v Example Write a program to read a list of 10 test scores, calculate the average score and print the scores that are above the average

9 program ProcessMarks; const NoOf Scores = 10; varScore1, Score2, Score3, Score4, Score5, Score6, Score7, Score8, Score9, Score10 : integer; Average : real; begin { Read the scores} readln (Score1); readln (Score2); readln (Score3); readln (Score4); readln (Score5); readln (Score6); readln (Score7); readln (Score8); readln (Score9); readln (Score10); ( Calculate the average} Average := (Score1 + Score2 + Score3 + Score4 + Score5 + Score6 + Score7+ Score8 + Score9 + Score10) ‘ NoOfScores; {print the scores above average} if Score1 > Average then writeln(Score1); if Score2 > Average then writeln(Score2);. if Score10 > Average then writeln(Score10) end. First, a program using separate variables for the test scores.

10 By using array, the program becomes much simpler and easier to read: ProgramProcessMarks; {Array version} constNoOfScores = 10; typeScoreArray = array[1..NoOfScores] of integer; varScore : ScoreArray; Average : real; i, Total : integer; begin {Read the scores} for i := 1 to NoOfScores do Readln(Score[ i ]); Total := 0; {Calculate the average} for i := 1 to NoOfScores do Total := Total + Score[ i ]; Average := Total / NoOfScores; {Print out scores above average} for i := 1 to NoOf Scores do if Score[ i ] > Average then writeln (Score[ i ]) end.

11 3.Processing arrays 3.1Loops for input and output 3.2Loops for assignment 3.3Summing an array 3.4Finding the largest or smallest value of an array 3.5Searching an array for a target value 3.6Copying an array

12 3.1.Loops for input and output v input e.g. for i := 1 to NoOf Scores do readln(Score[i]); v output e.g. for i := 1 to NoOfScores do writeln(Score[i]); readln(Score); writeln(Score);

13 3.2.Loops for assignment v to initialize the array Count to zero: for i := 1 to 10 do Count[ i ] := 0; v to design a card game, we need to initialize each element of the array CardNo to the value of its own index: for i := 1 to 52 do Count[ i ] := i ;

14 3.3.Summing an array v by adding the values of the elements, one at a time, to an accumulator: Total := 0; for i := 1 to 5 do Total := Total + Score[ i ];

15 3.4.Finding the largest & smallest value of an array v algorithm: 1.Largest First element 2.for each array element do 2.1 if the Current element > Largest thenLargest Current element v The procedure FindMax implements the algorithm procedure FindMax (Score : ScoreArray; NoOfScores : integer; var HighScore : ScoreType); vari : integer; begin HighScore := Score[ i ]; for i := 2 to NoOfScores do if Score[ i ] > HighScore then HighScore := Score[ i ] end;

16 3.5.Searching an array for a target value v e.g., we want to find out whether a certain person is a member of the Computer Club. v procedure Searching (Name : NameArray; Target : NameType; var Position : integer); var i : integer; begin Position := 0; for i := 1 to MaxSize do if Target = Name[ i ] then Position := i ; end;...... if Position > 0 then writeln (Target, ‘ is found in position ‘, i); else writeln (Target, ‘is not found.’)

17 3.6.Copying an array v if two arrays are declared the same array type, the contents of one array can be copied to the other array. v declaration: contMaxNo = 10; typeNumArray = array[1..MaxNo] of integer; varA, B : NumArray; C : array[1..MaxNo] of integer; v assignment statement: for i := 1 to MaxNo do A[ i ] := B[ i ];

18 4.Parallel Arrays v a data collection often contains items of different types. E.g., the data representing the performance of a class of students may consists of the student’s names, their exam scores and their grades. v to store these data, we can declare separate arrays with identical index type for names, scores and grades, as follows: constMaxNo = 40; typeNameArray = array[1..MaxNo] of string; ScoreArray = array[1..MaxNo] of real; GradeArray = array[1..MaxNo] of char; varName : NameArray; Score : ScoreArray; Grade : GradeArray; These 3 arrays are called parallel arrays. All data items with the same index belongs to a particular student.

19 4.[Cont’d] In the figure, related data items are with the same index 123 NameChan Tai WaiCheung Man YingChung Chi Ming 123 Score 89.5 65.2 90.2 123 Grade A CB v Example : write a program segment to display the name, the score and the grade of the student who is first in class.

20 4.[Cont’d] steps:1. find the index of the highest score 2. print out the name, the score and the grade with this particular index procedure FindMaxIndex (Score : MarkArray; var HighIndex : integer);....... vari : integer; {Main Program} begin FindMaxIndex(Score, HighIndex) HighIndex := 1 ; writeln(‘The student first in class is’, for i := 2 to MaxNo do Name[HighIndex]); if Score[ i ] > Score[HighIndex] writeln(‘The score obtained is ‘, then HighIndex := i ; Score[HighIndex]); end; Grade[HighIndex]);......

21 5.Two-dimensional Array v can be used to handle data represented in tabular form array [row index, column index] of base type e.g., type TableType = array[1..3, 1..5] of integer; var Table : TableType; represent a 3 by 5 table which can be pictured as follows: 12345 1 2 3 What metaphor can you think of to represent this?

22 5.Two-dimensional Array v can be used to handle data represented in tabular form Block ABlock BBlock CBlock DBlock E 3A 2A 1A 3B 2B 1B 3C 2C 1C 3D 2D 1D 3E 2E 1E 3/F 2/F 1/F Layout of Purple House

23 5.[Cont’d] v Example Declare a two-dimensional array and write a program segment to generate the contents of the following table: 12341234 24682468 3 6 9 12 4 8 12 16 5 10 15 20

24 5.[Cont’d]  Declaration: type TableType = array[1..4, 1..5} of integer; varTable : TableType;  Program Segment: for Row := 1 to 4 do for Col := 1 to 5 do Table[Row, Col] := Row * Col;

25 5.[Cont’d] v Declare a two-dimensional array to store the test scores for students in a class and write a procedure to calculate the total score of each student and the mean score of each test. You can assume that there are 10 students with three scores each. Student 1 Student 2 Student 3 Score 89 78 50 56 70 46 67 65 53 Student 9 Student 10 64 83 58 76 60 69

26 5.[Cont’d] v Declaration: type ScoreTable = array[1..110, 1..3] of integer; MeanArray = array[1..3] of integer; TotalArray = array[1..10] of integer; varScore : ScoreTable; {mean score of each test} TestMean : MeanArray; {total score of each student} TotalScore : TotalArray;

27 5.[Cont’d] v The procedure that processes the scores: procedure ProcessScores (Score : ScoreTable; var TestMean : MeanArray; var TotalScore : Total Array); varStudent, Test, Sum : integer; begin {Find the sum of each row} for Student := 1 to 10 do begin TotalScore[Student] := 0; for Test := 1 to 3 do TotalScore[Student] := TotalScore[Student] + Score[Student, Test]; end; {Finding the sum of each column and then the mean} for Test := 1 to 3 do begin Sum := 0 for Student := 1 to 10 do Sum:= /sum + Score[Student, Test]; TestMean[Test] := trunc(Sum / 10) end end;

28 6.Arrays as Parameters v The procedures headings in Section 3: procedure FindMax(Score : ScoreArray; NoOfScore : integer; var HighScore : ScoreType); procedure Searching (Name : NameArray; Target : NameType; var Position :integer);

29 6.Arrays as Parameters v The procedures headings in Section 3: procedure FindMax (Score : ScoreArray; NoOfScore : integer; varHighScore : ScoreType); procedure Searching (Name : NameArray; Target : NameType; varPosition : integer); v The modified procedure headings: procedure FindMax(varScore : ScoreArray; NoOfScores : integer; varHighScore : ScoreType); procedure Searching (varName : NameArray; Target : NameType; varPosition : integer);


Download ppt "ARRAYS 1.Basic Ideas 2.The Array Type 3.Processing Arrays 4.Parallel Arrays 5.Two-dimensional Array 6.Arrays as Parameters."

Similar presentations


Ads by Google