copyright©1998 angus wu programming methdolodgy and software engineering ee 31331 programming...

100
Copyright©1998 Angus Wu PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING

Post on 19-Dec-2015

225 views

Category:

Documents


6 download

TRANSCRIPT

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

INTRODUCTION

WHY PROGRAMMING METHODOLOGY?

WHAT IS THE PURPOSE OF STUDYING PROGRAMMING METHODOLOGY?

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

INTRODUCTION

PROGRAMMING IS AN ART? OR SOME SYSTEMATIC WAY FOR PROGRAMMING?

What is programming?

Programming is the translation process/ transformation of a concept/ algorithm/ conceptual operation/ human understandable idea into machine realization/operable code.

The objective is to have an effective, correct machine operable code to carry out the underlining process (the intended operations).

MAJOR CONCERN: EFFICIENCY AND CORRECTNESS

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

INTRODUCTION

Objective of the course is to develop a programming methodology that will enable us to write small and medium-sized programs that are computational effective and correctly implementing the target operations.

A methodology is a collection of methods which works together.

Here, in programming methodology, we try to teach a careful and systematic approach to programming.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PROGRAMMING APPROACH

There are two programming approaches. The length of the boxes is the passage of time.The first approach does not have the planning phase may lead to extensive debugging. Debugging- not only the syntax, the code, but the correctness of the system/operations being implemented.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PROGRAMMING APPROACH

Implementation of a FIR filter

i

n

i

ixasponse

1

Re

A general program for any order finite impulse response or fixed order….. Round off errors..

WHAT IT NEEDS - PLANNING and AN EFFECTIVEWAY OF MULTIPLICATION.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PHASE OF A SOFTWARE PROJECTThe basic software project development phases are:Specify, Design, Code, Test, Maintain

Specify - answer the question “what is the problem to be solved?” The specification must be precise and complete.

For example, the FIR problem, what is the allowable round off error (precision), integer or floating point multiplication, the range of input data and coefficients, How big is it allowed to be? Might it be negative? Is it more important to be fast or to be accurate? (depending on application)

Specify may be called ‘Analysis Phase’

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PHASE OF A SOFTWARE PROJECTDesign- answer the question “how should the problem be solved?”. Design includes finding the suitable abstractions, and decomposition of the problem into manageable components.

For FIR problem, how to implement the multiplication, fast, accurate? based on the specification from the first phase. How many modules? reading data, coefficient, multiplication, report data….. error handling, checking the validation of input data…..

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PHASE OF A SOFTWARE PROJECTCode - the transformation process of the underlying algorithm into machine readable code based on the specification. The basic problem of it is the coding style. It should be simple and easy to maintain.

example refer to sq0.c, sq2.c

Test - ensure that the program meets the specification. Not the syntax correctness but the correctness of the implementation of the underlying algorithm/operations. It should have a test plan for such task. Maintain - adapt the program to change of requirements or environment

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

MEANS OF DEVELOPMENTALGORITHM- It is the underlying process to be implemented as the machine operable instruction (codes).

In order to translate/transform the human understandable concepts, the targeted process/operation is represented as algorithm. The means to represent the algorithm should be a language. In fact, before we can develop program, we need a notation in which to express the development.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

ALGORITHM ALGORITHM - it is a finite set of instructions which, if followed, accomplish a particular task. It serves as a mean to express the underlying conceptual operations.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

ALGORITHM ALGORITHM must satisfy the criteria:•input - there are zero or more quantities which are externally supplied

•output - at least one quantity is produced.•definiteness - each step (instruction) must be clear and unambiguous

• finiteness - the algorithm will be terminated after a finite number of steps

• effectiveness - every step must be sufficiently basic that it can in principle be carried out by a person using only pencil and paper.

• proof of correctness - it must be able to provide a mean to validate the correctness of the implementing operations.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

MEANS OF DEVELOPMENTChoice: Natural language - too verbose and imprecise except at the analysis phase.

