Modified Leture Notes 7 CSC 103 Algorithm
Modified Leture Notes 7 CSC 103 Algorithm
a
• Valid name:
Average, hight,Counter_1, x1,ph_value
• Invalid Names:
123,(area),25th,%
Algorithm: Adding Two
Numbers
• Start
• Declare 3 variables x,y,z of int type
• Input the value on x
• Input the value on y
• Calculate z=x+y and store the
result on z
• Print z
• stop
• Declare 3 variables means:
x y z
• Input the value on x means:
x y z
5
• Input the value on y means:
x y z
5 + 7
• Calculate z=x+y means:
x y z
5 + 7 =
• Store the result on z means:
x y z
5 + 7 = 12
Algorithm: Find out average of
3 numbers
• Start
• Declare 4 variables a,b,c,d of int type
• Put the values on a,b,c
• Calculate d=(a+b+c)/3
• Store the result on d
• Print d
• stop
Algorithm: Find out the area of
a circle
• Start
• Declare 2 variables a and r of float
type
• Put the value on r
• Calculate a=3.1416*(r2)
• Store the result on a
• Print a
• Stop
Algorithm: Convert day to
month
• Start
• Declare two variables d and m.
• Put the value on d.
• Calculate m=d/30.
• Store the result on m
• Print m
• Stop
EXERSICE
• Ask for float number and divide
second by first
• Convert meter to kilometer
Algorithm: Read an integer
and print next & previous
integer
• Start
• Declare three variables c, x, y
• Put the value on c.
• Calculate x=c+1 (next int value)
• Print x.
• Calculate y=c-1 (prev int value)
• Print y.
• Stop
Relational Operators
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
Greater than or equal to
Less than or equal to
Not equal to
FLOW CHART
Flow Chart
A Flowchart
– Graphical Representation of Algorithm
– shows logic of an algorithm
– shows individual steps and their
interconnections
Flowchart Symbols
Basic
Example 3
Write an algorithm and draw a
flowchart that will read the two sides
of a rectangle and calculate its area.
Pseudocode
• Input the width (W) and Length (L) of a
rectangle
• Calculate the area (A) by multiplying L
with W
• Print A
Example 3
Algorithm START
Print
A
STOP
IF–THEN–ELSE
STRUCTURE
• The structure is as follows
If condition then
true alternative
else
false alternative
endif
IF–THEN–ELSE
STRUCTURE
• The algorithm for the flowchart is as
follows:
If A>B then
print A Y N
is
else A>B
print B
Print Print
endif A B
Example 5
• Write an algorithm that reads two values,
determines the largest value and prints the
largest value.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3: Print “The largest value is”, MAX
Example 5
START
Input
VALUE1,VALUE2
Y is N
VALUE1>VALUE2
Print
“The largest value is”,
MAX
STOP
Pseudocode & Algorithm
• Example 1: Write an algorithm to
determine a student’s final grade
and indicate whether it is passing or
failing. The final grade is calculated
as the average of four marks.
Pseudocode & Algorithm
Pseudocode:
• Input a set of 4 marks
• Calculate their average by summing and
dividing by 4
• if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
• Detailed Algorithm
• Step 1: Input M1,M2,M3,M4 of float
type
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Example
START
Step 1: Input M1,M2,M3,M4 of float type
Step 2: GRADE (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0
PRINT PRINT
“PASS” “FAIL”
STOP
The End