130 likes | 196 Views
Composition Steps. 03 / 20 / 2013. Sort an array of numbers. int main( int argc , char* argv []) { int numbers[SIZE] = {2,5,3…} // COMPOSE IN MODULE HERE }. Sort an array of numbers. int main( int argc , char* argv []) { int numbers[SIZE] = {2,5,3…} // COMPOSE IN MODULE HERE
E N D
Composition Steps 03/20/2013
Sort an array of numbers int main(intargc, char* argv[]) { int numbers[SIZE] = {2,5,3…} // COMPOSE IN MODULE HERE }
Sort an array of numbers int main(intargc, char* argv[]) { int numbers[SIZE] = {2,5,3…} // COMPOSE IN MODULE HERE } Source parameters // precondition: numbers is array of ints // postcondition: numbers[0] <= ... <= numbers[SIZE-1] Preconditions Postconditions
Sort an array of numbers int main(intargc, char* argv[]) { int numbers[SIZE] = {2,5,3…} // COMPOSE IN MODULE HERE } int* quicksort(int* input, int p, int r) void mergesort(char* input, int p, int r) void bubblesort(int* input) Available modules Starting slightly simpler … no structs
Quicksort composition int* quicksort(int* input, int p, int r) Target parameters // precondition: input != NULL // precondition: input is array of ints // precondition: p != r // postcondition: input[0] <= ... <= input[SIZE-1] Preconditions Postconditions
Quicksort composition int* quicksort(int* input, int p, int r) Target parameters // precondition: input != NULL // precondition: input is array of ints // precondition: p != r // postcondition: input[0] <= ... <= input[SIZE-1] Preconditions Postconditions
Quicksort composition int* quicksort(int* input, int p, int r) // precondition: input != NULL // precondition: input is array of ints // precondition: p != r // postcondition: input[0] <= ... <= input[SIZE-1] Preconditions Postconditions ROLE: buffer ROLE: High Index ROLE: Low Index
Quicksort composition int* quicksort(int* input, int p, int r) • Checking preconditions: • Source buffer is array of integers • Target buffer is array of integers • No transform required • Checking postconditions: • Postconditions match exactly • No mismatch detected • No transform required • [code] • Insert (wrapper | function pointer | method • invocation) Composition Steps Generated
Mergesort Composition void mergesort(char* input, int p, int r) Target parameters // precondition: input != NULL // precondition: input is array of chars // precondition: p != r // postcondition: input[0] <= ... <= input[SIZE-1] Preconditions Postconditions
Mergesort composition void mergesort(char* input, int p, int r) • Checking preconditions: • Source buffer is array of integers • Target buffer is array of chars • Transform required • Checking postconditions: • Postconditions do not match • Transform required • [code] • Int2Str • Insert (wrapper | function pointer | method) • Str2Int Composition Steps Generated
Bubblesort Composition void bubblesort(int* input) Target parameters // precondition: input != NULL // precondition: input is array of ints // precondition: p != r // postcondition: input[0] <= ... <= input[SIZE-1] Preconditions Postconditions
Bubblesort composition void bubblesort(int* input) • Checking preconditions: • Source buffer is array of integers • Target buffer is array of integers • No transform required • Checking postconditions: • Postconditions match • No transform required • [code] • Insert (wrapper | function pointer | method) Composition Steps Generated
New questions! • Is there a time when a function invocation is better than a pointer / wrapper? • Transforms may lend themselves better to wrappers – self contained • Function pointer “messier” as ‘anything’ can be passed in • Getting into soft metrics?