230 likes | 490 Views
Materials Prepared by Dhimas Ruswanto , BMm. Problem Solving. Skill Area 305.1. Lecture Overview. Problem Solving Algorithm Dry Run. PROBLEM SOLVING. Problem-Solving methodology: Define the problem Outline the solution Develop an algorithm Test the algorithm. DEFINE THE PROBLEM.
E N D
Materials Prepared by DhimasRuswanto, BMm Problem Solving Skill Area 305.1
Lecture Overview • Problem Solving • Algorithm • Dry Run
PROBLEM SOLVING Problem-Solving methodology: • Define the problem • Outline the solution • Develop an algorithm • Test the algorithm
DEFINE THE PROBLEM • Have a thorough understanding of the problem • State the problem in clear and concise terms to avoid any confusion or misunderstanding Examples:Calculate the sum and the average of two numbers 25 & 45
OUTLINE THE SOLUTION • The problem can be divided into three components: • Inputs – What do you have? • Outputs – What do you want to have? • Processing – How do you go from inputs to outputs? • Examples:Calculate the sum and the average of two numbers 25 & 45
OUTLINE THE SOLUTION (cont’d) • Inputs = • First number = a = 25 • Second number = b = 45 • Outputs = • sum • average • Processing = • sum = a +b • Average = a+b 2
ALGORITHM • An algorithm is a written step by step solution to a problem. • Why bother writing an algorithm? • For your own use in the future. You won’t have to rethink the problem. • So others can use it, even if they know very little about the principles behind how the solution was derived
ALGORITHM (cont’d) • An algorithm is a finite set of unambiguous, executable instructions that directs a terminating activity. • GOAL: To find a single set of instructions that can be used to solve any problem of a particular type (a general solution).
¢ EXAMPLE OF ALGORITHM MAKE A CALL
ALGORITHM REPRESENTATION • Syntax: • The grammar of the language • The way in which we construct sentences by following principles and rules • Semantics: • The meaning • The interpretations of and meanings derived from the sentence transmission and understanding of the message
SYNTAX AND SEMANTIC • Sentence : “Baby milk drinks” • Does not have a syntactic meaning • But through semantics most people would interpret it as meaning “Baby drinks milks”, therefore we can find a meaning from key words.
SYNTAX AND SEMANTIC (CONT’D) • In programming language: • Syntax defines how logical entities expressed in characters • Semantics describe the logical entities of a programming language and their interactions • Examples: • x = 0 - 1; y = - 1; • the syntax for the "-" token is the same, but, it has a different meaning ("semantic), depending where its used. • In the "x" assignment, "-" means the "subtraction" operation, In the "y" assignment, "-" means the "negative sign" operation.
DEVELOP AN ALGORITHM • Decompose the problem into simpler steps and expresses the solution in terms of • actions to be executed • order in which these actions are to be executed • Examples:Calculate the sum and the average of two numbers 25 & 45
DEVELOP AN ALGORITHM (CONT’D) • Declare two variables, a and b • Initialise both variables to 0 • a= 0, b= 0 • Ask user to enter first number • Assign user input to a variable • Ask user to enter second number • Assign user input to second b variable • Add first num to second num • Print the sum • Divide the sum by 2 • Print the average
TEST THE ALGORITHM • The programmer must make sure that the algorithm is correct. • The objective is to identify major logic errors early, so that they may be easily corrected. • Test data should be applied to each step, to check whether the algorithm actually does what it is supposed to.
DRY RUN • You can test your program without using a computer by dry runningit on paper • Dry runs involves the execution of code segments. • You act as the computer – following the instructions of the program, recording the valves of the variables at each stage • You can do this with a table
DRY RUN (Cont’d) • The table with have column headed with the names of the variables in the program • Each row in the table will be labelled with a line number form the program. • The entries of each row in the table will be values of the variables after the execution of the statement on that line • You don’t need a row for every line in the program, just those lines where some significant change to the state of the program occurs • e.g a variable gets a new value
DRY RUN (Cont’d) • In this table you can record all relevant changes to the variables as the program progresses, thereby test the logic of the program / algorithm • Do a dry run before you code your program on computer this way any logic errors will come to light during the dry run
Example of DRY RUN • L1 Declare two variables, a and b • L2 Initialise both variables to 0 • L3 a= 0, b= 0 • L4 Ask user to enter first number • L5 Assign user input to a variable • L6 Ask user to enter second number • L7 Assign user input to second b variable • L8 Add first num to second num • L9 Print the sum • L10 Divide the sum by 2 • L11 Print the average Do a Dry run for this program assuming the user enters 18 for first number and 24 for the second
SUMMARY • An algorithm is a finite set of unambiguous, executable instructions that directs a terminating activity. • Syntax: • The grammar of the language • Semantics: • The meaning • Dry runs involves the execution of code segments.