Target programming language - this would involve in too much detail and not flexible, without information hiding and abstraction.

Need an intermediate language for program development. PDL, program design language, or pseudocode

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

INTERMEDIATE LANGUAGEPSEUDOCODE It resembles the target language in that:• it is a sequence of operations (steps) • steps is precise and unambiguous•it has similar control structure of any high level structured programming languages such as C, Pascal, Perl

Pseudo Code C Codex = max of a, b, c x = a;

if (b > x) x = b;if (c > x) x = c;

Pseudocode has the concept of information hiding and abstraction that allows the programmer concentrating on the problem solving development instead of coding.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PSEUDOCODE

It is a collection of steps with the following operation rules:•A step is a single operation. •A sequence is a list of steps. •The steps in a sequence are performed one after the others.•A sequence is written as a vertical list of steps•Control structures determine the order of execution when this order is not sequential.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PSEUDOCODEConditional step has the general form

if P then A else B

The expression P must be evaluated to either true or false:P is the logical expression, a Boolean expression, or a predicate. The letters A and B denotes steps or sequences of steps.

P is evaluated; if the result is true, A is executed. Otherwise, B is executed. In all cases, exactly one of A or B is executed. The conditional step is a one-entry, one exit structure in order to maintain the structured programming concept.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PSEUDOCODELoop Step has the general form

while P do A

The layout is important that A has to be indented with respect to while. P is the predicate and A is a step or sequence of steps. The while loop is a one-entry, one-exit control structure. If the predicate P is false when the loop is entered the body, A, is not executed at all. The loop will be terminated only when the predicate P is false.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PSEUDOCODESequence of Steps has the general form

begin S1;S2;S3

endIt starts with a begin and terminated by end. There must be a semicolon, “;”, between each pair of steps and there may be a semicolon after the last step of the sequence.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

PROGRAM DESIGN LANGUAGE (PDL)

Program Design Language is a developed by Caine, Farber and Gordon in 1975 with the motivation to provide a form of language to implement software design in the same way programming languages implement an algorithm.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

PROGRAM DESIGN LANGUAGE

A program design language is a programming language that enforces procedural design with structured programming methodology. Characteristics of a procedural design language are:

Use a limited set of logical constructs:•Sequence•Conditional - if then else, select case•Loop - do while, repeat until

It leads to more readable, testable code, and supported by most modern programming languages.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

PROGRAM DESIGN LANGUAGE

Properties of a procedural design language are:

1. There is one input arc and one output arc.2. For each node, there is a path from the input arc to that node to

the output arc

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

PROGRAM DESIGN LANGUAGE

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

PROGRAM DESIGN LANGUAGE

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

PROGRAM DESIGN LANGUAGE

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

PDL FEATURES1. Structuring capability - Features for structuring a design should

correspond to the steps of the design technique chosen. (step-wise refinement possible)

2. Implementation hiding - It should be able to hide the details of lower design levels and their implementation. (level of abstraction) This reduces design complexity and promotes the understandability and modifiability of a design

3. Formality - The design language should be formal enough to expose ambiguities, inconsistencies, and other errors. It should be enable some degree of verification.

4. Ease of construction and modification - It must be able to accommodate changes easily.

5. Ease of understanding - It must promote better and more streamlined development.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

PDL STRUCTURES

PDL consists of two components: the outer syntax and inner syntaxThe outer syntax:

It states in formal statements, like any programming language but albeit in a much higher level It can be parsed, and it expresses the flow of control through the design.

The inner syntax:It is application dependent, informal and in plan English.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

OUTER SYNTAX

PDL enforces structure programming, it provides set of outer syntax statements:

• SequenceStatements are separated by semi-colons, written in the order of the desired order of execution.

Handling (command);Read_next_file;

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

OUTER SYNTAX• Selection Statements

IF Statement

IF <condition> THEN < statement1> ELSE <statement 2>;ENDIF;

CASE Statement

