17+-+Memory+Models

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

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

Memory Models EECS 280 Programming and Introductory Data Structures 1

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

View Full Document
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:
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++){ //

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

View Full Document
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.
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.

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

View Full Document
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
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,

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

View Full Document
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.
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

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

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

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online