Top Banner
FP101
23
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 2.3 Apply the different types of algorithm to solve problem

FP101

Page 2: 2.3 Apply the different types of algorithm to solve problem

WEEK 6

CLO 2

Apply the different types of algorithm to solve problem efficiently.

Page 3: 2.3 Apply the different types of algorithm to solve problem

Types of Algorithm

• Once you fully understand the problem and have clarified any questions you have, you need to develop your solution.

• Algorithm is a set of instructions for the computer• Setting up the algorithms is probably the hardest

part of problem solving on the computer• The instructions cannot assume anything, cannot

skip steps, must be executable one step at a time and must be complete

Page 4: 2.3 Apply the different types of algorithm to solve problem

Example 1:

Below is the algorithm to explain the morning activity for an employee, before going to the office.

1. Get up from bed2. Shower3. Get dressed4. Have breakfast5. Drive to the office

Types of Algorithm

Page 5: 2.3 Apply the different types of algorithm to solve problem

Types of Algorithm

• Example 2:

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

Page 6: 2.3 Apply the different types of algorithm to solve problem

Types of Algorithm

• Example 3:

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

Page 7: 2.3 Apply the different types of algorithm to solve problem

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

Page 8: 2.3 Apply the different types of algorithm to solve problem

Types of Algorithm

• Example 4:

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

Page 9: 2.3 Apply the different types of algorithm to solve problem

Types of Algorithm

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

Page 10: 2.3 Apply the different types of algorithm to solve problem

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.

Page 11: 2.3 Apply the different types of algorithm to solve problem

Pseudo Code

• Close to the actual language the programmer will be using

• Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading.

Page 12: 2.3 Apply the different types of algorithm to solve problem

Pseudo Code

• typically omits details that are not essential for human understanding of the algorithm, such as variable declarations, system-specific code and subroutines.

• No standard for pseudocode syntax exists, as a program in pseudocode is not an executable program

Page 13: 2.3 Apply the different types of algorithm to solve problem

• Example 1– Write a pseudo code to calculate the total of 3

numbers entered by user.

Start Program

Start Program START

Input number1, number2, number3

Sum = number1+ number2 + number3

Print SumENDEnd

Program

End Program

Display output on the screen

Display output on the screen

Instruction “input” tells the computer to obtain 3 numbers from user and

save it into variable number1, number2 and number3

Instruction “input” tells the computer to obtain 3 numbers from user and

save it into variable number1, number2 and number3

Instruction that tells the computer to add the 3

numbers

Instruction that tells the computer to add the 3

numbers

Pseudo Code

Page 14: 2.3 Apply the different types of algorithm to solve problem

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

Page 15: 2.3 Apply the different types of algorithm to solve problem

Pseudo Code

Answer:

START

Input hours _work, pay_rate

Salary= hours_work * pay_rate

Print Salary

END

Page 16: 2.3 Apply the different types of algorithm to solve problem

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

Page 17: 2.3 Apply the different types of algorithm to solve problem

Pseudo Code

Answer:

START

Input radius_of_circle

Area_of_circle= 3.14*radius*radius

Print Area_of_circle

END

Page 18: 2.3 Apply the different types of algorithm to solve problem

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

Page 19: 2.3 Apply the different types of algorithm to solve problem

Flowchart

• Symbols usedSymbol Function

Start/ end

process

Input/ output

condition

Flow lines

connector

Page 20: 2.3 Apply the different types of algorithm to solve problem

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

Page 21: 2.3 Apply the different types of algorithm to solve problem

Flowchart

Answer:start

Input hour_works, pay_rate

Salary = hour_works * pay_rate

Print Salary

end

Page 22: 2.3 Apply the different types of algorithm to solve problem

Case Study

Write the Problem Analysis, algorithm, pseudo

code and draw a flowchart based on the problem

given.

Question:You had bought a nice shirt with 15% discount. Count the

nett price for the shirt

Page 23: 2.3 Apply the different types of algorithm to solve problem

Case Study

Answer:Problem AnalysisInput : shirt cost

Process :

1. Set discount=0.15

2. Calculate discount price using formula:

discount price = discount x shirt cost3. Calculate total price after discount using formula:

total price = shirt cost – discount price

Output : discount price, total price