Você está na página 1de 24

DFC 1023 PROBLEM SOLVING &

PROGRAM DESIGN

2.3 PROBLEM SOLVING METHOD


DEFINITION OF ALGORITHM
 Algorithm is a set of instructions for the computer
 a step-by-step procedure for solving a problem or
accomplishing some end especially by a computer
 The instructions cannot assume anything, cannot skip
steps, must be executable one step at a time and
must be complete
EXAMPLE OF ALGORITHM

Example 1:
Below is the algorithm to explain the morning
activity for an employee, before going to the
office.
1. Get up from bed
2. Shower
3. Get dressed
4. Have breakfast
5. Drive to the office
Example 2:
Algorithm to make a coffee.
1. Boil water
2. Put coffee into cup
3. Pour boiled water into cup
4. Add sugar
5. Stir the mix
6. Taste it
7. If sugar not enough, repeat
step 4 to add sugar
8. Else coffee is done
 Example 3:
Algorithm to solve the problem of calculating the
average of 3 numbers.

1. Set Total=0, Average=0;


2. Input number1, number2, number3
3. Calculate sum of all 3 numbers using
formula:
Total = number1+number2+number3
Calculate average using the formula:
Average = Total/3
5. Print Average
 Example 4:
Write an algorithm to solve the problem below:
Calculate the salary of an employee who works
hourly basis. The formula to be used is Salary =
hour works * pay rate
Types of Algorithm

Answer:
1. Input the hours work and pay rate
2. Calculate salary using the formula:
Salary= hours work x pay rate
3. Print Salary
• Example 5:
Write an algorithm to solve the problem of
calculating the area for a circle based on
problem analysis given below:
Problem Analysis:
Input : radius of circle
Process : Calculate area for a circle using the formula -
Area of circle= 3.14 x radius x radius
Output: Area of circle
Answer:
1. Input radius of circle
2. Calculate area of a circle using formula:
Area of circle= 3.14*radius*radius
3. Print Area of circle
TYPES OF ALGORITHM

Pseudo Code

Flowchart
PSEUDO CODE
Uses English like statements in place of the
flowchart graphical symbols.
It is easier to code a program from it than from
flowchart.
It is not tied to any programming language.
 It is easy to modify but not graphical, difficult
to use for logically complex problems, and
slower to create.
Pseudo Code
 Example 1
 Write a pseudo code to calculate the total of 3 numbers
entered by user.

Start
Program START

Input number1, number2, number3

Sum = number1+ number2 + number3


Display output on
Print Sum the screen
End
Program
END

Instruction “input” tells the computer Instruction that tells the


to obtain 3 numbers from user and computer to add the 3
save it into variable number1, numbers
number2 and number3
Pseudo Code
 Example 2:
Write a pseudo code to solve the problem below:
Calculate the salary of an employee who works
hourly basis. The formula to be used is Salary =
hour works * pay rate
Pseudo Code

Answer:
START
Input hours _work, pay_rate
Salary= hours_work * pay_rate
Print Salary
END
Pseudo Code
• Example 3:
Write a pseudo code to solve the problem of
calculating the area for a circle based on
problem analysis given below:
Problem Analysis:
Input : radius of circle
Process : Calculate area for a circle using the formula -
Area of circle= 3.14 x radius x radius
Output: Area of circle
Pseudo Code

Answer:
START
Input radius_of_circle
Area_of_circle= 3.14*radius*radius
Print Area_of_circle
END
FLOWCHART
• graphic representations of the algorithm
• shows the flow of processing from the
beginning to the end of a solution
• each block in a flowchart represents one
instruction from an algorithm
• Flow lines indicate the direction of the data
flow
Flowchart
• Symbols used
Symbol Function

Start/ end

process

Input/ output

condition

Flow lines

connector
Flowchart
• Example 1:
Draw a flow chart based on the problem given
below:
Calculate the salary of an employee who
works hourly basis. The formula to be used is
Salary = hour works * pay rate
Flowchart
Answer: start

Input hour_works, pay_rate

Salary = hour_works * pay_rate

Print Salary

end
Flowchart
• Example 2:
Draw a flow chart based on the problem given
below:
Add two number that entered by user.
Answer:
Exercise 1:

a) In the flowchart above, what will be the first action you take?
b) If you haven’t gone 50 steps yet, what will you do next?
c) If you’ve gone 50 steps, what do you do?
Exercise 2:
• On a separate sheet of paper, make a flowchart organizing the “flow” of
getting ready to go to school in the morning.
• Be sure to include the following steps in your chart, but don’t be afraid to
add other things if you need them!

Você também pode gostar