+ All Categories
Home > Documents > Introduction

Introduction

Date post: 31-Dec-2015
Category:
Upload: columbia-adriel
View: 21 times
Download: 0 times
Share this document with a friend
Description:
Introduction. Why Data Structures? What AreData Structure? Phases of Software Development Precondition and Postcondition Examples. Why Data Structures?. Main function of computers is to perform calculations—fast, efficiently. Complex jobs require intensive calculations - PowerPoint PPT Presentation
25
1 Introduction 1. 1. Why Data Structures? Why Data Structures? 2. 2. What AreData Structure? What AreData Structure? 3. 3. Phases of Software Phases of Software Development Development 4. 4. Precondition and Precondition and Postcondition Postcondition 5. 5. Examples Examples
Transcript
Page 1: Introduction

1

Introduction

1.1. Why Data Structures?Why Data Structures?

2.2. What AreData Structure?What AreData Structure?

3.3. Phases of Software DevelopmentPhases of Software Development

4.4. Precondition and PostconditionPrecondition and Postcondition

5.5. ExamplesExamples

Page 2: Introduction

2

Why Data Structures?

Main function of computers is to perform Main function of computers is to perform calculations—fast, efficiently.calculations—fast, efficiently.

Complex jobs require intensive calculationsComplex jobs require intensive calculations How do you make computers work efficiently?How do you make computers work efficiently?

HardwareHardware SoftwareSoftware

Software solution (independent of hardware)Software solution (independent of hardware) AlgorithmsAlgorithms Algorithms depend on data structuresAlgorithms depend on data structures

Page 3: Introduction

3

What Is a Data Structure?

In a general sense, any data representation is a In a general sense, any data representation is a data structure. data structure. E.g., integer, stringE.g., integer, string

Specifically, Specifically, data structuredata structure is an is an organization organization for a collection of data itemsfor a collection of data items..

You choose appropriate data structure for You choose appropriate data structure for efficientefficient processing of data. processing of data.

Page 4: Introduction

Phases of Software Development

1.1. Problem AnalysisProblem Analysis

2.2. Design of SolutionDesign of Solution Decomposing the problemDecomposing the problem Using existing sourcesUsing existing sources

3.3. ImplementationImplementation

4.4. TestingTesting

Page 5: Introduction

An important topic:An important topic:preconditionspreconditions and andpostconditionspostconditions..

They are a method of specifying They are a method of specifying what a function accomplishes.what a function accomplishes.

Preconditions and Postconditions

Page 6: Introduction

Preconditions and Postconditions

Frequently, programmer must communicate Frequently, programmer must communicate precisely precisely whatwhat a function accomplishes, a function accomplishes, without any indication of without any indication of howhow the function the function does its work.does its work.

Can you think of a situationCan you think of a situationwhere this would occur ?where this would occur ?

Page 7: Introduction

Example

You are the head of a You are the head of a programming team programming team and you want one of and you want one of your programmers to your programmers to write a function for write a function for part of a project.part of a project.

HERE ARETHE REQUIREMENTS

FOR A FUNCTION THAT IWANT YOU TO

WRITE.

I DON'T CAREWHAT METHOD THE

FUNCTION USES,AS LONG AS THESE

REQUIREMENTSARE MET.

Page 8: Introduction

What are Preconditions and Postconditions?

One way to specify such requirements is One way to specify such requirements is with a pair of statements about the with a pair of statements about the function.function.

The The preconditionprecondition statement indicates what statement indicates what must be true before the function is called.must be true before the function is called.

The The postconditionpostcondition statement indicates statement indicates what will be true when the function what will be true when the function finishes its work.finishes its work.

Page 9: Introduction

Example

void write_sqrt(double x)

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.

...

Page 10: Introduction

Example

void write_sqrt( double x)

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.

...}

The precondition and The precondition and postcondition appear as postcondition appear as comments in your program.comments in your program.

Page 11: Introduction

Example

void write_sqrt( double x)

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.

...}

Page 12: Introduction

Example

write_sqrt( -10 );write_sqrt( 0 );write_sqrt( 5.6 );

Which of these function callsWhich of these function callsmeet the precondition ?meet the precondition ?

Page 13: Introduction

Example

void write_sqrt( double x)

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.

...}

The postcondition always indicates The postcondition always indicates what work the function has what work the function has accomplished. In this case, when accomplished. In this case, when the function returns the square root the function returns the square root of of xx has been written. has been written.

Page 14: Introduction

Another Example

