Скачать презентацию CHAPTER 1 Introduction to Structured Programming 1 Скачать презентацию CHAPTER 1 Introduction to Structured Programming 1

b862a4b97954b99a5404d0b5add605f4.ppt

  • Количество слайдов: 63

CHAPTER 1 Introduction to Structured Programming 1 CHAPTER 1 Introduction to Structured Programming 1

PROGRAMMING TERMINOLOGY An activity consists of writing a program that will process the input PROGRAMMING TERMINOLOGY An activity consists of writing a program that will process the input data to produce the required output/document. INPUT DATA Input (Set of Instructions) Processing Output (Report) 2

PROGRAM written using a programming language, eg. C, Java, C++, BASIC etc. Before writing PROGRAM written using a programming language, eg. C, Java, C++, BASIC etc. Before writing code, it is very important to plan the solution to the problem – solution algorithm 3

SOLUTION ALGORITHM A sequence of logic steps/instructions to the computer/people to perform a specific SOLUTION ALGORITHM A sequence of logic steps/instructions to the computer/people to perform a specific task It is like a recipe : Step by step guide, that need to be followed in the right sequence Properties: Steps must be defined precisely (in detail) and clearly Process must have an end – finite steps The correct solution to an identified problem 4

ALGORITHM Example : At a Public Phone ¢ MAKE A CALL 5 ALGORITHM Example : At a Public Phone ¢ MAKE A CALL 5

ALGORITHM Steps are in correct order, performed from top to bottom. Steps can be ALGORITHM Steps are in correct order, performed from top to bottom. Steps can be made up of combination of all control structures (sequence, selection, iteration) Indentation is very important to show different control structures 6

ALGORITHM 1. Give an example of an algorithm of a daily activity e. g. ALGORITHM 1. Give an example of an algorithm of a daily activity e. g. watching a TV programme. 2. TASK : Write a solution algorithm to add 2 numbers and show the result. 1. Get first number 2. Get second number 3. Add first number and second number, put the answer in sum 4. Display sum 7

ALGORITHM TASK : Write a solution algorithm to find and display the average temperature. ALGORITHM TASK : Write a solution algorithm to find and display the average temperature. The maximum and minimum temperature readings are accepted as integers. 1. Get min_temp 2. Get max_temp 3. Calculate average avg_temp = (min_temp + max_temp) / 2 4. Display avg_temp 8

GENERAL STEPS IN PROGRAM DEVELOPMENT Define problem Understand requirements Determine input, output and processes GENERAL STEPS IN PROGRAM DEVELOPMENT Define problem Understand requirements Determine input, output and processes Outline solution Break problem into smaller tasks or steps, draft solution Develop outline to algorithm Expand to precise steps – tasks and order Test algorithm for correctness Desk checking – walk through the logic of algorithm Code the algorithm Run on the computer Using compiler and test data to check for errors Document and maintain program 9

PROGRAM LIFE CYCLE aka Software/Program development cycle 9 Steps: 1. 2. 3. 4. 5. PROGRAM LIFE CYCLE aka Software/Program development cycle 9 Steps: 1. 2. 3. 4. 5. 6. 7. 8. 9. Problem specification Problem analysis Structured program design Program coding Program debugging Program testing Program installation Program maintenance Program documentation 10

(1) PROBLEM SPECIFICATION Definition of problem by user to system analyst/programmer This might include (1) PROBLEM SPECIFICATION Definition of problem by user to system analyst/programmer This might include new user requirements Need to be read carefully to understand completely the user requirements 11

Project Management 12 Project Management 12

More Perceptions…. 13 More Perceptions…. 13

14 14

(2) PROBLEM ANALYSIS a Includes : Initialization • Giving specific/initial value to variables eg. (2) PROBLEM ANALYSIS a Includes : Initialization • Giving specific/initial value to variables eg. Total = 0 Input definition • Data to be fed into the computer – from keyboard/file Output definition • layout of the output – screen/printer Processing requirements • Solution algorithm/sequence of steps required to transform input to the required output Processing controls • Validation checks on input for accurate processing Program test plan • A range of test data to ensure the correctness of the program. This includes the calculation of expected result to verify the actual result produced. 15

