Top Down Design. 2. Design Methodology

Similar documents
III. Check if the divisors add up to the number. Now we may consider each of these tasks separately, assuming the others will be taken care of

INTRODUCTION TO ALGORITHMS

CS 199 Computer Programming. Spring 2018 Lecture 2 Problem Solving

MANUAL ACCORDION TOOL

Chapter 1 - An Introduction to Computers and Problem Solving

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

Checking Program Properties with ESC/Java

Formal Methods. CITS5501 Software Testing and Quality Assurance

Assertions & Verification & Example Loop Invariants Example Exam Questions

Outline. Introduction to Programming (in C++) Introduction. First program in C++ Programming examples

Assertions & Verification Example Exam Questions

Programming II (CS300)

Outline. Program development cycle. Algorithms development and representation. Examples.

6. C++ Subprograms David Keil CS I slides 7/03 1

Method & Tools for Program Analysis & Design

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

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

ENERGY 211 / CME 211. Evolution

Steps to program development

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

Lecture Notes on Contracts

1. Introduction to Programming

SNS COLLEGE OF ENGINEERING,

Introduction. SSH Secure Shell Client 1

Symbolic Execution and Proof of Properties

IT 374 C# and Applications/ IT695 C# Data Structures

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Lecture Notes on Linear Search

COP 1220 Introduction to Programming in C++ Course Justification

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

TABLE OF CONTENTS 2 CHAPTER 1 3 CHAPTER 2 4 CHAPTER 3 5 CHAPTER 4. Algorithm Design & Problem Solving. Data Representation.

Repetition Through Recursion

Computer System and programming in C

CHAPTER 2 PROBLEM SOLVING TECHNIQUES. Mr Mohd Hatta Bin Hj Mohamed Ali Computer Programming BFC2042

We first learn one useful option of gcc. Copy the following C source file to your

Chapter 3. Describing Syntax and Semantics

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Spark verification features

Programming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs

Chapter 4 Introduction to Control Statements

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

Lecture 10 Design by Contract

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Lecture 3. More About C

Chapter 4 Defining Classes I

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Business Insight Authoring

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

The divide and conquer strategy has three basic parts. For a given problem of size n,

CS Fundamentals of Programming II Fall Very Basic UNIX

Unit-3 Software Design (Lecture Notes)

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Algorithms 1 / 24. Algorithms Sequential Search

Word 2016 Advanced. North American Edition SAMPLE

Cambridge Assessment International Education Cambridge International Advanced Subsidiary and Advanced Level. Published

COP 1170 Introduction to Computer Programming using Visual Basic

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Program Verification (Rosen, Sections 5.5)

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

Lab 1: Getting familiar with LabVIEW: Part I

Chapter 8 Algorithms 1

Chapter 4. Computation. Bjarne Stroustrup.

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

SOLUTIONS TO EXERCISES PART ONE: Problem-Solving Techniques

Lecture Notes for Chapter 2: Getting Started

CSE 142 Wi03 Midterm 2 Sample Solution All Versions Page 1 of 6

AEMLog Users Guide. Version 1.01

Computer Science & Engineering 150A Problem Solving Using Computers

LECTURE 16. Functional Programming

Intermediate Programming, Spring Misha Kazhdan

1. The Mac Environment in SIE 1222

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design.

Chapter 4. Procedural Abstraction and Functions That Return a Value

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Lecture 4 Searching Arrays

Hardware describing languages, high level tools and Synthesis

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Basic Verification Strategy

Introduction to Computer Science and Business

CST112--Functions Page 1

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

Creating a new CDC policy using the Database Administration Console

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Introduction to Computer Science Unit 2. Notes

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

AEMLog users guide V User Guide - Advanced Engine Management 2205 West 126 th st Hawthorne CA,

Object-Oriented and Classical Software Engineering THE TOOLS OF THE TRADE 9/22/2017. CHAPTER 5 Slide 5.2. Stephen R. Schach. Overview Slide 5.

Programming Logic and Design Ninth Edition

Programming Logic and Design Seventh Edition

Chapter 3 (part 3) Describing Syntax and Semantics

Propositional Calculus. Math Foundations of Computer Science

4 VISUAL FOXPRO - AN INTRODUCTION

DDDA User Guide Version 1.5

Transcription:

Top Down Design 1 A solution method where the problem is broken down into smaller subproblems, which in turn are broken down into smaller problems until each subproblem can be solved in a few steps. (Also called: Divide & Conquer strategy) Top Down Design <==> Step-Wise Refinement <==> Modular Programming Problem: An iterative numerical computation BEGIN Numerical problem INPUT COMPUTATION OUTPUT END Numerical problem BEGIN Computation INITIALIZATION VERIFICATION FINAL COMPUTATIONS END Computation

Design Specification 2 Techniques: 1. Module Structure Chart ( Hierarchical Tree ) 2. Outlining ( writing decomposition) 3. Pseudocode (mixture of English & C++) Module Chart Format: STEP 1 STEP 2 STEP 3 STEP 1 STEP 2 STEP 3 step a STEP 2.1 STEP 2.2 STEP 3.1 STEP 3.2 STEP 2.1 STEP 2.2 STEP 3.1 STEP 3.2 step b STEP 2.2.1 step g step h step c STEP 2.2.2 step i STEP 2.2.1 STEP 2.2.2 step d step e step f

Outline Design Example 3 Problem: Determine if a positive integer is a perfect number. Defn: a perfect number is a positive integer that is equal to the sum of its divisors, excluding the number itself; e.g. 6, 28, 496,... 1. Get the number to be tested 2. Determine the divisors of the number 3. Check if the divisors add up to the number. 1.1 Prompt the user for the number (p) 1.2 Read the number (check if number is positive int) 2.1 Set the divisor sum = 1 2.2 check each integer, x, from 2... p/2 to see if x is a divisor of p 2.2.1 Find the remainder of p/x 2.2.1.1 remainder = p - truncate(p/x) * x 2.2.2 If the remainder of p/x is 0 then then add x to the divisor sum 3.1 If divisor sum = p then output p is perfect Else output p is NOT perfect

