• Home
  • Queen's University
  • Software Specifications
  • Array-Component Assignment and Loop Invariants

Array-Component Assignment and Loop Invariants

CISC 223 - Assignment 8 (Winter 2014) Due: beginning of class (3:30 PM), Wednesday March 26 1. (4 marks) Use the array-component assignment axiom two times to find the (most general) sufficient pre-condition P for the following code fragment: ASSERT( P ) /* determine what is P */ A [k] = z; A[j] = 5; ASSERT( A[i] - A[j] >= z ) Above z is an integer variable, A is an array of integers and we assume that all the subscripts are within the range of subscripts for A. First write the assertion P using the notation from the array-component assignment axiom, and then rewrite P in a logically equivalent and simplified form that does not contain any notation (A | I + E) (as in examples on pp. 85-86 and in examples covered in class). 2. (6 marks) Assume a declarative interface where n and max are constant integers. Also A is an array of integers and we know that the entries in the segment A [0 : max] are defined. For nonnegative integers x and i, power (x, i) denotes xi (the i'th power of x), that is, power(x,0) == 1 and power(x, i+1) == x * power(x, i). (* denotes multiplication.) Consider the following (partial) correctness statement: ASSERT (1 <= n < max) { int j; j = 1; A[0] = 1; while (j < n) { A[j] = A[j-1] + power(2, j); j = j+1; } //end-while } ASSERT( ForAll(k = 0; k < n) A[k] == power(2, k+1) - 1 ) Choose a loop invariant and give a complete proof tableau by adding all the intermediate assertions. Clearly state any mathematical facts used. Also make an argument for termination by including a suitable assertion in the loop invariant. Regulations on assignments . The assignments may be done in groups consisting of one, two, three or four students. If more than one students are collaborating on an assignment, they must submit a single joint solution. . Clearly print (or type) the name(s) and student number(s), and course number, at the top of the first page. Additionally each student collaborating on an assignment must sign the top of the first page. . If the submission consists of more than one page, the pages must be stapled together. . Note: You are asked to write your solutions using non-erasable pen (or to type the solutions). Solutions written in pencil or erasable ink will be marked, but they will not be considered for remarking after the assignments are returned.