Thursday 4/9/02 Week 12

Chapter 9

Student notes

 

Objectives

Arrays

Sorting Arrays

 

1.  What is an array?

·        records contain

·         

·        arrays contain

·         

·        an array groups together

·         

·         

·        TYPE FloatArray IS ARRAY (1..8) OF Float;

X: FloatArray;

·        This declaration creates a type called FloatArray which will hold 8 float values together

 

2.   Getting at elements in an array

·        we are able to reference individual elements in an array by using the

·         

·         

·        X(1) is a                                   and refers to the element at position 1 in the array

·        see page 385 for a diagram

·        page 386 shows how to work with array elements

 

3.  Operations on Arrays

·         

·         

·        in the following examples we assume that A is an array type, B is an array of the same type as A, that C is a variable compatible with the element type of A, and that i is an expression that is compatible with the subscript type of A

·        Store: inserts a value into the array    A(i) := C;

·        Retrieve: retrieves element i of array A and copies it into C.      C := A(i);

·        Assignment: copies all the elements from one array to another, assuming both arrays are of the same type          A := B;

·        Equality test: equality and inequality operators can also be used to compare two arrays if they are of the same type.             A = B; and A /=B ;

 

4.  Aggregate Array Assignment

·        we can do aggregate array assignments with arrays, just like we can do with records

·        there are three ways to fill an entire array

·         

·         

·         

·         

·         

·         

·        if A is an array with 5 values and we know all 5 values in advance we can use

A := (1.9, 2.3, 3.6, 4.1, 5.7);

·        This is called an anonymous assignment. As with records, this is legal, but dangerous.

·        Another way is to specify the subscript\

A := (1=>1.9, 2=> 2.3, 3=>3.6, 4=>4.1, 5=>5.7);

·        This is obviously cumbersome

·         

·         

·         

·         

·        initializing can be done with a FOR loop

FOR i IN 1..MaxSize LOOP

  A(i) := 0.0;

END LOOP;

·        the initialization can also be done with the simple statement:

A := (1..MaxSize => 0.0);

·        What bout initializing some values, but not all?

A := (1.0, 27.0, 35.0, -4.0, 15.0, OTHERS => 0.0);

this initializes the first five elements to specific values, then assigns the rest of the values to 0.0

 

            A:= (1 => 1.0, 3=> 27.0, 5 => 35.0, OTHERS => 0.0);

initializes the values at subscripts 1, 3, and 5 to specific values and assigns everything else to 0.0

 

5.  The array subscript

·        an array’s range may be defined by

·         

·        usually we use positive integer starting from 1 or 0, but this is not a requirement

 

 

 

 

6.  More work with array subscripts

·        page 391

·        FOR loops and arrays are often seen together

·        since arrays have a discreet size, we often use FOR loops to make sure that we hit every element in an array

·        page 392

·        arrays can be processed in random or sequential order.

 

 

 

 

 

 

 

 

7.  Copying and Comparing arrays

·        MaxSize : CNSTANT Positive := 100;

SUBTYPE Index IS Positive RANGE 1..MaxSize;

TYPE TestArray IS ARRAY (Index) OF Float;

W : TestArray;

X : TestArray;

Y : TestArray;

 

X := Y; copies each value from Y into X so X(1) = Y(1), X(2) = Y(2) after the assignment

 

IF X = Y THEN

   Ada.Text_IO.Put(Item => “equal”);

ELSE

   Ada.Text_IO.Put(Item => “not equal”);

END IF;

is a legal IF statement

 

Z : ARRAY (Index) OF Float; is a legal declaration in Ada, BUT Z is NOT of type TestArray therefore

Z := Y;

X := Z;

IF Z = X THEN ...

are all incorrect and will cause compilation errors

 

·        Note that the elements are compatible so comparisons and assignment statements between elements are fine.

 

Z(3) := X(2); --fine

IF X(5) /= Z(6) THEN – fine

 

8.  Arrays as Parameters

·        arrays may be passed as parameters just like any other variable

·        page 399 for example

 

9.  Reading only part of an array

·        part of the advantage of arrays is that we can store a group of related data together, but we may not need to look at all of this data every time

·        page 404 gives a few examples of reading only a section of an array

 

10.  Searching an array

·        MaxSize : COSTANT Positive := 250;

MaxScore : CONSTANT Positive := 100;

SUBTYPE ClassIndex IS Positive RANGE 1..MaxSize;

SUBTYPE ClassRange IS Natural RANGE 0..MaxSize;

SUBTYPE ScoreRange IS Natural RANGE 0..MaxScore;

TYPE ScoreArray IS ARRAY (ClassIndex) OF ScoreRange;

 

Score : ScoreArray;

ClassSize : ClassRange;

 

·        We want to search the array Score to see if a specific score exists in the array

·        the specific score is called a

·         

·        If we find a match, then we want to return the subscript as the search result

·        page 411

 

11.  Sorting an Array

 

  • Bubble Sort

 

 

 

 

 

·         

·        Now here is another intuitive (but not very fast) method of sorting called selection sorting

·        pg 412-413

 

12. Big-O notation

·        How fast is your search or sort going to be?

·        we analyze sorting and searching algorithms with mathematical notation

·        we can count the number of times each element will be examined as expression of N

·        Big-O refers to the Order of Magnitude and is written as O(N)

·        the select sort algorithm has a Big-O of N2

·        pg 414

·        in this book all of our arrays will be very small, but as you start dealing with larger arrays or other data structures the Big-O of your searching and sorting functions becomes very important

 

13.  Arrays of Records

·        It is perfectly legal to have an array of records

·        TYPE ScoreRecord IS RECORD

  Name : StudentName;

  Score : ScoreRange;

END RECORD;

TYPE ScoreArray IS ARRAY (ClassIndex) OF ScoreRecord;

 

·        If we wanted to sort an array of student records, we would have to choose one of the record fields to sort by

·         

·         

·         

·         

·         

·         

·         

·         

·         

·        pg 417