CASE <expression><value 1> : <statement 1>;.<value j> : <statement j>;OTHERWISE <statement j+1>;END CASE;

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

OUTER SYNTAX• Selection Statements

IF command valid THEN determine command_type;ELSE report error;ENDIF;

CASE command_type;AN : perform analysis;ST : provide statistics;RE : provide report;SA : save information;

OTHERWISE report system error;END CASE;

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

OUTER SYNTAX• Repetition

WHILE <condition> DO;<statement>

ENDDO;

DO<statement>

UNTIL <condition>

FOR < variable>=<expression 1> TO<expression 2> STEP < expression 3>;<statement>;

ENDFOR;

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

OUTER SYNTAX• Construct to Leave a LoopThere two ways to exit a loop: when the condition is satisfied or current status value reaches the limit. Two control statements are used: LEAVE and CYCLE

WHILE there are more record to process;process(record);IF process successful THEN compile update;ELSE CYCLE;ENDIF;IF no more record THEN LEAVE;

ENDWHILE;However, it can be done in:

WHILE not empty record ….…..ENDWHILE

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

OUTER SYNTAX• Assignment

<variable>:= <expression>;

first entry := name of project;next free element address:= relative address + base register;

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

OUTER SYNTAX• Procedures, Operations, and Functions

For procedures, we simply write their name, followed by actual parameters. Alternative way is to use CALL.

Functions are used within the expression.

Handle (command); - procedureCALL parser(line); - proceduretoken:= next_token(line); - functionport_connect(port2); - procedure

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

OUTER SYNTAX• Type, Variable, Constant DeclarationPDL uses type,variable, constant declaration statements to define data abstraction.

TYPE semaphore;special variable with two operations P,Vonly use for synchronization

END_TYPE;

VAR sem-driver, sem_quene: semaphore;CONS size_of_symbol_table;

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

EXAMPLE OF ALGORITHM

How to Divide - informal problem description is:given M and N, find the quotient and remainder when M is divided by N

A more formal precise specification for the above problem is:Specification : given integers M 0 and N > 0, find integers Q and R such that M = QN + R and 0 R < N. (Q is the quotient and R is the remainder).

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

DIVISION ALGORITHM1. R: = M;2. Q: = 0;3. if R < N the goto step 7;4. R: = R -N;5. Q: = Q +1;6. goto step 3;7. stop

R:= M; this step is not ‘R equal to M’ but as an assignment.“R is assigned M” or “R gets M”

In fact, it is not a good algorithm and not even structured programming as goto statement is used.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

REVISED DIVISION ALGORITHMQ: = 0;R: = M;while R N do

begin Q: = Q +1; R: = R - N;end

If the algorithm is correct, the final values of Q and R will satisfy the conditions

0 R < N (1) M = QN + R (2)

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

REVISED DIVISION ALGORITHMQ: = 0;R: = M;while R N do

begin Q: = Q +1; R: = R - N;end

If the algorithm is correct, the final values of Q and R will satisfy the conditions

0 R < N (1) M = QN + R (2)

Consider (1) first, 1. It is a property of the while loop that its termination condition is false when the loop terminates. When the program terminates, R N is false and so R < N must be true.

2. The first assignment sets R:=M and from the specification, M 0. Thus, R 0 initially.

3. The assignment R:= R -N is ‘guarded’ by the while condition R N. This assignment can’t make R negative.

Consequently, when the program terminates, 0 R < N .

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

REVISED DIVISION ALGORITHM

Q: = 0;R: = M;while R N do

begin Q: = Q +1; R: = R - N;end

If the algorithm is correct, the final values of Q and R will satisfy the conditions

0 R < N (1) M = QN + R (2)

Consider (2), After executing Q:=0;

