1 program planning and design important stages before actual program is written

18
1 Program Planning and Program Planning and Design Design Important stages before Important stages before actual program is written actual program is written

Upload: blaze-walker

Post on 13-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Program Planning and Design Important stages before actual program is written

11

Program Planning and Program Planning and DesignDesign

Important stages before Important stages before actual program is written actual program is written

Page 2: 1 Program Planning and Design Important stages before actual program is written

22

Steps to Problem-solvingSteps to Problem-solving

Analyze the problem and develop Analyze the problem and develop specificationsspecifications

Design solutionsDesign solutionsCode the program with Code the program with

documentationdocumentationTest the programTest the programValidate the program Validate the program

Page 3: 1 Program Planning and Design Important stages before actual program is written

33

Algorithm Algorithm

Definition 1: The sequence of steps Definition 1: The sequence of steps required to solve a problemrequired to solve a problem

Definition 2: A definite list of well-Definition 2: A definite list of well-defined instructions for completing a defined instructions for completing a task; that given an initial state, will task; that given an initial state, will proceed through a well-defined proceed through a well-defined series of successive states, series of successive states, eventually terminating in an end-eventually terminating in an end-state. state.

Page 4: 1 Program Planning and Design Important stages before actual program is written

44

Presentation of Algorithms Presentation of Algorithms

Structure Chart:Structure Chart: Structure of modules needed to solve Structure of modules needed to solve

a problem.a problem.Pseudo-codes:Pseudo-codes:

Semiformal, English-like list of steps Semiformal, English-like list of steps of the algorithm. of the algorithm.

Flow-charts:Flow-charts: Graphical/schematic representation of Graphical/schematic representation of

an algorithm.an algorithm.

Page 5: 1 Program Planning and Design Important stages before actual program is written

55

Structure ChartStructure Chart

To chart out the modules and To chart out the modules and statements to break down a statements to break down a programming problemprogramming problem

Multiply two integers

Get two integers Formula Print result

no1 no2Multiply= no1 * no2

Multiply

Page 6: 1 Program Planning and Design Important stages before actual program is written

66

Pseudo-codePseudo-code

Semi-formal, English-like description Semi-formal, English-like description of logic of the algorithmof logic of the algorithm

Helps programmer to “think out” the Helps programmer to “think out” the programprogram

Advantage:Advantage:Simple Simple Easy to understandEasy to understand

Page 7: 1 Program Planning and Design Important stages before actual program is written

77

Example of Pseudo-code: Example of Pseudo-code:

To calculate Sales Amount:To calculate Sales Amount:

1. First get user to enter quantity1. First get user to enter quantity

2. Get the price of the product2. Get the price of the product

3. Calculate amount = price * 3. Calculate amount = price * quantityquantity

4. Display the amount4. Display the amount

Page 8: 1 Program Planning and Design Important stages before actual program is written

88

Example of Pseudo-code: Example of Pseudo-code:

Write an algorithm of a program that will Write an algorithm of a program that will accept four marks and determine a student’s accept four marks and determine a student’s final grade and indicate whether it is passing final grade and indicate whether it is passing or failing. The passing mark is 50. The final or failing. The passing mark is 50. The final grade is calculated as the average of four grade is calculated as the average of four marks.marks.Pseudo-codePseudo-code:: Input a set of 4 marksInput a set of 4 marks Calculate their average by summing and dividing by 4Calculate their average by summing and dividing by 4 If average is below 50If average is below 50

Print “FAIL”Print “FAIL”elseelse

Print “PASS”Print “PASS”

Page 9: 1 Program Planning and Design Important stages before actual program is written

99

Flow-ChartsFlow-Charts Graphical Graphical

representation representation of an algorithmof an algorithm

Uses Uses symbolssymbols and and shapesshapes to to describe flow or describe flow or process of the process of the algorithm to algorithm to solve a problem. solve a problem.

Example:Example:

Page 10: 1 Program Planning and Design Important stages before actual program is written

1010

Flow-charting SymbolsFlow-charting Symbols

Selection/ Decisions

Process Connectors

Input/Output Terminators

Direction of Flow

Loop

Page 11: 1 Program Planning and Design Important stages before actual program is written

1111

Flow-charting Symbols Flow-charting Symbols (continued)(continued)

Page 12: 1 Program Planning and Design Important stages before actual program is written

1212

Example of Flowchart: Example of Flowchart:

START

InputM1, M2, M3,

M4

GRADE(M1+M2+M3+M4)/4

ISGRADE<5

0

STOP

YESNO

Print PASS Print FAIL

Pseudo-codePseudo-code:: Input a set of 4 marksInput a set of 4 marks Calculate their Calculate their

average by summing average by summing and dividing by 4and dividing by 4

If average is below 50If average is below 50Print “FAIL”Print “FAIL”

elseelsePrint “PASS”Print “PASS”

Page 13: 1 Program Planning and Design Important stages before actual program is written

1313

Example of Flowchart Example of Flowchart (continued) :(continued) :

Page 14: 1 Program Planning and Design Important stages before actual program is written

1414

DebuggingDebugging

A process to remove error A process to remove error Error = bug, the first computer Error = bug, the first computer

“bug” is a bug found on a printer“bug” is a bug found on a printer4 kinds of errors : 4 kinds of errors :

Syntax errorSyntax errorRun-time errorRun-time errorUndetected errorUndetected errorLogic/semantic errorLogic/semantic error

Page 15: 1 Program Planning and Design Important stages before actual program is written

1515

1. 1. Syntax errorSyntax error

Violation of the C grammar rules, detected Violation of the C grammar rules, detected during program translation (compilation)during program translation (compilation)

Example:Example:Missing semicolon Missing semicolon Undeclared variableUndeclared variable

Page 16: 1 Program Planning and Design Important stages before actual program is written

1616

2. 2. Run-Time ErrorsRun-Time Errors An attempt to perform An attempt to perform

an invalid operation an invalid operation detected during program detected during program executionexecution

Example:Example: Dividing a number by Dividing a number by

zerozero When a run time occurs, When a run time occurs,

the computer will stop the computer will stop executing your program executing your program and will display a and will display a diagnostic message that diagnostic message that indicates the line where indicates the line where an error was detected an error was detected

Page 17: 1 Program Planning and Design Important stages before actual program is written

1717

3. 3. Undetected errorsUndetected errors

Many execution error may Many execution error may not prevent a C not prevent a C programming from running programming from running to completion but they may to completion but they may simply lead to incorrect simply lead to incorrect resultresult

Example:Example: When programmer When programmer

forgot to write ‘&’ in forgot to write ‘&’ in scanfscanf

The program runs to The program runs to completion using whatever completion using whatever “garbage” value originally “garbage” value originally in the memory locations in the memory locations named to variable in scanfnamed to variable in scanf

Page 18: 1 Program Planning and Design Important stages before actual program is written

1818

4. 4. Logic ErrorsLogic Errors

An error caused by following an An error caused by following an incorrect algorithmincorrect algorithm

Usually do not cause run-time errors Usually do not cause run-time errors and do not display error messages, and do not display error messages, they are difficult to detectthey are difficult to detect