350 likes | 533 Views
TL: Arrays. Programming Fundamentals 13 Feliks Klu ź niak. There are two principal ways to think about an array ar :. There are two principal ways to think about an array ar : I t is a collection of N variables of some type T that share a common name ( ar ) ….
E N D
TL: Arrays Programming Fundamentals 13 Feliks Kluźniak
There are two principal ways to think about an array ar: TL: Arrays
There are two principal ways to think about an array ar: • It is a collection of N variables of some type T that share a common • name (ar) … TL: Arrays
There are two principal ways to think about an array ar: • It is a collection of N variables of some type T that share a common • name (ar) and have the convenient property that the choice about • which of the variables is accessed can easily be made at runtime • (by using the notation ar[ exp ] ). TL: Arrays
There are two principal ways to think about an array ar: • It is a collection of N variables of some type T that share a common • name (ar) and have the convenient property that the choice about • which of the variables is accessed can easily be made at runtime • (by using the notation ar[ exp ] ). • We normally think of an array as a block of memory, • and it is usually implemented in this way, but in most cases • this is not an essential property. TL: Arrays
There are two principal ways to think about an array ar: • It is a collection of N variables of some type T that share a common • name (ar) and have the convenient property that the choice about • which of the variables is accessed can easily be made at runtime • (by using the notation ar[ exp ] ). • It is a single variable whose value is a function from the integer • range [ 0 .. N ) to T. TL: Arrays
There are two principal ways to think about an array ar: • It is a collection of N variables of some type T that share a common • name (ar) and have the convenient property that the choice about • which of the variables is accessed can easily be made at runtime • (by using the notation ar[ exp ] ). • It is a single variable whose value is a function from the integer • range [ 0 .. N ) to T. • Instead of writing ar( i ) we write ar[ i ] . TL: Arrays
There are two principal ways to think about an array ar: • It is a collection of N variables of some type T that share a common • name (ar) and have the convenient property that the choice about • which of the variables is accessed can easily be made at runtime • (by using the notation ar[ exp ] ). • It is a single variable whose value is a function from the integer • range [ 0 .. N ) to T. • Instead of writing ar( i) we write ar[ i ] . • In this view, when we change one element of the array, we • change the value of the entire array: we now have a different • function. TL: Arrays
An array has three important attributes: the type of each element (or of the value of the function); 3 a[ 3 ] 2 a[ 1 ] a[ 0 ] = 0 a[ 1 ] = 2 a[ 2 ] = 1 a[ 3 ] = 3 a[ 4 ] = 1 1 a[ 2 ] a[ 4 ] a[ 0 ] 1 2 3 4
An array has three important attributes: the type of each element (or of the value of the function); the size of the array, i.e., the number of elements (or the domain of the function); 3 a[ 3 ] 2 a[ 1 ] a[ 0 ] = 0 a[ 1 ] = 2 a[ 2 ] = 1 a[ 3 ] = 3 a[ 4 ] = 1 1 a[ 2 ] a[ 4 ] a[ 0 ] 1 2 3 4
An array has three important attributes: the type of each element (or of the value of the function); the size of the array, i.e., the number of elements (or the domain of the function); the particular values of the elements (or the function itself). 3 a[ 3 ] 2 a[ 1 ] a[ 0 ] = 0 a[ 1 ] = 2 a[ 2 ] = 1 a[ 3 ] = 3 a[ 4 ] = 1 1 a[ 2 ] a[ 4 ] a[ 0 ] 1 1 2 2 3 3 4 4
An array has three important attributes: the type of each element (or of the value of the function); the size of the array, i.e., the number of elements (or the domain of the function); the particular values of the elements (or the function itself). We will treat pt. 3 as the value of an array variable. The other points will be its type. 3 a[ 3 ] 2 a[ 1 ] a[ 0 ] = 0 a[ 1 ] = 2 a[ 2 ] = 1 a[ 3 ] = 3 a[ 4 ] = 1 1 a[ 2 ] a[ 4 ] a[ 0 ] 1 2 3 4
The type of each element (or of the value of the function). The size of the array, i.e., the number of elements (or the domain of the function). The two attributes of the type of an array are independent of each other. TL: Arrays
The type of each element (or of the value of the function). The size of the array, i.e., the number of elements (or the domain of the function). The two attributes of the type of an array are independent of each other. The type of the element is the more basic one, as it will be relevant in every context in which we access an element, e.g., v := a[ i ]; a[ i ] := 7 The size is not always relevant, as long as we know the index expression is in bounds. (Equivalently: that the argument of the function is in the domain.) TL: Arrays
The type of an element is the more basic attribute, size is not always relevant. In TL, each array ar carries information about its size, which is the value of the expression size( ar ). TL: Arrays
The type of an element is the more basic attribute, size is not always relevant. In TL, each array ar carries information about its size, which is the value of the expression size( ar) . This is implemented as follows: The address of the array the value of N a[ 0 ] : a[ 1 ] : a[ 1 ] : So size( a ) = N, where N is some concrete integer …. a[ N – 1 ] : TL: Arrays
The type of an element is the more basic attribute, size is not always relevant. In TL, each array ar carries information about its size, which is the value of the expression size( ar ). So if an array is passed as an argument to a subroutine, the subroutine can access its size … TL: Arrays
The type of an element is the more basic attribute, size is not always relevant. In TL, each array ar carries information about its size, which is the value of the expression size( ar ). So if an array is passed as an argument to a subroutine, the subroutine can access its size: arrays of different sizes can be handled by the same subroutine. TL: Arrays
The type of an element is the more basic attribute, size is not always relevant. In TL, each array ar carries information about its size, which is the value of the expression size( ar ). So if an array is passed as an argument to a subroutine, the subroutine can access its size: arrays of different sizes can be handled by the same subroutine. However, the type of the elements must always be the same, because our language is (statically) strongly typed. TL: Arrays
And so, we can speak of the generictypeof an array, one that is independent of its size. In TL such a type is declared as follows: typeNameOfNewArrayType=NameOfElementType[] TL: Arrays
And so, we can speak of the generictype of an array, one that is independent of its size. In TL such a type is declared as follows: typeNameOfNewArrayType=NameOfElementType[] For example, type ArrayOfIntegers = int [] ; type ArrayOfBooleans = bool [] TL: Arrays
And so, we can speak of the generictype of an array, one that is independent of its size. In TL such a type is declared as follows: typeNameOfNewArrayType=NameOfElementType[] For example, type ArrayOfIntegers = int [] ; type ArrayOfBooleans = bool [] . There is one predefined generic array type, string, which corresponds to the declaration type string = char [] . TL: Arrays
And so, we can speak of the generictype of an array, one that is independent of its size. In TL such a type is declared as follows: typeNameOfNewArrayType=NameOfElementType[] For example, type ArrayOfIntegers = int [] ; type ArrayOfBooleans = bool [] . There is one predefined generic array type, string, which corresponds to the declaration type string = char [] . Unlike other array types, the type string has associated literals. For example const s = “\”string\”\n”this is an array of 9 characters (!) TL: Arrays
To give an array a size, we define a concrete array type, which is an instantiation of some generic type: typeNameOfConcreteType=NameOfGenericType[ size] TL: Arrays
To give an array a size, we define a concrete array type, which is an instantiation of some generic type: typeNameOfConcreteType=NameOfGenericType[ size] For example: const dimensions = 3; constnum_of_events = 32; type Vector = ArrayOfIntegers[ dimensions ] ; type Flags = ArrayOfBooleans[ num_of_events ] TL: Arrays
To give an array a size, we define a concrete array type, which is an instantiation of some generic type: typeNameOfConcreteType=NameOfGenericType[ size] For example: const dimensions = 3; constnum_of_events = 32; type Vector = ArrayOfIntegers[ dimensions ] ; type Flags = ArrayOfBooleans[ num_of_events ] var v1 : Vector; var v2 : Vector; varevent_flags : Flags; TL: Arrays
Please note: • An array variable cannot be directly assigned to another array • variable: we must do it element by element. TL: The joys of recursion
Please note: • An array variable cannot be directly assigned to another array • variable: we must do it element by element. • Arrays cannot be directly compared with =, !=, < etc. : we must do • it element by element. Don’t forget that strings are arrays, too! TL: The joys of recursion
Please note: • An array variable cannot be directly assigned to another array • variable: we must do it element by element. • Arrays cannot be directly compared with =, !=, < etc. : we must do • it element by element. Don’t forget that strings are arrays, too! • A variable must have a concrete type, so one cannot declare • varmy_string : string TL: The joys of recursion
Please note: • An array variable cannot be directly assigned to another array • variable: we must do it element by element. • Arrays cannot be directly compared with =, !=, < etc. : we must do • it element by element. Don’t forget that strings are arrays, too! • A variable must have a concrete type, so one cannot declare • varmy_string : string • However, the following is perfectly all right (since the compiler can • determine the length of the string): • constmy_string = “Hello, world!\n” TL: The joys of recursion
Please note: • An array variable cannot be directly assigned to another array • variable: we must do it element by element. • Arrays cannot be directly compared with =, !=, < etc. : we must do • it element by element. Don’t forget that strings are arrays, too! • A variable must have a concrete type, so one cannot declare • varmy_string : string • However, the following is perfectly all right (since the compiler can • determine the length of the string): • constmy_string = “Hello, world!\n” • We can, of course, access the elements of this constant array in the • usual manner. For example, the value of the boolean expression • my_string[ 1 ] = ‘e’ • is WHAT? TL: The joys of recursion
Please note: • An array variable cannot be directly assigned to another array • variable: we must do it element by element. • Arrays cannot be directly compared with =, !=, < etc. : we must do • it element by element. Don’t forget that strings are arrays, too! • A variable must have a concrete type, so one cannot declare • varmy_string : string • However, the following is perfectly all right (since the compiler can • determine the length of the string): • constmy_string = “Hello, world!\n” • We can, of course, access the elements of this constant array in the • usual manner. For example, the value of the boolean expression • my_string[ 1 ] = ‘e’ • is true . TL: The joys of recursion
A note about “debugging” In order to see what is going on in your program, you can insert the special statement TRACE at various places. When the statement is encountered during execution, information will be printed on stderr: the information will consist of the line number and the values of the expressions you included in the statement. Example: TRACE “a = “, a, “, b = “, b, “, (a = b) = ”, a = b When executed, this will print something like: a = 17, b = 18, (a = b) = false TL: Arrays
A note about “debugging” • In order to see what is going on in your program, you can insert the special statement TRACE at various places. When the statement is encountered during execution, information will be printed on stderr: the information will consist of the line number and the values of the expressions you included in the statement. • Example: • TRACE “a = “, a, “, b = “, b, “, (a = b) = ”, a = b • NOTE: • The number of expressions in a TRACE statement is not limited, but must be greater than zero. Each expression must be of type int, char, bool or string. • How many expressions are there in this example? TL: Arrays
A note about “debugging” In order to see what is going on in your program, you can insert the special statement TRACE at various places. When the statement is encountered during execution, information will be printed on stderr: the information will consist of the line number and the values of the expressions you included in the statement. Example: TRACE “a = “, a, “, b = “, b, “, (a = b) = ”, a = b NOTE: The number of expressions in a TRACE statement is not limited, but must be greater than zero. Each expression must be of type int, char, bool or string. This is just to help you make your program right: it should not be used for output in the final version of the program. However, for the time being you can use it to print results.