Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)

Similar presentations


Presentation on theme: "Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)"— Presentation transcript:

1 Program Design Tool

2 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location) Compare 2 variables and select 1 of 2 alternative actions Repeat a group of actions

3 Algorithm Steps involved in accomplishing a task In programming – a set of detailed, unambiguous and ordered instructions developed to describe the process necessary to produce the desired output from a given input Written in simple English Not a formal document Must : – Be lucid, precise and unambiguous – Give the correct solution in all cases – Eventually end

4 Pseudocode For representing algorithm Use structured English Like the high-level computer languages Conventions: – Statements are written in simple English – Each instruction is written on a separate line – Keywords and indentation are used to signify particular control structures – Each set of instructions is written from top to bottom, with only one entry and one exit – Groups of statements may be formed into modules, and that module given a name

5 What is inside? Naming Convention – Unique and meaningful names for variables 3 basic control structures – Sequence – Selection – Repetition

6 Basic Control Structure - Sequence Execution of once processing step after another Processing for input, output, calculation operations, assign values Ex: add 1 to pageCount Print heading line1 Print heading line2 Set lineCount to zero Read customer record

7 Basic Control Structure - Selection A condition and the choice between 2 actions Ex: IF student_status is part_time THEN add 1 to part_time_count ELSE add 1 to full_time_count ENDIF

8 Basic Control Structure - Repetition A set of instructions to be performed repeatedly as long as a condition is true or until a terminating condition occurs Ex: Set student_total to zero DOWHILE student_total < 50 Read student record Print student name, address to report add 1 to student_total ENDDO

9 Defining Problem Divide the problem into 3 separate components – Input: a list of source data provided to the problem – Output: a list of the outputs required – Processing: a list of actions needed to produce the required outputs

10 Developing an Algorithm Get the problem description and identify the nouns and adjectives used in the specification to establish input and output components Then, fill up the defining diagram Underline the verbs and adverbs used in the specification to establish the actions required Fill up the Processing column in the defining diagram Design the solution algorithm Perform desk check – To test for correctness – Must select the correct test data

11 Example 1: Add 3 number Get the problem description and identify the nouns and adjectives used in the specification to establish input and output components Ex: – A program is required to read three numbers, add them together and print their total. Then, fill up the defining diagram InputProcessingOutput number1total number2 number3

12 Example 1: Add 3 number Underline the verbs and adverbs used in the specification to establish the actions required Ex: – A program is required to read three numbers, add them together and print their total. InputProcessingOutput number1Read 3 numberstotal number2Add numbers together number3Print total number

13 Example 1: Add 3 number Solution algorithm Add_three_numbers 1Read number1, number2, number3 2total = number1 + number2 + number3 3Print total End InputProcessingOutput number1Read 3 numberstotal number2Add numbers together number3Print total number

14 Example 1: Add 3 number – desk check 6 steps in desk checking 1.Choose simple input test cases that are valid (2 – 3 test cases) 2.Establish what the expected result should be for each test case 3.Make a table of the relevant variable names within the algorithm 4.Walk-through the algorithm using the test case and keep track of the contents of each variable 5.Repeat the walk-through process using the other test data cases until the algorithm has reached its logical end 6.Check that the expected result established in Step 2 matches the actual result developed in Step 5

15 Example 1: Add 3 number – desk check 1 st data set2 nd data set number11040 number22041 number33042 1 st data set2 nd data set Total60123 Established test cases Established expected results

16 Example 1: Add 3 number – desk check Setup variables and pass each test data set through the solution algorithm statement by statement Statement nonumber1number2number3total 1 st pass 1102030 260 3print 2 nd pass 1404142 2123 3print Compare the result with expected result

17 Selection Control Structures To illustrate a choice between 2 or more actions depending on a condition is true or false Operators are used to compare OperatorDescription Relational Operator <Less than >Greater than =Equal to <=Less than or equal to >=Greater than or equal to <>Not equal to Logical Operator ANDBoth expression must be true OREither expression can be true NOTNegation expression

18 Selection Control Structures 4 types – Simple selection (simple IF statement) – Simple selection with null false branch (null ELSE statement) – Combined selection (combined IF statement) – Nested selection (nested IF statement) Linear nested IF statements Non-linear nested IF statements

19 Selection Control Structures Simple selection (simple IF statement) IF age < 10 THEN Print “under age” ELSE Print “OK” ENDIF Simple selection with null false branch (null ELSE statement) IF attendance = 0 THEN Print “barred from exam” ENDIF

20 Selection Control Structures Combined selection (combined IF statement) IF age > 12 AND gender = male THEN add 1 to male_count ENDIF