(3) STRUCTURED PROG. DESIGN Use of proper design techniques to illustrate the program specification (3) STRUCTURED PROG. DESIGN Use of proper design techniques to illustrate the program specification in a systematic way representing the tasks required in the program specification showing all input, processing and output requirements Examples : Top-down stepwise refinement method Pseudocode JSP 16

(4) PROGRAM CODING Translation of design into programming language code e. g. C, C++, (4) PROGRAM CODING Translation of design into programming language code e. g. C, C++, Pascal, QBasic aka implementation 17

(5) PROGRAM DEBUGGING The task of finding and removing errors/bugs from the program. 3 (5) PROGRAM DEBUGGING The task of finding and removing errors/bugs from the program. 3 types of errors: Syntax – error relating to the programming language used Logical – error in programming logic Run-time/execution – error during running/ execution of program 18

(6) PROGRAM TESTING Is done using test data specified in the problem analysis. Compiler (6) PROGRAM TESTING Is done using test data specified in the problem analysis. Compiler is required to detect for Syntax and execution error, while specific test data to detect for logical error. repeatedly done until program is running and producing the output as required Less time required for testing if the program is well designed. 19

(7) PROGRAM INSTALLATION Program is transferred to the user’s machine and made to work. (7) PROGRAM INSTALLATION Program is transferred to the user’s machine and made to work. 3 strategies: The new program may run concurrently with the old one or it may completely replaced the old one or It may phased in gradually 20

(8) PROGRAM MAINTENANCE Once the program is installed and operated, it will be monitored (8) PROGRAM MAINTENANCE Once the program is installed and operated, it will be monitored for some time to ensure that program is error-free. Corrections will have to be made to the affected program/part of the program. 21

(9) PROGRAM DOCUMENTATION All processes in all stages during the system development must be (9) PROGRAM DOCUMENTATION All processes in all stages during the system development must be documented/written down. This documentation is vital for future reference/for future maintenance of the program. 22

CHARACTERISTICS OF A GOOD PROGRAM Correctness – fulfilling user requirements Reliability – it produces CHARACTERISTICS OF A GOOD PROGRAM Correctness – fulfilling user requirements Reliability – it produces correct output and validates input data to avoid program crash Portability – easily installed from one machine to another with minimal modification Maintainability – easily followed and modified, not only to the programmer who wrote it Readability – clearly documented Use of resources – fast in processing, uses minimum storage space and able to run on existing hardware. 23

STRUCTURED PROGRAMMING A programming methodology for constructing hierarchically ordered modular programs using standardised control STRUCTURED PROGRAMMING A programming methodology for constructing hierarchically ordered modular programs using standardised control structures Structured programming helps to write effective, error-free programs – new/modified. There are many ways to solve a problem, but some are more desirable than others. Original concepts was first published in 1964 by Bohm and Jacopini. Designing of programs using a Structure Theorem which eliminating the GOTO statement and established a structured framework for representing solution. 24

STRUCTURED PROGRAMMING Structure theorem is based on 3 control structures – sequence, selection and STRUCTURED PROGRAMMING Structure theorem is based on 3 control structures – sequence, selection and iteration. Structured programming incorporates the concepts of stepwise refinement method and modular design. Modular design groups tasks which perform the same function together e. g. printing of report headings, calculation etc. 25

Structured Techniques Some of the techniques for program design: Structured English Flowcharts Top-down stepwise Structured Techniques Some of the techniques for program design: Structured English Flowcharts Top-down stepwise refinement method Jackson Structured Programming method Pseudocode etc. 26

Problem specification A program is required to take in 2 numbers as integers, then Problem specification A program is required to take in 2 numbers as integers, then calculate and display the sum. 27

Structured English A method of specifying unambiguous narrative used to define procedural logic. It Structured English A method of specifying unambiguous narrative used to define procedural logic. It lacks the precision of meaning and simplicity when programming. Example : 1. 2. 3. Accept first integer and second integer Add first integer and second integer and then put the result in sum Display sum 28

