Anda di halaman 1dari 29

Lecture Notes 7

Department of Computer Science and Engineering


Sec-K,O,E,R,V
Course: CSC103 Fundamental Computer and
Applications

Basic Algorithm
Given By- Lecturer Fardina Fathmiul Alam
CSE Dept. IUBAT

Variable
A variable is a data name that may be
used to store a data value.
Variable hold a space in the computer
memory. Variable a declaring means
its hold a position in the computer
memory:

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

Input the value on y means:


x
y
z

Calculate z=x+y means:


x
y
z

Store the result on z means:


x
y
z

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
Step 1:
Step 2:
Step 3:

START

Input W,L
AL x W
Print A

Input
W, L

ALxW

Print
A

STOP

IFTHENELSE
STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif

IFTHENELSE
STRUCTURE
The algorithm for the flowchart is as
follows:
If A>B then
print A
Y
N
is
A>B
else
print B
Print
Print
A
B
endif

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

is
VALUE1>VALUE2

MAX VALUE1

MAX VALUE2

Print
The largest value is,
MAX
STOP

Pseudocode & Algorithm


Example 1: Write an algorithm to
determine a students 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
Step 3: if (GRADE <50) then
Print FAIL
else
Print PASS
endif

Input
M1,M2,M3,M4

GRADE(M1+M2+M3+M4)/4

IS
GRADE<5
0

PRINT
PASS

PRINT
FAIL

STOP

The End

Anda mungkin juga menyukai