CSC195, Class 18: Assertions about Arrays Overview: * What is an array? Revisited * Lots and lots of notation * More notation: Multidimensional arrays * Array pictures * An application: Partitioning Notes: * Posses and need-blind admissions * I plan to distribute the first exam tomorrow or Friday. * Professor Gum will run Friday's lab (I hope). * Obligatory joke: Why are logicians like C programmers? * Reminder: Notation is intended to be a convenience. Once you understand what it's saying, it will be a help rather than a hindrance. ---------------------------------------- What is an array? (Yesterday) * A contiguous block of storage (implementation perpsective) * A "random access" data structure What is an array? (Gries) * A partial function from indices to elements * A bunch of similarly-named subscripted variables Just as in every other language, we need a notation for arrays and the operations upon them. * Declare arrays (implicit) b[1:2] = (5,3) This is an assertion about the array b b[1:3] = (x,y,z) * Refer to single elements of an array b[10] Assert: b[10] = 5 Assert: b[10] > b[11] Assert: b[i] < i Assert: For all i b[i] < i Assert: For at least one i b[i] < i Assert: Count(i s.t. b[i] > i) >= 5 * We can traditionally change the values of arrays. In this notation, we don't change arrays, we build new ones that are similar Instead of b[i] := 5; (b; i:5) We can use this whereever we'd expect an array a = (b; i:5) "a is the same as b, except in position i where a is 5" (b; i:5) = (1,2,3,4,5) If this holds, what does this tell us about b and i? b contains five elements. The first four elements are 1,2,3,4 If b is indexed starting at 1, i is 5 (b; i:5)[i] = 5 (b; i:5)[j] = b[j] , j!=i * What else can we do? We can take subranges of an array b[1..100] = a b[5..7] = ( 3, 4, 5 ) * The notation is context-sensitive b[1..100] = a Elements 1 through 100 of b are the same as elements 1..100 of a b[1..100] = a For all i, 1 <= i <= 100; b[i] = a Increase your fun with multidimensional arrays b[2:5][1:7] = ( (1,1,1,1) (2,2,2,2,2,2,2) ) Unlike C, Gries notation lets you fill in only sum of the array parameters (but from left to right) If b is a two-dimensional array, b[i] is a one-dimensional array Gries notation is also strictly typed. You cannot treat a two-dimensional array as a one-dimensional array. Extracting values follows the logical pattern b[i][j] How do we say "something just like b, except that 5 is at position [3][4]?" (b; [3][4]:5) (fourd: [1][1][1][1]:6) i ----I---- Recursive definition of what these things mean: (b; :5) is 5 (b; [i]I:v)[i] is (b[i]; I:v) (b; [i]I:v)[j] is b[j] if j!=i 1 s l n +-----------+-------------+--------------+ | <= k | | > k | +-----------+-------------+--------------+