Pseudocode Design Example 4 Print Enter the possible perfect number Read the number into: p Comment initialize sum of the divisors & divisor SumOfDivisors <-- 1 Divisor <-- 2 Comment find all possible divisors While Divisor <= p / 2 Do R <-- p - (Truncate ( p/divisor ) )* Divisor If R = 0 Then SumOfDivisors <-- SumOfDivisors + Divisor Endif Divisor <-- Divisor + 1 Endwhile If SumOfDivisors = p Then Print p is a perfect number Else Print p is NOT a perfect number Endif

Top-Down Design Example 5 Problem: How to enter & run a C++ program on a PC. ( Sys: MS Visual C++ on a hard disk.) Main Module. Turn the system on. Start the MS Visual C system. Enter program. Run program Start the MS Visual C system. (Start Menu, Programs, MS Visual C++, MS Developer Studio.) (or move cursor to MS C++ icon (Msdev) and double-click left mouse button.) Enter program. Choose File menu, New, Text File. Type the program in using the editor. Use the cursor & the backspace keys to make corrections. Under the File pull down menu, select Save As; enter the name of your program in the dialog box & save. Run Program. Build (compile) the executable (F7), create a default workspace (Correct any errors). If an error message appears, note what it says & then press any key. Otherwise enter data as needed and observe any output, until the program stops running. Under the File pull down menu, select Exit to quit the MS Visual C system.

Top-Down Design Example 6 Problem: How to enter & run a C++ program on a Unix workstation. ( Sys: FreeBSD on a IntelPC) Main Module. Turn the system on & Login. Enter program. Run program Login. Type "guest" after the login: prompt & Type "visitor" after the password: prompt. Enter program. Type emacs filename.cc to start the editor. Type the program into the editor. Use ctrl-f, ctrl-b, ctrl-n, ctrl-p & the backspace key to make corrections. Press ctrl-x ctrl-s to save the file. Run Program. Type "gcc -o filename filename.cc" to compile the program. If an error message appears below, locate & correct the error & recompile. Otherwise type "filename", enter data as needed & observe any output, until the program stops running.

Program Proofs Program Proof: Assertion: code analysis to achieve a formal verification of the correctness of a program s output. statement of relationship between a program s variables and data. 7 Every program statement can be preceded & succeeded by an assertion, (termed the precondition & postcondition respectively). Program proofs involves applying deductive logic to show that the precondition & postcondition assertions are true for all program statements Due to practical time considerations assertions are made & proven only for sequences of statements. Example: // root > 0 root2 = sqrt( root ) ; // root = root2 * root2 precondition postcondition Better postcondition: // root <= (root2 * root2) + epsilon

Program Proofs (cont) 8 Loop Invariant: assertions that are true prior to loop execution & following each loop iteration. Commonly composed of the boolean expression controlling the loop, its negation, ranges of loop variables, data structure states & file status. Derived by analyzing the setup & modifications of the loop variables. Example: // integer multiplication by repeated addition product = 0 ; factor = multiplier ; while (factor > 0) { product = product + multiplicand ; factor = factor - 1 ; } Loop Invariant: product = multiplicand * ( multiplier - factor ) ; factor is in the range 0... multiplier

Program Documentation 9 A natural outgrowth of the software development method is the production of external program documentation prior to program implementation. The documentation describes the purpose of a program and its data. Internal documentation, in the form of in-line comments, as well as module documentation, is produced in conjunction with code development. See the Elements of Programming Style in the appendices.

Modular Decomposition 10 One of the most fundamental ideas in problem solving Divide and Conquer involves subdividing a problem into subproblems and solving each subproblem individually. Quite often, the attempt to solve a subproblem introduces new subproblems at a lower level. The subdivision of subproblems should continue until a subproblem can easily be solved. This is similar to the refining of an algorithm. STEP 1 STEP 2 STEP 3 STEP 1 STEP 2 STEP 3 step a STEP 2.1 STEP 2.2 STEP 3.1 STEP 3.2 STEP 2.1 STEP 2.2 STEP 3.1 STEP 3.2 step b STEP 2.2.1 step g step h step c STEP 2.2.2 step i STEP 2.2.1 STEP 2.2.2 step d step e step f

Structure Chart 11 A structure chart is a documentation tool that enables the designer to keep track of the relationships among subproblems throughout this refinement process. The structure chart gives a detailed description of the original problem in terms of subproblems. This top-down description will make it easier to write a program to solve the original problem. We can solve each box in the structure chart by using a C++ function. A function is a grouping of statements into a single unit. This single unit can be invoked to solve its subproblem simply by activating it by means of a function call. Since the structure chart gives a pictorial mechanism for visualizing the solution to a problem and each of the pieces of the structure chart corresponds to a function, the solution to a problem is obtained by correctly invoking a series of functions. In order to solve problems, we need only solve all the subproblems by means of functions. Thus a C++ program is a collection of functions that solve a series of subproblems. This is the essence of top-down design.

Procedural Abstraction 12 The top-down approach to program design involves procedural abstraction. This enables us to associate meaningful names with more complicated algorithm steps, reference these steps by name, and defer their implementation details until later. Our programs are written using logically independent sections, similar to the manner in which we developed the solution algorithm (as logically independent steps). Using logically independent program sections enables the programmer to hide the details of subproblem implementations from one another. This concept, information hiding, is critical in all stages of the software development process.