15+-+Memory+Models - 5/21/2009 Invariants Recall: Checking...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
5/21/2009 1 Memory Model Ve 280 Programming and Introductory Data Structures Memory Models 1 Invariants Recall: Checking for Representation Invariants y Last time, we showed how to confirm that an array was sorted, with no duplicates. y Base cases: y An array of zero elements is sorted and has no duplicates 2 y An array of one element is sorted and has no duplicates y Inductive step: An array of N elements (N>=2) is sorted and has no duplicates if: y A[0] < A[1] y The array A[1]. ..A[N-1] is sorted and has no duplicates. Invariants Checking for Representation Invariants bool strictSorted(int a[], int size) { // REQUIRES: a has size elements // EFFECTS: returns true if a is sorted // with no duplicates if (size <= 1) return true; // base cases 3 for (i=0; i<size-1; i++){ // iterative step if (a[i] >= a[i+1]) { return false; } } return true; } Invariants Checking for Representation Invariants (Continued) y If the array is unsorted, we only have to find duplicates. But, this is harder. y Base case: y An array with 0 elements has no duplicates. y Inductive Step: 4 An array with N elements (N>0) has no duplicates if: y A[0] doesn't appear anywhere else in the array. y The remainder of the array has no duplicates. y The basic strategy is, for each element, see if there are any elements to the "right" that are equal. y To do this, we'll have to write a "nested loop". Invariants Checking for Representation Invariants (Continued) bool noDuplicates(int a[], int size) { for (int i 0; i < size; i++) { i is the "outer loop index“. It points to the element currently under consideration. 5 for (int i = 0; i < size; i++) { // Check elements to the right of i } } The "outer loop" is the one that considers each element of the array. Invariants Checking for Representation Invariants (Continued) bool noDuplicates(int a[], int size) { for (int i = 0; i < size; i++) { // Check elements to the right of i } } 6 y For each element to the right, we need to check if it is a duplicate of i : i Check all in here
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5/21/2009 2 Invariants Checking for Representation Invariants (Continued) bool noDuplicates(int a[], int size) { for (int i = 0; i < size; i++) { // Check elements to the right of i } } 7 y For each element to the right, we need to check if it is a duplicate of i : i Check all in here We need a new loop index ( j ), which starts at i+1 and goes until size-1 . Note that this interval might be empty, and it will be if i==size . Invariants Checking for Representation Invariants (Continued) bool noDuplicates(int a[], int size) { for (int i=0; i<size; i++) { for (int j=i+1; j<size; j++) { if (a[i] == a[j]) return false; 8 } } return true; } If these two elements are equal, there are duplicates, so return false. If we fall through the loop, there are no
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

15+-+Memory+Models - 5/21/2009 Invariants Recall: Checking...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online