This too.pdf
Document Details
Uploaded by UnrestrictedCamellia
Full Transcript
Design Solution IT 210- INFORMATION TECHNOLOGY FUNDAMENTALS Design Solution Designing a solution algorithm Most challenging task in the life cycle of a program First attempt at designing a solution algorithm usually does no t result in a finished...
Design Solution IT 210- INFORMATION TECHNOLOGY FUNDAMENTALS Design Solution Designing a solution algorithm Most challenging task in the life cycle of a program First attempt at designing a solution algorithm usually does no t result in a finished product Pseudocode is useful in the trial-and-error process where it is relatively easy to add, alter or delete instructions If th e algorithm is no t correct, th e program will never be correct It is impor tant no t to start coding until necessary steps of defining the problem and designing th e solution algorithm have been completed. Your solution algorithm should be tested against all possible inputs to confirm its appropriateness. Checking the solution algorithm After a solution algorithm has been established, it must be tested for correctness It is necessary because most major logic errors occur during the development of the algorithm (not detected then these errors can be passed on to the program) Easier to detect errors in pseudocode tha n in the corresponding program Desk checking involves tracing through the logic of the algorithm with some chosen test data Walk through the logic of the algorithm exactly as a computer would, keeping track of all major variables values on a sheet of paper Helps detect errors early and allows the user to become familiar with the way the program runs This P hoto by Unknow n Author is licensed under CC BY-NC Checking the solution algorithm Steps in desk checking an algorithm – Choose simple input test cases that are valid – Establish the expected result for each test case – Make a table on a piece of paper of the relevant variable names within the algorithm – Walk the first test case through the algorithm – Repeat the walk-through process using other test data – Check the expected result established in Step 2 matches the actual in Step 5 The Structure Theorem There are thre e basic control structures Sequence Selection Repetition IT210_1442_1 5 This P hoto by Unknow n Author is licensed under CC BY The selection The condition in the IF statement is based on a comparison of two items and it is expressed with the following relational < LES S TH AN < = LES S THAN OR EQ UAL TO operations: >greater than >=greater than on equal to There are several variation of the selection =equals to not equal to structure as follows: 1. Simple selection (simple IF statement) 2. Simple selection with null false branch (null ELSE statement) 3. Combined selection (combined IF statement) 4. Nested selection (nested IF statement) Simple selection 1. Simple selection (simple IF statement) Simple selection occurs when a choice is made between two alternate paths depending on the result of a condition being true or false Keywords: IF THEN, ELSE, ENDIF Simple selection with null false 2. Simple selection with null false branch (null ELSE state ment) The null ELSE structure is a variation of the simple IF structure It is used when a task is performed only when a particular condition is true No processing will take place if condition is false In the example, the part_time_count will be altered only if the student’s attendance patte rn is part-time Combined selection 3. Combined selection (combined IF statement) IF statement with AND or OR connector Example IF, AND connector In the example, stude nt record will undergo two test. Only those students who are female and who attend par t-time will be selected; counter go up. If either condition is to be found to be false, the counter will not change Combined seclection Example IF, OR connector In the example, Counter will only increase if The stude nt is par t-time regardless of gender The stude nt is female regardless of attendance pattern Combined selection More t han two condition can be linked to gether with t he AND or OR operators. Remark: parentheses m ust be used to avoid ambiguity Combined selection The NOT operator can be used for the logical negation of a condition Note th at the AND and OR operators can also be used with the NOT operator, but great care m ust be taken and parentheses used to avoid ambiguity Nested selection 4. Nested selection (nested if statement) Can be classified as Linear nested IF statements Non-linear nested IF statements The linear nested selection Linear nested IF statem ent is used whe n a variable is being tested for various values and a different action is to be taken for each value It is called Linear due to each ELSE immediately follows t he IF condition to which it corresponds Note there are an equal num be r of IF, ELSE and ENDIF and indentatio n makes it easier to read and understand The linear nested selection The linear nested IF structure can be replaced with a case control structure This simplifies t he basic selection control structure and extends it from a choice between two values to a choice of multiple values In pseudocode, t he keywords CASE OF and ENDCASE serve to identify the structure with t he multiple values indented The non linear nested if Note: Equal num ber of IF, ELSE and ENDIF Practice For the example below, the problem will be defined, solution algorithm will be developed and the algorithm will be manually tested Design an algorithm th at will prompt a user for three characters, accept those characters as input, sor t t he m into ascending sequence and out put t he m onto screen. Practice Define th e diagram Practice Solution algorithm Read_three_characters 1 Prompt the operator for char_1, char_2, char_3 Solution algorithm requires a 2 Get char_1, char_2, char_3 series of IF statements to sort the 3 IF char_1 > char_2 THEN temp = char_1 three characters into ascending sequence char_1 = char_2 Logic of the algorithm is char_2 = temp concerned with the sorting of the ENDIF three characters into alphabetic sequence 4 IF char_1 char_2 > char_3 THEN temp = char_2 char_2 = char_3 Desk Checking char_3 = temp Set of valid characters will be used to ENDIF 5 IF char_1 > char_2 THEN check the algorithm; the characters k, b temp = char_1 and g will be used char_1 = char_2 char_2 = temp ENDIF 6 Output to the screen char_1, char_2, char_3 19 END Practice Desk checking Set of valid characters will be used to check the algorithm; the characters k, b and g will be used Practice Repeitation Types of loop: control WHILE loop Do..WHILE loop structure Automatic-Counter Loop Repetition using WHILE structure WHILE construct is a leading decision loop – that is the condition is tested before any statements are executed. The following processing takes place: 1. Logical condition p is tested 2. If condition p is found to be true, the statements within the statement block are executed once. The delimiter ENDWHILE then triggers a return of control to the retesting of condition p 3. If condition p is still true, the statements are executed again, and so the repetition process continues until the condition is found to be false 4. If condition p is found to be false, no further processing takes place within this loop Repetition using DO…WHILE structure Trailing decision loop DO…WHILE structure is similar to the WHILE structure (group of statements are repeated in accordance with specific condition) DO…WHILE structure tests condition at the end of the loop This means that the statement within the loop will be executed once before the condition is tested Example: Get the sum of 1, 2, 3, …100. Counted repetition Counted repetition occurs only once when the exact num ber of loop iterations is known in advance Simple keyword FOR is use as shown: The FOR loop does more t han repeat the stateme nt block. 1. Initialise the loop_index to the required intial_value 2. Increment the loop_index by 1for each pass through the loop 3. Test the value of loop_index at the beginning of each loop to ensure that it is within the stated range of values 4. Terminate the loop when the loop_index has exceeded the specified final_value Example: Process number pairs Design an algorithm t hat will prompt for and receive pairs of num be rs from an operator at a terminal and display their sum, product and average o n th e screen. If th e calculated sum is over 200, an asterisk is to be displayed beside the sum. The program is to terminate when a pair of zero values is entered. Example: Process number pairs Define diagram Example: Process number pairs Control structures required 1. A WHILE loop to control the repetition 2. An IF statement to determine if an asterisk is to be displayed Example: Process number pairs BEGIN Set sum to 0 Prompt "Enter number1, number2" Get number1, number2 WHILE NOT (number1 = 0 AND number2 = 0) sum