Flow Chart START Diagrams which show the sequence of steps to perform a specific Flow Chart START Diagrams which show the sequence of steps to perform a specific programming task Get num 1 & num 2 Sum : = num 1 + num 2 Display sum END 29

Top-Down Stepwise Refinement Method a process of developing a program by performing a sequence Top-Down Stepwise Refinement Method a process of developing a program by performing a sequence of refinement steps. First general solution is outlined and then is broken down into more detailed steps Uses basic control structures : • sequence structure • selective structure • iterative structure 30

Top-Down Stepwise Refinement Method Example : Initialization 1. Read num 1, num 2 2. Top-Down Stepwise Refinement Method Example : Initialization 1. Read num 1, num 2 2. Calculate sum = num 1 +num 2 3. Display sum none Input Definition num 1, num 2 Output Definition Sum = 999 Processing Requirements Processing Control 1. Numbers must be integer 31

Top-Down Stepwise Refinement Method Test Data and Expected Result num 1 29 30 50. Top-Down Stepwise Refinement Method Test Data and Expected Result num 1 29 30 50. 8 num 2 50 120 20 sum 79 150 error Calculate Sum Read num 1 Read num 2 Sum = num 1 + num 2 Print Sum STRUCTURED CHART 32

Jackson Structured Program (JSP) Method Uses the same principle of control structures but concentrates Jackson Structured Program (JSP) Method Uses the same principle of control structures but concentrates on both data structures and program structures Combine the input/output data structures to form a program structure, identify the condition and Actions involved, produce the pseudocode and implement the pseudocode 33

Jackson Structured Program (JSP) Method Input Data num 1 num 2 INPUT DATA STRUCTURE Jackson Structured Program (JSP) Method Input Data num 1 num 2 INPUT DATA STRUCTURE Output Sum OUTPUT DATA STRUCTURE 34

Jackson Structured Program (JSP) Method Calculate Sum num 1 num 2 Sum PROGRAM STRUCTURE Jackson Structured Program (JSP) Method Calculate Sum num 1 num 2 Sum PROGRAM STRUCTURE 35

Jackson Structured Program (JSP) Method List of Conditions none List of Actions A. Input Jackson Structured Program (JSP) Method List of Conditions none List of Actions A. Input Action 1. Read num 1 2. Read num 2 B. Output Action 3. Print sum C. Computation 4. Sum = num 1 + num 2 36

Jackson Structured Program (JSP) Method ALLOCATION OF ACTIONS Calculate Sum num 1 1 num Jackson Structured Program (JSP) Method ALLOCATION OF ACTIONS Calculate Sum num 1 1 num 2 Sum 2 4, 3 37

Pseudocode aka Schematic Logic Represent the statements of algorithm in English which makes it Pseudocode aka Schematic Logic Represent the statements of algorithm in English which makes it easy to read and write and to convert it to the targeted programming language. Translation stage between the program structure diagram and the program code Example : BEGIN PROCESS_SUM sequence READ num 1, num 2; sum = num 1 + num 2; WRITE sum; END PROCESS_SUM sequence 38

Pseudocode properties Understandable to user Hierarchically structured and using indentation to show this structure Pseudocode properties Understandable to user Hierarchically structured and using indentation to show this structure Similar structure to the targeted programming code Comments are clearly marked 39

Pseudocode rules Indentation Control structures are clear sequence steps are placed on separate lines, Pseudocode rules Indentation Control structures are clear sequence steps are placed on separate lines, each is ended with a semicolon. Continuation lines are indented if more than one line is required. Use of keywords to show the structures e. g IF, THEN, ELSE, WHILE etc The logic e. g. AND, OR, NOT Keywords may be selected to be independent of the language descriptions 40

Pseudocode rules Block of instructions are grouped together having meaningful name to describe the Pseudocode rules Block of instructions are grouped together having meaningful name to describe the function (modularization) Keywords and names of blocks are in capital letters, others are not Comments are delimited with a beginning asterisks and ends with semicolon Use of parenthesis to show precedence of operations to avoid ambiguities Make the end of a structure clear using ENDIF, ENDDO etc 41