R:=M;we have Q =0 and R=M and therefore M = 0*N + M = QN +RSince the loop condition (the predicate is true), we executeQ:= Q +1; and R:= R-N; we use R’ and Q’ be the value of R and Q after assignment, we have R’ = R -N and Q’ = Q +1 ( Q = Q’-1, R = R’+N )Before the execution of the loop M = QN + R After the execution of the loop M = (Q’-1)N + (R’ + N ) = Q’N + R’(2) is true before the execution of the assignment, and it still be true after the assignment with new Q and R, it will be true when the program terminates. We call (2) an invariant of the loop. Since both (1) and (2) are true, the program meets the specification.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

REVISED DIVISION ALGORITHMQ: = 0;R: = M;while R N do

begin Q: = Q +1; R: = R - N;end

If the algorithm is correct, the final values of Q and R will satisfy the conditions

0 R < N (1) M = QN + R (2)

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

REVISED DIVISION ALGORITHM

{M 0, N > 0} -- pre-conditionQ: = 0;R: = M;while R N do

begin Q: = Q +1; R: = R - N;end

{0 R < N, M = QN + R}-- post-condition

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

REVISED DIVISION ALGORITHM

{M 0, N > 0} -- pre-condition.{0 R < N, M = QN + R}-- post-condition

precondition: it states the condition under which the program will work correctly, or called the domain of the program

postcondition: it states relations that are true at the specified place in the computation.That is the end of the while loop.

Invariant: the condition that does not change within the loop

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

LOOP DESIGNX;while B /* I */

Y;X is the sequence of steps which initializes the loop.Y is the sequence of steps which constitute the body of the loop. !B is the termination condition and I is the invariant1. Decide what the loop is supposed to accomplish, i.e., the

postcondition. Express the postcondition in the form of I &&!B

2. Code X which establishes I3. Design code for Y which must maintain I and make progress

towards the goal, i.e. , to make B false.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

EXAMPLEQ: = 0;R: = M;while R N do

begin Q: = Q +1; R: = R - N;end

If the algorithm is correct, the final values of Q and R will satisfy the conditions

0 R < N (1) M = QN + R (2)

precondition: given M 0 and N > 0postcondition (1) and (2)

B is R N, !B is R < N, so the postcondition I && !B is R<N && M=QN+R, that is conditions (1) and (2)

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

EXAMPLEQ: = 0;R: = M;while R N do

begin Q: = Q +1; R: = R - N;end

If the algorithm is correct, the final values of Q and R will satisfy the conditions

0 R < N (1) M = QN + R (2)

The loop terminates when both (1) and (2) are true. Thus the program meets it specification.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

MORE ON ALGORITHM

Multiplication

Given two integers M 0 and N, find MN.More precisely, we want to establish the postcondition

p = MN (c)

We need to have an equation which (a). has more variables(b). can be established easily(c). simplifies to (1) with appropriate value of the

variables.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

MULTIPLICATION

Consider the equation

xN + p = MN (2)

For (a), it has more variables ( x and p ) When x is M, and p = 0, then (2) satisfies the condition (b).It satisfies (c) as it simplifies to (1) when x=0, and p = MN.

It is obvious that let x be M, and reducing x accordingly. Terminating when x =0.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

MULTIPLICATION ALGORITHM

x := M;p:= 0;and reducing x accordingly. Terminating when x =0. We must maintain the invariant xN + p = MN, throughout the process. The complete program : x:= M;

p:= 0;while x 0 do

beginx:= ..p:=…

end

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

MULTIPLICATION ALGORITHM

One way to do that is by reducing x by 1 at a time. let x’ and p’ be the new value of x and p inside the loop. We require:

xN + p = MN and x’N + p’ = MNLet x’= x-1, then

(x-1) N + p’ = MNsimplifies

xN + (p’-N) = MNThat is

p = p’ - NIt gives p’ = p +N

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

MULTIPLICATION ALGORITHM

The complete program : x:= M;

p:= 0;while x 0 do

beginx:= x - 1; p:= p + N;

endWhen this algorithm terminates, we have x =0 and xN + p = MNfrom which we can easily infer p = MN as required by (1) , the post condition.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

