Pre/Post Condition Discussion 03/13/2013. Quicksort int main(int argc, char* argv[]) { int numbers[SIZE] = {2,5,3…} struct Sorter s; // initialize generic.

Slides:



Advertisements
Similar presentations
Back to Sorting – More efficient sorting algorithms.
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Lab class 10: 1. Analyze and implement the following merge-sorting program. //import java.lang.*; public class MergeSorter { /** * Sort the elements of.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
A file reminder Files are used to store data and information They are manipulated through a pointer to FILE (FILE *) This pointer is returned when opening.
Chapter 7 Sorting Part I. 7.1 Motivation list: a collection of records. keys: the fields used to distinguish among the records. One way to search for.
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
MergeSort (Example) - 1. MergeSort (Example) - 2.
CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Stack buffer overflow
Functional Design and Programming Lecture 4: Sorting.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
S: Application of quicksort on an array of ints: partitioning.
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Arrays and Pointers in C Alan L. Cox
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Some Example C Programs. These programs show how to use the exec function.
Pre/Post Condition Logic 03/06/2013. Agenda Hoare’s Logic Overview Application to Pre/Post Conditions.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Code Update 01/16/2013. Agenda OOP API Update (Semi-)Automated Composition.
ITEC 320 C++ Examples.
C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
File IO and command line input CSE 2451 Rong Shi.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Chapter 6 Sorting. Agenda Swapping two values in array of int Bubble sort O(n 2 ) Swapping/sorting an array of Object Swapping/sorting a Vector of Object.
Generic Functions1 Generic Functions: A generic function is one that can work on any underlying C data type. Generic functions allow us to reuse programs.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Cryptography.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Data Types Storage Size Domain of all possible values Operations 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
Sorting divide and conquer. Divide-and-conquer  a recursive design technique  solve small problem directly  divide large problem into two subproblems,
M.Sc. Seminar - Keren Lenz Supervisor - Dr. Yossi Gil July 1 st 2007 Simple and Safe SQL Queries with C++ Templates A RA R AT -
C:\Temp\Templates 4 5 Use This Main Program 6.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPTER 11.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
QUICKSORT Quicksort is a sort-in-place algorithm that uses “divide-and-conquer”. First, partition the array into two subarrays A and B such that all in.
Project Summary Develop methodology and framework to automatically compose software modules Target evolutionary computation as an algorithm to search for.
Arrays and Sorting. Process Open a file that contains integers, one per line. Read each line, convert to short and store each into an array Sort the array.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
5.13 Recursion Recursive functions Functions that call themselves
Command Line Arguments
Command line arguments
Understand argc and argv
More Examples of argc and argv
Command-line Arguments
Command Line Arguments
Yung-Hsiang Lu Purdue University
Sorting.
Procedure Activations
Strings and Pointer Arrays
프로그래밍2 및 실습 Sort Code 전명중.
Pointers and dynamic objects
Presentation transcript:

Pre/Post Condition Discussion 03/13/2013

Quicksort int main(int argc, char* argv[]) { int numbers[SIZE] = {2,5,3…} struct Sorter s; // initialize generic data producer setup_data(&s, (int*)numbers); int* (*qs)(int*, int, int) = &quicksort; s.sort_fn = qs; call_sort(&s); }

Quicksort int main(int argc, char* argv[]) { int numbers[SIZE] = {2,5,3…} struct Sorter s; // initialize generic data producer setup_data(&s, (int*)numbers); int* (*qs)(int*, int, int) = &quicksort; s.sort_fn = qs; call_sort(&s); } Functions we care about providing pre/postconditions for as they are a part of the composition strategy

Quicksort numbers[SIZE] = {2,3,5…} // Generic sorting class void setup_data(struct Sorter* sorter_, int* input) { int i; // store initial dataset internally for (i = 0; i < SIZE; ++i) sorter_->numbers_[i] = input[i]; }

Quicksort numbers[SIZE] = {2,3,5…} // Generic sorting class void setup_data(struct Sorter* sorter_, int* input) { int i; // store initial dataset internally for (i = 0; i < SIZE; ++i) sorter_->numbers_[i] = input[i]; } // precondition: sorter_ != NULL // precondition: input != NULL // postcondition: for all i, sorter_->numbers[i] == numbers[i]

Quicksort // quicksort recursive function int* quicksort(int* input, int p, int r) { int *ptr; ptr = input; if (p < r) { int j = partition(input, p, r); quicksort(input, p, j-1); quicksort(input, j+1, r); } return ptr; }

Quicksort // quicksort recursive function int* quicksort(int* input, int p, int r) { int *ptr; ptr = input; if (p < r) { int j = partition(input, p, r); quicksort(input, p, j-1); quicksort(input, j+1, r); } return ptr; } // precondition: input != NULL // precondition: p != r // postcondition: input[0] <=... <= input[SIZE-1]

Quicksort void call_sort(struct Sorter* sorter_) { if (sorter_->sort_fn != NULL) { sorter_->sort_fn(sorter_->numbers_, 0, SIZE-1); }

Quicksort void call_sort(struct Sorter* sorter_) { if (sorter_->sort_fn != NULL) { sorter_->sort_fn(sorter_->numbers_, 0, SIZE-1); } // precondition: sorter_ != NULL // precondition: sorter_->sort_fn != NULL // postcondition: sorter_->numbers_ numbers_[SIZE-1]

Mergesort (with strings) void mergesort_wrapper(char* input[]); int main(int argc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array }

Mergesort (with strings) void mergesort_wrapper(char* input[]); int main(int argc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array } // precondition-desc input must be converted to/from integer ??? // precondition-desc: input = array of strings // precondition: input != NULL // postcondition-desc: input = sorted array of strings // postcondition: input[0] <=... <= input[SIZE-1]

Mergesort (with strings) void mergesort_wrapper(char* input[]); int main(int argc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array } // precondition-desc input must be converted to/from integer ??? // precondition-desc: input = array of strings // precondition: input != NULL // postcondition-desc: input = sorted array of strings // postcondition: input[0] <=... <= input[SIZE-1] Necessary information for CFG?

Mergesort (with strings) void mergesort_wrapper(char* input[]); int main(int argc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array } How do we formally specify where to insert composed code? How do we formally specify necessary behavioral information? e.g. input must be converted to int array before being passed to merge sort Precondition? Method signature?