Problem Solving

22
Problem Solving Skill Area 305.1 terials Prepared by Dhimas Ruswanto, BMm

description

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. - PowerPoint PPT Presentation

Transcript of Problem Solving

Page 1: Problem Solving

Problem SolvingSkill Area 305.1

Materials Prepared by Dhimas Ruswanto, BMm

Page 2: Problem Solving

LECTURE OVERVIEW

• Problem Solving• Algorithm• Dry Run

Page 3: Problem Solving

PROBLEM SOLVINGProblem-Solving methodology:1. Define the problem2. Outline the solution3. Develop an algorithm4. Test the algorithm

Page 4: Problem Solving

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

Page 5: Problem Solving

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

Page 6: Problem Solving

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

Page 7: Problem Solving

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

Page 8: Problem Solving

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).

Page 9: Problem Solving

EXAMPLE OF ALGORITHM

MAKE A CALL¢

Page 10: Problem Solving

EXAMPLES OF ALGORITHM

Page 11: Problem Solving

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

Page 12: Problem Solving

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.

Page 13: Problem Solving

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.

Page 14: Problem Solving

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

Page 15: Problem Solving

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

Page 16: Problem Solving

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.

Page 17: Problem Solving

DRY RUN• You can test your program without

using a computer by dry running it 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

Page 18: Problem Solving

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

Page 19: Problem Solving

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

Page 20: Problem Solving

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

Page 21: Problem Solving

A SAMPLE OF DRY RUN TABLE

Afterexecution

First num

Second num

Sum Average

Line 2 0 0

Line 5 18 0

Line 7 18 24

Line 8 18 24 42

Line 10 18 24 42 21

Page 22: Problem Solving

SUMMARY• An algorithm is a finite set of unambiguous, executable instructions that directs a terminating activity.•Syntax:

o The grammar of the language•Semantics:

o The meaning•Dry runs involves the execution of code segments.