Software Development. Modular Design and Algorithm Analysis

Similar documents
CSC Advanced Object Oriented Programming, Spring Specification

Assertions. Assertions - Example

a correct statement? You need to know what the statement is supposed to do.

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Design by Contract in Eiffel

An Annotated Language

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

Violations of the contract are exceptions, and are usually handled by special language constructs. Design by contract

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Procedures in Visual Basic

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

CSE 230 Intermediate Programming in C and C++ Recursion

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

6.001 Notes: Section 4.1

Problem Solving with C++

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

Absolute C++ Walter Savitch

isinstance and While Loops

Hoare Logic: Proving Programs Correct

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Programming II (CS300)

University of Massachusetts Lowell

Assertions, pre/postconditions

Symbolic Execution and Proof of Properties

Lecture Notes on Contracts

Spark verification features

Programming II (CS300)

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Repetition Through Recursion

Programming Languages Third Edition

Combining Static and Dynamic Contract Checking for Curry

Programming II (CS300)

15-122: Principles of Imperative Computation (Section G)

17. Assertions. Outline. Built-in tests. Built-in tests 3/29/11. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

More About Recursive Data Types

17. Assertions. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen


PROCESS DEVELOPMENT METHODOLOGY The development process of an API fits the most fundamental iterative code development

Lecture 5 - Axiomatic semantics

6.001 Notes: Section 6.1

This session. Recursion. Planning the development. Software development. COM1022 Functional Programming and Reasoning

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Constants, once routines, and helper functions

Adding Contracts to C#

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

Part I Basic Concepts 1

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Fundamentals of Programming Session 13

Intro to: Design Principles

FreePascal changes: user documentation

Reasoning about programs

Last time. Reasoning about programs. Coming up. Project Final Presentations. This Thursday, Nov 30: 4 th in-class exercise

Recursive Definitions

State-Based Testing Part B Error Identification. Generating test cases for complex behaviour

Verification and Validation. Verification and validation

Inference rule for Induction

Chapter 7. Iteration. 7.1 Multiple assignment

DM502 Programming A. Peter Schneider-Kamp.

Functional Programming

STUDENT LESSON A9 Recursion

CS Lecture 19: Loop invariants

Lecture Notes on Linear Search

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

142

CSE 230 Computer Science II (Data Structure) Introduction

Lecture 10 Parsing 10.1

Chapter 4 Defining Classes I

Chapter 7 Functions. Now consider a more advanced example:

Principles of Programming Languages

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Testing Object-Oriented Software. 22 November 2017

Warm-Up Problem. Let be a set of well-formed Predicate logic formulas. Let be well-formed Predicate logic formulas. Prove or disprove the following.

Inheritance (Chapter 7)

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

RECURSIVE FUNCTIONS ON STACK

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Compositional Cutpoint Verification

CS 161 Computer Security

ECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Ambientes de Desenvolvimento Avançados

An introduction to Scheme

Object Oriented Programming

Runtime Checking and Test Case Generation for Python

Contract-based Programming in Ada 2012 A Tutorial

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop

Chapter 6 Sub Procedures

C Functions. Object created and destroyed within its block auto: default for local variables

Transcription:

Software Development Modular Design and Algorithm Analysis

Precondition and Postcondition To create a good algorithm, a programmer must be able to analyse a precondition (starting state) and a postcondition (ending state). Once a programmer gets more experienced, they can add preconditions and postconditions to their algorithms. However, doing so is not always a good practice because algorithms should only take care of one piece of functionality.

Defining a Precondition A precondition is a condition or predicate that must always be true just prior to the execution of some section of code or before an operation in a formal specification. If a precondition is violated, the effect of the section of code becomes undefined and thus may or may not carry out its intended work. Security problems can arise due to incorrect preconditions.

Often, preconditions are simply included in the documentation of the affected section of code. Preconditions are sometimes tested using assertions within the code itself, and some languages have specific syntactic constructions for doing so. For example: the factorial is only defined for integers greater than or equal to zero. So a program that calculates the factorial of an input number would have preconditions that the number be an integer and that it be greater than or equal to zero.

Preconditions and inheritance In the presence of inheritance, the routines inherited by descendant classes (subclasses) do so with their preconditions in force. This means that any implementations or redefinitions of inherited routines also have to be written to comply with their inherited contract. Preconditions can be modified in redefined routines, but they may only be weakened. That is, the redefined routine may lessen the obligation of the client, but not increase it.

Postcondition A postcondition is a condition or predicate that must always be true just after the execution of some section of code or after an operation in a formal specification. Postconditions are sometimes tested using assertions within the code itself. Often, postconditions are simply included in the documentation of the affected section of code.

For example: The result of a factorial is always an integer and greater than or equal to 1. So a program that calculates the factorial of an input number would have postconditions that the result after the calculation be an integer and that it be greater than or equal to 1. Another example: a program that calculates the square root of an input number might have the postconditions that the result be a number and that its square be equal to the input.

Postconditions and inheritance In the presence of inheritance, the routines inherited by descendant classes (subclasses) do so with their contracts, that is their preconditions and postconditions, in force. This means that any implementations or redefinitions of inherited routines also have to be written to comply with their inherited contracts. Postconditions can be modified in redefined routines, but they may only be strengthened. That is, the redefined routine may increase the benefits it provides to the client, but may not decrease those benefits.

Recursion Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration). The approach can be applied to many types of problems, and recursion is one of the central ideas of computer science. Recursive functions need special stop conditions. Otherwise they will infinitely continue calling themselves. With the ByRef keyword, you can provide a way to stop a recursive function.

Example The Recursive() Function receives two arguments. The first argument is a value that is doubled on each call. The second argument is received ByRef it exists in one memory place and it is incremented on every function invocation.

Output

Pitfalls in recursive functions Infinite recursion Infinite recursion is a special case of an infinite loop that is caused by recursion. Limiting Conditions You must design a recursive procedure to test for at least one condition that can terminate the recursion, and you must also handle the case where no such condition is satisfied within a reasonable number of recursive calls. Without at least one condition that can be met without fail, your procedure runs a high risk of executing in an infinite loop.

Memory Usage Your application has a limited amount of space for local variables. Each time a procedure calls itself, it uses more of that space for additional copies of its local variables. If this process continues indefinitely, it eventually causes a StackOverflowException error. Efficiency You can almost always substitute a loop for recursion. A loop does not have the overhead of passing arguments, initializing additional storage, and returning values. Your performance can be much better without recursive calls.

Mutual Recursion You might observe very poor performance, or even an infinite loop, if two procedures call each other. Such a design presents the same problems as a single recursive procedure, but can be harder to detect and debug. Calling with Parentheses When a Function procedure calls itself recursively, you must follow the procedure name with parentheses, even if there is no argument list. Otherwise, the function name is taken as representing the return value of the function.

Testing If you write a recursive procedure, you should test it very carefully to make sure it always meets some limiting condition. You should also ensure that you cannot run out of memory due to having too many recursive calls.

Fibonacci number In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence: By definition, the first two numbers in the Fibonacci sequence are 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

Activity 3.6 Write and algorithm that calculates part of a Fibonacci series. The user must input the starting number and how many numbers they want to display. As an extra bonus, code your function in VB. Save your work as john_s_3_6_algofib under the appropriate folder for your class in the DropOff folder on the X: drive