17+-+Memory+Models - EECS 280 Programming and Introductory...

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

View Full Document Right Arrow Icon
Memory Models EECS 280 Programming and Introductory Data Structures 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Invariants Recall: Checking for Representation Invariants 2 Last time, we showed how to confirm that an array was sorted, with no duplicates. Base cases: An array of zero elements is sorted and has no duplicates An array of one element is sorted and has no duplicates Inductive step: An array of N elements (N>=2) is sorted and has no duplicates if:
Background image of page 2
Invariants Checking for Representation Invariants 3 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 for (i=0; i<size-1; i++){ //
Background image of page 3

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

View Full DocumentRight Arrow Icon
Invariants Checking for Representation Invariants (Continued) 4 If the array is unsorted, we only have to find duplicates. But, this is actually harder. Base case: An array with 0 elements has no duplicates. Inductive Step: An array with N elements (N>0) has no duplicates if: A[0] doesn't appear anywhere else in the array.
Background image of page 4
Invariants Checking for Representation Invariants (Continued) 5 bool noDuplicates(int a[], int size) { for (int i = 0; i < size; i++) { // Check elements to the right of i } } i is the "outer loop index“. It points to the element currently under consideration. The "outer loop" is the one that considers each element of the array.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Invariants Checking for Representation Invariants (Continued) 6 bool noDuplicates(int a[], int size) { for (int i = 0; i < size; i++) { // Check elements to the right of i } } 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 6
Invariants Checking for Representation Invariants (Continued) 7 bool noDuplicates(int a[], int size) { for (int i = 0; i < size; i++) { // Check elements to the right of i } } For each element to the right, we need to check if it is a duplicate of i : i Check all in her We need a new loop index ( j ), which starts at i+1 and goes until size-1 . Note that this interval might be empty,
Background image of page 7

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

View Full DocumentRight Arrow Icon
Invariants Checking for Representation Invariants (Continued) 8 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; } } return true; } If these two elements are equal, there are duplicates, so return false. If we fall through the loop, there are no duplicates, so return true.
Background image of page 8
Invariants Checking for Representation Invariants (Continued) 9 Writing these “checker” functions is of more than academic interest – you can use them for defensive programming. So, you can write a private method to ensure that each invariant is true ( before exiting , or after entering, each method): bool repOK(); // EFFECTS: returns true if the // rep. invariant holds
Background image of page 9

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

View Full DocumentRight Arrow Icon
Invariants Checking for Representation Invariants (Continued) 10 Next, add the following code right before returning from any function that modifies any of the representation:
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 35

17+-+Memory+Models - EECS 280 Programming and Introductory...

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

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