MULTIPLICATION ALGORITHM

Why the algorithm will terminate?1. initially x 0;2. x is reduced by 1 during every iteration; and3. the loop terminates when x =0.

SOUND OBVIOUS? The example demonstrates the precondition, invariant, and postcondition for a loop.

WHY BOTHER? Any high level language has division and multiplication instructions.

Can we speed up the process?

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

IMPROVED MULTIPLICATION ALGORITHM

We can improve the algorithm by decreasing x more rapidly.Using the fact that, if x is even, then

(x/2) (2y) = xy (1)By introducing another variable, y, to the invariant,

xy + p = MNThe precondition is x = M, y = N, and p =0, which can be easily established by the assignments:

x : = M;y : = N;p : =0;

What is the algorithm then?

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

IMPROVED MULTIPLICATION ALGORITHM

As before, we can reduce x by 1 if x is odd, then:x’ = x-1y’ = yp’ = p +y

If x is even then x’ = x/2y’ = 2yp’ = p

The complete algorithm is

x:= M;y:= N;p:= 0;while x 0 do

beginwhile even(x) do

begin

x:= x - 1; p:= p + N;end

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

x:= M;y:= N;p:= 0;while x 0 do

beginwhile even(x) do

begin x: = x div 2; y: = 2y;end;

x:= x - 1; p:= p + y;

end

The complete algorithm is

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

x:= M;y:= N;p:= 0;while x 0 do

beginwhile even(x) do

begin x: = x div 2; y: = 2y;end;

x:= x - 1; p:= p + y;

end

M 13N 17x 13 12 6 3 2 1 0y 17 34 68 136p 0 17 85 221

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

What are the precondition, invariant, and postcondition for the multiplication algorithms?

precondition -- M 0, and N >0

loop invariant -- for original algorithm xN + p = MN

for improved algorithmxy + p = MN

postcondition -- x = 0 && p = MN

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

WHAT HAS IT DEMONSTRATED?

We have two algorithms for multiplication. The time required to multiply M and N is proportional to M for the first algorithm, O(M), while the improved algorithm is proportional to log2 M, O(log M)

A considerable improvement is achieved in the improved algorithm.

It is one of the reason why we have to study programming methodology, computational efficiency.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

COMPLEXITY ANALYSIS

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

REASONS

The reasons for mathematical analysis of algorithms are:• To compare different algorithms for the same task• To predict performance in a new environment• To set values of algorithm parameters

In other words, we want to estimate how much time andmemory spacer are required to run the program in terms of the size of the problem instance.

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm Efficiency

Algorithmics, defined as the systematic study of the fundamental techniques used to design and analyze efficient algorithms.

General formula:

f(n) = efficiency

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm Efficiency

I := 1;

loop (I< 1000)

application code;

I := I + 1;

endloop;

I := 1;

loop (I< 1000)

application code;

I := I + 2;

endloop;

Loops for 999 Loops for 500

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm Efficiency

I := 1;

loop (I< 1000)

application code;

I := I + 1;

endloop;

I := 1;

loop (I< 1000)

application code;

I := I + 2;

endloop;

Linear loop, f(n) = n Efficiency is proportional to the number of iteration

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm EfficiencyI := 1;

loop (I< 1000)

application code;

I := I x 2 ;

endloop;

I := 1000;

loop (I 1)

application code;

I := I / 2;

endloop;

Multiply: 2 iteration < 1000Divide: 1000/2 iteration 1

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm EfficiencyI := 1;

loop (I< 1000)

application code;

I := I x 2 ;

endloop;

I := 1000;

loop (I 1)

application code;

I := I / 2;

endloop;

Logarithmic loopf(n) = log2n

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm EfficiencyI := 1;

loop (I < 10 )j :=1;

loop( j 10) {

application code;

j := j * 2;}

I:= I + 1;

endloop;

Nested loopIteration = outer loop iteration * inner loop iteration

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm EfficiencyI := 1;

