1 / 35

TL: Arrays

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 ) ….

kimo
Download Presentation

TL: 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. TL: Arrays Programming Fundamentals 13 Feliks Kluźniak

  2. There are two principal ways to think about an array ar: TL: Arrays

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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.

More Related