0% found this document useful (0 votes)
26 views25 pages

Algorithm Development

A pdf on algorithm development.

Uploaded by

rahiman.zoha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views25 pages

Algorithm Development

A pdf on algorithm development.

Uploaded by

rahiman.zoha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Algorithm

Development
What is an Algorithm

• It is the set of logical steps you will apply to solve a problem.


• An algorithm is expected to have the following characteristics:
• Algorithm steps should be clear and decisive.
• After a certain number of steps, the algorithm should end.
• Algorithms should be general enough to handle all possibilities.
1- Analyze the problem

• You must understand exactly what you need to do.


• “Understanding a problem is half-solving it.”
• Given a problem,
• you should identify the problem inputs, that is, the data you need to work with;
• outputs, that is, the desired results;
• any additional requirements or constraints on the solution,
• required format in which the results should be displayed (for example, as a
table with specific column headings) and
• a list of problem variables and their relationships (in terms of formulas).

3
1- Analyze the problem

• Problem: Given a distance in meters and the speed of a car in


miles per hour (mph), calculate how many minutes it will take
for that car to travel that distance.
• Inputs: distance (in meters)
speed (in mph)

• Output: time (in minutes)

4
1- Analyze the problem

• Develop a list of formulas that specifies relationships between


them.
time = distance / speed
• We need to calculate the distance in miles. 1 mile is 1.609344 km,
thus 1609.344 meters. So:
distance in miles = distance in meters / 1609.344
• Notice also that, we are required to calculate the time in minutes
time in minutes = time in hours x 60
5
2- Develop an algorithm

• Real life Examples:


• A recipe to prepare a meal
• list of ingredients and
• step by step instructions

6
Pseudocode

• Pseudocode: fake code


• Informal language that has no syntax rule
• Not meant to be compiled or executed
• Used to create model program
• No need to worry about syntax errors, can focus on program’s design
• Can be translated directly into actual code in any programming language
Algorithm
Start
Example 1
Enter first number
(a)
• Algorithm to print which of the two
Enter second number
entered number is smaller: (b)

if a < b print “First number is smaller”

if not a < b print “Second number is smaller”

End
Star
t

a, b

Flow Chart
a<
b Yes

(Ex. 1)
No

b is a is
smaller smaller

End
Algorithm
Start
Example 2
Enter first number
(a)

• Algorithm that calculates the Enter second number


(b)
average of two entered numbers:
Find summation of the two numbers and assign the
result to the sum (sum = a + b)

A5 Divide the sum by 2 and assign the result to the average


(average = sum / 2)

Print average

End
Star
t

a, b

sum = a +

Flow Chart b

(Ex. 2) average = sum /


2

average

End
Pseudocode Example

• Problem: Display Unsuccessful if the average of two


grades is less than 50, Successful otherwise
Input grade1, grade2
average = (grade1 + grade2) / 2
if (average < 50)
Output “Unsuccessful”
else
Output “Successful”
13
Algorithm
Example 3 Start

Enter a number
(number)
• Algorithm that prints whether an
entered number is positive or if number = 0 repeat A2
negative:
if number < 0, print “the entered number is negative” and
end the program

if number > 0, print “the entered number is positive”

End
Start

num

Yes

num =

Flow Chart 0

No

(Ex. 3) num <


0
No
num is
positive

Yes

num is
negative

End
Start

b, h

Flow Chart a=

(Ex. 4) b*h/2

End
Algorithm
Example 4 Start

Enter the base (b)

• Algorithm that calculates the area of


a triangle using a side length and Enter the height (h)
height of that side:
A4 Multiply the base by the height and divide by 2 and assign
the result to a (a=b*h/2)

Print a

End
Algorithm
Example 5 Start

Enter midterm grade (mt)


• Algorithm that calculates the
average of a student's midterm Enter final grade (fin)
grade and final grade (calculate by
taking 30% of the midterm grade A4 Take 30% of the midterm grade, 70% of the final grade and
and 70% of the final grade): add the two together and assign the result to the average
(average=0.3*mt+0.7*fin)

Print average

End
Start

mt

fin

Flow Chart
(Ex. 5) average = 0.3*mt +
0.7*fin

average

End
Algorithm
Example 6 Start

Enter radius (r)


• Algorithm for calculating the
circumference and area of a circle Take pi as 3.14
using radius (take pi as 3.14):
A4 Multiply the radius by the number of Pi and double it and
assign it to the circumference (circumference = 2*pi*r)

Multiply the pi by the square of the radius and assign to


the area (area = pi*r*r)
Print circumference and area

End
Start

pi =
3.14

Flow Chart circumference =


2*pi*r

(Ex. 6) area =
pi*r*r

circumference
, area

End
Start

num = 0

Flow Chart Yes


num >
999

(Ex. 7) num = num +


No

num

End
Algorithm
Example 7
Start
• Algorithm to print numbers from 1
assign zero value to number
to 1000:
if number is greater than 999 end the
program
If not add 1 to number and assign result to number again (number =
number+1)
Print number and repeat A3

End
Algorithm Example
8
Start

• Algorithm that converts an angle


Enter the angle in degrees (d)
entered in degrees to Radians and
Gradians:
A3 multiply the entered angle by the number pi, divide by 180 and
assign to the radian (r=d*pi/180)

A4 multiply the entered angle by 200, divide by the number pi and assign to the
grad (g=d*200/pi)

Print radian and grad

End
Start

Flow Chart
(Ex. 8)

End

You might also like