bool is_vowel( char letter )// Precondition: letter is an uppercase or// lowercase letter (in the range 'A' ... 'Z' or 'a' ... 'z') .// Postcondition: The value returned by the// function is true if Letter is a vowel;// otherwise the value returned by the function is// false.

...

Page 15: Introduction

Another Example

is_vowel( 'A' );is_vowel(' Z' );is_vowel( '?' );

What values will be returnedWhat values will be returnedby these function calls ?by these function calls ?

Page 16: Introduction

Another Example

is_vowel( 'A' );is_vowel(' Z' );is_vowel( '?' );

What values will be returnedWhat values will be returnedby these function calls ?by these function calls ?

truetrue

falsefalse

Nobody knows, because theNobody knows, because theprecondition has been violated.precondition has been violated.

Page 17: Introduction

Always make sure the precondition is valid . . .

The programmer who The programmer who calls the function is calls the function is responsible for responsible for ensuring that the ensuring that the precondition is valid precondition is valid when the function is when the function is called.called.

Page 18: Introduction

. . . so the postcondition becomes true at the function’s end.

The programmer who The programmer who writes the function counts writes the function counts on the precondition being on the precondition being valid, and valid, and ensures that the ensures that the postcondition becomes postcondition becomes true true at the function’s end.at the function’s end.

THEN MY FUNCTIONWILL EXECUTE, AND WHEN

IT IS DONE, THEPOSTCONDITION WILL BE

TRUE.I GUARANTEE IT.

Page 19: Introduction

A Quiz

Suppose that you call Suppose that you call a function, and you a function, and you neglect to make sure neglect to make sure that the precondition that the precondition is valid. is valid. Who is responsible if Who is responsible if this inadvertently this inadvertently causes a 40-day flood causes a 40-day flood or other disaster?or other disaster?

YouYou The programmer who The programmer who

wrote that torrential wrote that torrential functionfunction

NoahNoah

Page 20: Introduction

Your Turn

1.1. Suppose that you are to write a boolean Suppose that you are to write a boolean function which checks if an integer is even or function which checks if an integer is even or not. What are the preconditions and not. What are the preconditions and postconditions?postconditions?

2.2. Suppose that you are to write a function which Suppose that you are to write a function which imports the balance and interest rate for a imports the balance and interest rate for a savings account and returns the amount of savings account and returns the amount of interest. What are the preconditions and interest. What are the preconditions and postconditions?postconditions?

Page 21: Introduction

Your Turn

1.1. Suppose you are to write a function which Suppose you are to write a function which converts a Frerenheit temperature reading converts a Frerenheit temperature reading into Celsius. What are the preconditions into Celsius. What are the preconditions and postconditions?and postconditions?

2.2. Suppose that you are to write a function Suppose that you are to write a function which imports an int array and the number which imports an int array and the number of array elements and prints the array of array elements and prints the array contents. What are the preconditions and contents. What are the preconditions and postconditions?postconditions?

Page 22: Introduction

On the other hand, careful programmers also follow these rules:

When you write a function, you should When you write a function, you should make every effort to detect when a make every effort to detect when a precondition has been violated.precondition has been violated.

If you detect that a precondition has been If you detect that a precondition has been violated, then print an error message and violated, then print an error message and halt the program.halt the program.

Page 23: Introduction

On the other hand, careful programmers also follow these rules:

When you write a function, you should When you write a function, you should make every effort to detect when a make every effort to detect when a precondition has been violated.precondition has been violated.

If you detect that a precondition has been If you detect that a precondition has been violated, then print an error message and violated, then print an error message and halt the program...halt the program...

...rather than causing...rather than causing

a disaster.a disaster.

Page 24: Introduction

Advantages of Using Preconditions and Postconditions

Succinctly describes the behavior of a Succinctly describes the behavior of a function...function...

... without cluttering up your thinking with ... without cluttering up your thinking with details of how the function works.details of how the function works.

At a later point, you may reimplement the At a later point, you may reimplement the function in a new way ...function in a new way ...

... but programs (which only depend on the ... but programs (which only depend on the precondition/postcondition) will still work precondition/postcondition) will still work with no changes.with no changes.

Page 25: Introduction

PreconditionPrecondition The programmer who calls The programmer who calls

a function ensures that the a function ensures that the precondition is valid.precondition is valid.

The programmer who The programmer who writes a function can bank writes a function can bank on the precondition being on the precondition being true when the function true when the function begins execution.begins execution.

PostconditionPostcondition The programmer The programmer

who writes a who writes a function ensures function ensures that the that the postcondition is postcondition is true when the true when the function finishes function finishes executing.executing.

Summary


Recommended