Keywords KEYWORD DESCRIPTION PROCEDURE Starts a logical segment ENDPROCEDURE Terminates a logical segment BEGIN Keywords KEYWORD DESCRIPTION PROCEDURE Starts a logical segment ENDPROCEDURE Terminates a logical segment BEGIN Starts a logical block within a segment ENDBEGIN Terminates a begin block GET / READ Input data PUT / WRITE Output data IF…THEN Conditional branch IF…THEN…ELSE Conditional branch (two ways) 42

Keywords KEYWORD ENDIF DESCRIPTION Terminates IF structure DO…WHILE Repetitive block DO…UNTIL Repetitive block that Keywords KEYWORD ENDIF DESCRIPTION Terminates IF structure DO…WHILE Repetitive block DO…UNTIL Repetitive block that will be executed at least once DO…FROM…TO…BY Incremental block ENDDO Terminates a block CASEENTRY Starts a multiple branch ENDCASE Terminates a CASE structure CALL Invocation of a subroutine 43

Problem specification Example 1 A program is required to read three numbers, add them Problem specification Example 1 A program is required to read three numbers, add them together and print their total 44

Problem analysis Using IPO Chart (Input-Process-Output Chart) INPUT PROCESS OUTPUT (nouns & adjectives) (verbs Problem analysis Using IPO Chart (Input-Process-Output Chart) INPUT PROCESS OUTPUT (nouns & adjectives) (verbs & adverbs) (nouns & adjectives) number 1 number 2 number 3 Read 3 numbers Calculate total Print total 45

Problem analysis (step-wise refinement method) Initialization none b. Input Definition number 1 , number Problem analysis (step-wise refinement method) Initialization none b. Input Definition number 1 , number 2, number 3 c. Output Definition The sum is = 99999. 9 d. Processing Requirements 1. Read number 1, number 2, number 3 2. Calculate total a. total = number 1 + number 2 + number 3 3. Print total 46

Problem analysis (step-wise refinement method) e. f. Processing Controls none Test Data and Expected Problem analysis (step-wise refinement method) e. f. Processing Controls none Test Data and Expected Results number 1 number 2 number 3 total 40 50 60 150. 0 10. 5 20. 2 30. 8 61. 5 10 20 -5 25. 0 47

Variables Variables/objects in the problem and the processing steps/program are referenced by names. Names Variables Variables/objects in the problem and the processing steps/program are referenced by names. Names must be unique and short (max 25 letters) but meaningful Names are used to identify a particular storage area in the memory. Good Examples : number, sum, average, min, max. Bad Examples: a, b, c, m 1, x 2 48

Problem specification Example 2 A program is required to read in the maximum and Problem specification Example 2 A program is required to read in the maximum and minimum temperatures on a particular day and calculate and print the average temperature 49

Problem analysis INPUT PROCESS OUTPUT (nouns & adjectives) (verbs & adverbs) (nouns & adjectives) Problem analysis INPUT PROCESS OUTPUT (nouns & adjectives) (verbs & adverbs) (nouns & adjectives) Min_temp Max_temp Read max_temp, min_temp Calculate average temp Print average temp avg_temp 50

Problem analysis (step-wise refinement method) Initialization none b. Input Definition max_temp, min_temp c. Output Problem analysis (step-wise refinement method) Initialization none b. Input Definition max_temp, min_temp c. Output Definition Average Temperature = 9999. 9 d. Processing Requirements 1. Read max_temp, min_temp 2. Calculate average a. avg_temp = (max_temp + min_temp) / 2 3. Print avg_temp 51

Problem analysis (step-wise refinement method) e. f. Processing Controls 1. Temperatures cannot be negative Problem analysis (step-wise refinement method) e. f. Processing Controls 1. Temperatures cannot be negative values 2. Average temperature is rounded to 1 d. p. Test Data and Expected Results max_temp min_temp avg_temp 60 50 55. 0 85 60 72. 5 30 -10 error 52

Problem specification Example 3 Design a program to print the student number and grade Problem specification Example 3 Design a program to print the student number and grade given the student number and four test marks. The grade is based on the following rules : average mark ≥ 80 ≥ 65 and < 80 ≥ 50 and < 65 < 50 grade Distinction Merit Pass Fail 53

Problem analysis Using IPO Chart (Input-Process-Output Chart) INPUT PROCESS OUTPUT (nouns & adjectives) (verbs Problem analysis Using IPO Chart (Input-Process-Output Chart) INPUT PROCESS OUTPUT (nouns & adjectives) (verbs & adverbs) (nouns & adjectives) student. No Test 1 Test 2 Test 3 Test 4 Read student. No, test 1, test 2, test 3, test 4 Calculate average mark Decide grade Print student. No, grade student. No grade 54

Problem analysis (step-wise refinement method) Initialization no_test = 4 b. Input Definition student. No, Problem analysis (step-wise refinement method) Initialization no_test = 4 b. Input Definition student. No, test 1, test 2, test 3, test 4 c. Output Definition Student No : XXXXXX Grade : XXXXX d. Processing Requirements 1. Read student. No, test 1, test 2, test 3, test 4 2. Calculate average a. avg_mark = (test 1 + test 2 + test 3 + test 4) / no_test 55

Problem analysis (step-wise refinement method) d. Processing Requirements (Cont. . ) 3. Decide Grade Problem analysis (step-wise refinement method) d. Processing Requirements (Cont. . ) 3. Decide Grade If avg_mark >= 80 grade = “Distinction” else if avg_mark >= 65 grade = “Merit” else if avg_mark >= 50 grade = “Pass” else grade = “fail” 4. Print student. No, grade 56

Problem analysis (step-wise refinement method) e. Processing Controls 1. Test mark should be in Problem analysis (step-wise refinement method) e. Processing Controls 1. Test mark should be in the range of 0 to 100 2. Average mark must be integer. 57

Problem analysis (step-wise refinement method) e. Test Data and Expected Results student. No Test Problem analysis (step-wise refinement method) e. Test Data and Expected Results student. No Test 1 Test 2 Test 3 Test 4 avg_mark grade Cp/01/21/01 90 90 80 85 86 Distinction Cp/02/21/01 60 50 75 80 66 Merit Cp/03/21/01 50 40 70 60 55 Pass Cp/04/21/01 40 30 30 40 35 Fail Cp/05/21/01 110 60 70 50 error Cp/06/21/01 60 -70 80 90 error 58

Problem specification Example 4 Find the average of 10 integers which lie between 0 Problem specification Example 4 Find the average of 10 integers which lie between 0 and 100 exclusive 59

Problem analysis Using IPO Chart (Input-Process-Output Chart) INPUT PROCESS OUTPUT (nouns & adjectives) (verbs Problem analysis Using IPO Chart (Input-Process-Output Chart) INPUT PROCESS OUTPUT (nouns & adjectives) (verbs & adverbs) (nouns & adjectives) number (10 integers) Read number Calculate average Print average avg_num 60

Problem analysis (step-wise refinement method) a. b. c. Initialization total = 0 , num_count Problem analysis (step-wise refinement method) a. b. c. Initialization total = 0 , num_count = 0 Input Definition number Output Definition Average value = 9999. 9 61

Problem analysis (step-wise refinement method) d. Processing Requirements 1. While num_count < 10 I. Problem analysis (step-wise refinement method) d. Processing Requirements 1. While num_count < 10 I. II. Read number If number > 0 AND number <100 total = total + number III. Add 1 to num_count 2. Calculate average avg_num = total / num_count 3. Print avg_num 62

Problem analysis (step-wise refinement method) e. f. Processing Controls 1. Number must be > Problem analysis (step-wise refinement method) e. f. Processing Controls 1. Number must be > 0 and < 100 2. Average is rounded off to 2 d. p. Test Data and Expected Results number output 1, 2, 3, 4, 5…. 99 Print average 0 error 103 error 63