fall 2001(c)opyright brent m. dingle 2001 arrays brent m. dingle texas a&m university chapter 9...

25
Fall 2001 (c)opyright Brent M. Ding le 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)

Upload: julian-garrison

Post on 31-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Arrays

Brent M. DingleTexas A&M UniversityChapter 9 – Sections 1 and 2(and some from Mastering Turbo Pascal 5.5, 3rd Edition by Tom Swan)

Page 2: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Many things all the same

So we can now create new data types.But what if we have a bunch of things that are all the same type and sort of ‘go together?’ e.g. 4 Test scores

How/ can we store those things ‘together’ in some way?

Page 3: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Grades

Say we wanted to write a program that read in 4 test scores for a student and we were to calculate the average of them.Currently we would need 4 variables to hold the test scores – one for each test

Page 4: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Grades Program 1 – No ArraysPROGRAM Grades1;VAR grade1, grade2 : real; grade3, grade4 : real; avg : real;

PROCEDURE GetGrade(var grade : real; grade_num : integer);

BEGIN Write(‘Enter grade ’,

grade_num, ‘ --> ‘); Readln(grade);END;

BEGIN { main } GetGrade(grade1, 1); GetGrade(grade2, 2); GetGrade(grade3, 3); GetGrade(grade4, 4);

avg := grade1+grade2+grade3+grade4;

avg := avg / 4;

Writeln(‘Average = ‘, avg);END.

Page 5: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

A better way

It almost seems silly to have 4 different variables that are so similar.And what if instead of 4 grades, there were 20? or 40? or 100?Would we really want to have 20, 40 or 100 variables?Think of all the typing… =)Fortunately there is a better way.

Page 6: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

What is an array?

Arrays are collections of values all of the same type.Remember how a variable was like a box?

Page 7: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Variables as Boxes

Page 8: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Boxes are like drawers or shelves

Page 9: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Arrays are like a stack of boxes – or shelves that are numbered

Page 10: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Properties

Every shelf is the same type.Every shelf has the same name BUT a unique number in square brackets.Each shelf has its own value.

Page 11: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Indexing

In the picture our indexing starts at 0.Indexing of arrays usually starts at 0 or 1.But it can start and end at any integer value (enumerated types can also be used as indices).Notice where the indexing starts is extremely important when reading and writing code.

Page 12: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Indexing and math

Indexing is very similar to subscripting in math.Notice the similarities of FOR loops of computer science and Summations of Math and how they both use indexing to simplify/compress the notation.Though computer science allows for more ‘options.’

Page 13: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Declaring Arrays

The best way to declare an array in Pascal is to create a Type for the desired array:TYPE ARY_0_TO_4_REAL = array [0..4] of real;VAR x : ARY_0_TO_4_REAL;

So in the above we would have an array named x of type real containing 5 elements where the first element is at index = 0.Notice usually you will pick a better TYPE name, but for illustrative purposes ARY_0_TO_4_REAL was used.

Page 14: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Accessing the values in arrays

To access the values stored in an array you use the array name and the index of the desired element, where the index is placed in square brackets.So if we wanted to display the element at index 3 we would say:Writeln(x[3]);Let’s now go back to our Grade program, but this time we will use an array named grade of type real containing 4 elements where the first element is at index = 1.

Page 15: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Grades Program 2 – ArraysPROGRAM Grades2;TYPE GRADE_ARRAY = array[1..4] of real;VAR grade : GRADE_ARRAY; avg : real; index : integer;

PROCEDURE GetGrade(var grade : real; grade_num : integer);BEGIN Write(‘Enter grade ’, grade_num, ‘ --> ‘); Readln(grade);END;

Page 16: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Grades Program 2 – Arrays(continued)

BEGIN { main } avg := 0; FOR index := 1 to 4 DO BEGIN GetGrade(grade[index], index); avg := avg + grade[index]; END; avg := avg / 4;

Writeln(‘Average = ‘, avg);END.

Page 17: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Downsides to ArraysFunctions cannot return arrays.You may go out of bounds of the array.

i.e. You use an index that is invalid – out of range.

For example if x was an array of 5 elements of type real with the first index starting at 1:

x[0] would be out of bounds x[34] would be out of bounds If index = 5 then

x[index] would be valid but x[index + 1] would NOT be valid

Sometimes the compiler catches these errors (as in the first 2 cases above), sometimes it does not (as in the x[index + 1] case. Bad things may result.

Page 18: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Problems (not graded)

pages 333, 334 1, 2, 3, 4, 5, 6, 7, 8

Notice that in 7 and 8 you see arrays may be declared without using TYPE, however they also illustrate some of the problems that result (6 also hints at another problem).And in problem 8 don’t worry so much about the compiler directive.

Page 19: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Array size

Notice the size of every array is declared – or rather we cannot change the size of an array while the program is running.Sometimes it is said that array size is static (or non-dynamic).

Page 20: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Array Initialization

Notice that the value of each array element is NOT automatically initialized, so it is a good idea to run through a for loop to initialize every array.EX:FOR i := 1 to array_size DOBEGIN array_name[ i ] := initial_value;END;

Page 21: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Using ArraysOften we may not know when writing the program how many items will need to be stored in an array,But we may know the maximum number of items that will ever be stored (or we chose a maximum).So sometimes we create an array from 1 to max_num items, initialize it to ‘invalid’ values and then use it as we see fit.This can waste a great deal of memory, but it works.See the programming example on pages 344-346 to see how this works.This should also illustrate how partially filled and empty arrays come to be.

Page 22: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Memory efficiency

Memory used to be difficult to come by, today that is not so much a problem.However needlessly wasting memory can slow down a program.Some ways to save on memory usage are to limit the size of arrays to as small as possible,

but still functional use variable parameters over value

parameters when it is SAFE to do so.

Page 23: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

Accessing Arrays – ways to

Accessing an array in order, say from the first element to the last is called sequential accessing. This is usually done using loops

a for loop is most common.

Accessing an array in a fashion say like middle element, 3rd element, 2nd element, 23rd element, 1st element is called random accessing since you do not know which element will be accessed next.

Page 24: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

More Problems (not graded)

pages 351 – 353 9,10, 11, 12, 13 14, 17

Page 25: Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal

Fall 2001 (c)opyright Brent M. Dingle 2001

End Arrays

Chapter 9.1, 9.2 now complete.