loop (I < 10 )j :=1;

loop( j 10) {

application code;

j := j * 2;}

I:= I + 1;

endloop;

10 * log2 10

Linear Logarithmicf(n) = n log2 n

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm Efficiencyi := 1;

loop (i 10 )j :=1;

loop( j i) {application code;

j := j +1;}

i:= i + 1;

endloop;

Inner loop1+ 2 +3 …9 + 10 = 55Mathematically,

it is (n + 1)/2

Dependent Quadraticf(n) = n (n+1)/2as outer loop is n

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm Efficiencyi := 1;

loop (i 10 )j :=1;

loop( j 10) {

application code;

j := j +1;}

i:= i + 1;

endloop;

Inner loop it is nOuter loop

it is n

Quadraticf(n) = n*n = n2

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm EfficiencyNotation: f(n) is expressed in terms of the Big-O, as in On-The-Order-Of

f(n) = n = O(n)

f(n) = n(n+1)/2 = 1/2 * n2 + 1/2 * nO(f(n)) = O(1/2 * n2 + 1/2 * n)

= O(n2)Only count for the largest order

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm Efficiency

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm Efficiency

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Algorithm Efficiency

O(n2

)

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

EXAMPLE

# define N 5 typedef int matrix [N][N};1. void Add_Matrix(matrix A,matrix B, matrix C)2. { int i, j; 3. for ( i = 0; i < N; i++)4. for ( i = 0; i < N; i++)5. C[i][j]= A[i][j] + B[i][j];6. return;7. }

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

EXAMPLE

for (Start; Test; Action) statementfor (year = 1; year < PERIOD; year ++) ..

Upon execution of the for loop, it initializes the year to 1 then tests the testing condition, PERIOD and execute the statement it increases year and test the condition again the loop exists when year exceeding PERIOD

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

EXAMPLE # define N 5 typedef int matrix [N][N};1. void Add_Matrix(matrix A,matrix B, matrix C)2. { int i, j; 3. for ( i = 0; i < N; i++)4. for ( j = 0; j < N; j++)5. C[i][j]= A[i][j] + B[i][j];6. return;7. }

No. of times of each line of code executed when the program terminated.

line 1. 0 line 4. 30 2. 0 5. 25 3. 6 6. 0

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

EXAMPLE # define N 5 typedef int matrix [N][N};1. void Add_Matrix(matrix A,matrix B, matrix C)2. { int i, j; 3. for ( i = 0; i < N; i++)4. for ( j = 0; j < N; j++)5. C[i][j]= A[i][j] + B[i][j];6. return;7. }

In general line 1. 0 line 4. N(N+1) 2. 0 5. N2

3. N+1 6. 1

Total: 2N2 +2N+2 = O(N2)

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

EXAMPLE

1. void Mult_Matrix(matrix A,matrix B, matrix C)2. { int i, j, k; 3. for ( i = 1; i < N; i++)4. for ( j = 1; j < N; j++)5. { C[i][j] = 0;6. for( k = 0; k < N; k++)7. C[i][j]= A[i][k]*B[k][j];8. }9. return;10. }

Time complexity of matrix multiplication is O(N3)

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

SOFTWARE DEVELOPMENT

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Software Development Cycle

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

SOFTWARE DEVELOPMENT

• Analysis – (Planning, Definition, Establishing Requirement)

• Design – (High Level Abstraction)

• Coding/Implementation – (Various Tasks)

• Testing/Verification and Validation– (Examination of Goals and Objectives)

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Top Down Design

•Complex programming composes of more than a single program for complex algorithm

•Top down design approach decomposes the complex problem into sub-problems

•Functional decomposition

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Top Down Design

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Top Down DesignProblemDrawing simple diagrams (a house, a gal) on printer or screen

AnalysisDevelop basis components for the tasks

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Top Down DesignAnalysisBasic components:- a circle - a base line - parallel lines - intersecting lines

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Top Down DesignDesign (a gal) Initial Algorithm1. Draw a circle2. Draw a triangle3. Draw intersecting lines

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