21 Selection Control Structures Nested selection (nested IF statement) – Linear nested IF statements IF mark > 80 THEN grade = ‘A’ ELSE IF mark > 70 THEN grade = ‘B’ ELSE IF mark > 60 THEN grade = ‘C’ ELSE IF mark > 50 THEN grade = ‘D’ ELSE grade =‘F’ ENDIF

22 Selection Control Structures Nested selection (nested IF statement) – Non-linear nested IF statements IF mark > 50 THEN IF attendance = part_time THEN add 1 to part_time_pass ELSE add 1 to full_time_pass ENDIF ELSE add 1 to fail_no ENDIF

23 Selection Control Structures IF structure IF grade = ‘A’ THEN add 1 to counter_A ELSE IF grade = ‘B’ THEN add 1 to counter_B ELSE IF grade = ‘C’ THEN add 1 to counter_C ELSE add 1 to counter_D ENDIF Case structure CASE OF grade ‘A’ : add 1 to counter_A ‘B’ : add 1 to counter_B ‘C’ : add 1 to counter_C ‘D’ : add 1 to counter_D ENDCASE

24 Repetition Control Structures 3 ways for a set of instructions can be repeated – Leading decision loop – Trailing decision loop – Counted loop

25 DOWHILE Structure Leading decision loop – Condition checking at the beginning of the loop – Types Known number of times DOWHILE count < 5 add 1 to count ENDDO Unknown number of times – Trailing record or sentinel controlled DOWHILE total_score<1000 Read exam_score add exam_score to total_score ENDDO – No trailer or sentinel exist DOWHILE more data DOWHILE more records DOWHILE records exist DOWHILE NOT EOF

26 REPEAT…UNTIL Structure Trailing decision loop REPEAT add 1 to count UNTIL count>=5

27 Counted Loop DO loop_index = initial_value to final_value statement block ENDDO

28 Flowchart Graphical representation of program logic by a series of standard geometric symbols and connecting lines

29 Flowchart SymbolDescription Terminal symbol -Indicates the starting or stopping point in the logic -Every flowchart should begin and end with a terminal symbol Input/Output symbol -Represents an input or output process in an algorithm -Ex: reading input or writing output Process symbol -Represents any single process in an algorithm -Ex: assign value Predefined process symbol -Represents a module in a algorithm -Predefined process has its own flowchart

30 Flowchart SymbolDescription Decision symbol -Represents a decision in the logic involving the comparison of 2 values -Alternative paths are followed, depending on whether the decision is true or false Flowlines -Connect various symbols -Contain an arrowhead only when the flow is not from top to bottom or left to right

31 Flowchart 3 basic control structures – Sequence Straightforward execution of one processing step after another One entrance and one exit

32 Flowchart 3 basic control structures – Selection Presentation of a condition, and the choice between 2 actions depending on whether the condition is true or false Construct represents decision-making abilities of the computer Represent selection control with a decision symbol with one line entering at the top, and two or more lines leaving following the selection condition met

33 Flowchart 3 basic control structures – Repetition Presentation of a set of instructions to be performed repeatedly as long as the condition is true Construct represented by a decision symbol and one or more process symbols to be performed while a condition is true A flowline then takes the flow of control back to the condition in the decision symbol that is tested before the process is repeated

34 Flowchart 3 basic control structures – Repetition Simple IF statement Null ELSE statement Combined IF statement Nested IF statement

35 Flowchart 3 basic control structures – Repetition Simple IF statement Null ELSE statement Combined IF statement Nested IF statement

36 Flowchart Ex: – A program is required to read three numbers, add them together and print their total.

37 Flowchart Process customer record A program required to read a customer’s name, a purchase amount and a tax code. – 0 tax exempt (0%) – 1 state sales tax only (3%) – 2 federal and state sales tax (5%) – 3 special sales tax (7%) The program must then compute the sales tax and the total amount due, and print the customer’s name, purchase amount, sales tax and total amount due. Fill up the defining diagram: InputProcessingOutput total

38 Flowchart The defining diagram: InputProcessingOutput cust_name purch_amt tax_code Read customer details Compute sales tax Compute total amount Print customer details cust_name purch_amt sales_tax total_amt

39 Flowchart

40 Error Handling Syntax refers to the rules governing the computer operating system, the language, and the application. An error is called a bug. A bug must be found and corrected, a process called debugging

41 Reference Sprankle, M & Hubbard, J. (2012) Problem Solving and Programming Concepts, Pearson Education, Inc., New Jersey, Chapter 2 Hanly, J.R. & Elliot B.K. (2009) Problem Solving and Program Design, Pearson Education, Inc., USA, Chapter 1, 2, 3, 4 & 5


Download ppt "Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)"

Similar presentations


Ads by Google