Top Down DesignRefinementSince a triangle is not a basic component, it is refined into:1. Draw intersecting lines2. Draw a base

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

The Structure Chart For Top Down Design

Draw afigure

Draw abase

Drawinteresecting lines

Draw atriangle

Draw acircle

Drawinteresecting lines

Level 0

Level 2

Level 1

Original problem

Sub-problem

Detailed sub-problem

Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING

/*Draws a stick figure*/#include <stdio.h>void draw_circle(void); /* Draws a circle */void draw_intersect(void); /* Draws intersecting lines */void draw_base(void); /* Draws a base line */void draw_triangle(void); /* Draws a triangle */

int main(void){ /* Draw a circle. */ draw_circle();

/* Draw a triangle. */ draw_triangle();

/* Draw intersecting lines. */ draw_intersect(); return (0);}

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PROGRAM DEVELOPMENT

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PROGRAM DEVELOPMENTThe development sequence:

idea -> algorithm -> pseudo-code -> programSpecification: Input: real number a, b, c. Output : real values of x, if any, which satisfy the equation: ax2 + bx + c = 0

Simple formula x1, x2 = a

acbb

2

42

However, if a = 0, it does not work at all, and if b2 < 4ac,it yields complex number

Task: to carry out the above task as a program.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

ALGORITHM

a 0 a = 0 > 0 = 0 < 0 b 0 b = 0

2 roots 1 root no roots c 0 c = 0no roots roots

b2 - 4ac is the discriminant of the equation if there are two real roots, if there is one real roots,if there are no real roots

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PSEUDO-CODE

Development of the program is by refinement. The idea is to start with a version which is abstract but clearly correct,and then to proceed by refining parts of it. Pseudo-code

get value of a, b, and c;if a = 0

then considers cases else use formula

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

REFINEMENT

get value of a, b, and c;

Refine steps to get value of a, b, c in exactly the same way.scanf (“%lf”, &a);………… …&c);

scanf (“%lf %lf%lf”, &a, &b, &c);

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

REFINEMENT

if a = 0 then consider caseselse formula

The next step is to refine consider cases. Refining considercases give rise to two possibilities:if b = 0, then consider values of c; else the equation has only one root, x = -c/b.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

REFINEMENT

if a = 0 then consider caseselse formula

consider cases => /* refinement of consider cases */if b = 0 then consider value of c else write (‘the equation has only one root : -c/b)

consider values of c => /* refinement of consider if c =0 values of c*/ then write (‘The equation is satisfied by all value of x’) else write (‘ The equation has no solution’).

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

REFINEMENTif a = 0 then consider cases else formula

Refine the step use formula. There three cases to consider,depending on the value of b2 - 4ac .use formula => b2 - 4ac ; if then write (‘The equation has no real roots.’) else if 0 then write (‘ There is one root : - b/2a’)

elsebegin q := square_root(

write(‘ roots are: (-b+q)/2a and (-b-q)/2a’ )end

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

PROGRAMMING

get value of a, b,and c:if a = 0 then consider cases

else formula

Refinement: get value, consider cases, formula Program modules/functions

• get value• consider cases• formula

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

TOP DOWN DEVELOPMENT SUMMARY

Top down development with stepwise refinement:

• Start by looking the problem as a whole• Express the solution in pseudo-code at a high level abstraction• Progressively refine each pseudo-code statement until it can be directly translated into the target language.

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

FUNCTION AND REFINEMENT

Using function headings as refinement steps

• get_values• consider cases• formula

Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING

SUMMARY

• Stepwise refinement reduces the task of writing a complex program to a sequence of manageable stages

• At each stage, we have a program that is correct but contains more detail than the previous versions

• The refinement steps provide useful comments in the final program

• Top-down design places emphasis on the entire problem• Top-down design with stepwise refinement allow us to postpone decisions until we have enough information to make them correctly