Algorithms and Flowcharts
Algorithms and Flowcharts
FLOWCHARTS
A typical programming task can be
divided into two phases:
Problem solving phase
produce an ordered sequence of
steps that describe solution of
problem
this sequence of steps is called
an algorithm
Implementation phase
implement the program in some
programming language
Name
Symbol
Use in Flowchart
Oval
Parallelogram
Rectangle
Diamond
Hybrid
Flow line
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
Detailed Algorithm
Step 1:
Input
M1,M2,M3,M4
Step 2:
GRADE
(M1+M2+M3+M4)/4
Step 3:
if (GRADE < 50)
then
Print FAIL
else
Print PASS
endif
The Flowchart
(Dictionary) A schematic
representation of a sequence of
operations, as in a manufacturing
process or computer program.
(Technical) A graphical representation
of the sequence of operations in an
information system or program.PRINT
Information system flowcharts show
PASS
how data flows from source
documents through the computer to
final distribution to users. Program
flowcharts show the sequence of
instructions in a single program or
subroutine. Different symbols are used
to draw each type of flowchart.
A Flowchart
shows logic of an algorithm
emphasizes individual steps and
their interconnections
e.g. control flow from one action
to the next
Flowchart Symbols : Basic
99
God Bless Everyone!
ST
Input
AR
Lft
T
Lcm
Prin
Lft
t30
xST
Lcm
OP
Example
Step 1:
Step 2:
Step 3:
PRINT
Lcm
Input M1,M2,M3,M4
GRADE (M1+M2+M3+M4)/4
if (GRADE <50) then
Print FAIL
else
Print PASS
endif
ST
AR
Input
T
M1,M2
GRADE(M1+
,M3,M
NM2+M3+M4)/4
Y
IS4
GRA
PRIN
DE<
T
ST
50
FAIL
OP
PRINT
FAIL
Example 2
Write an algorithm and draw a
flowchart to convert the length in feet
to centimeter.
Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by
multiplying LFT with 30
Print length in cm (LCM)
Algorithm
Step 1: Input Lft
Step 2:
Lcm Lft x 30
Step 3:
Print Lcm
Flowchart
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
Algorithm
Step 1:
Input W,L
Step 2:
A L x W
Step 3:
Print A
STA
RT
Input
W, L
ALx
W
Print
A
STO
P
PRINT
A
Example 4
Write an algorithm and draw a
flowchart that will calculate the roots
of a quadratic equation
Hint: d = sqrt (
), and the
roots are: x1 = (b + d)/2a and x2 =
(b d)/2a
Pseudocode:
Input the coefficients (a, b, c) of the
quadratic equation
99
God Bless Everyone!
Calculate d
Calculate x1
Calculate x2
Print x1 and x2
Algorithm:
Step 1:
Input a, b, c
Step 2:
d sqrt (
)
Step 3:
x1 (b + d) / (2 x a)
Step 4:
x2 (b d) / (2 x a)
Step 5:
Print x1, x2
ST
AR
Input
a,
b,
dT
c xb
x1 (b
+
sqrt(b
X
(b
24 /x(2
d)
a xx
Prin
d)c)
/ (2 x
a)
t
a)
ST
x1
OP
,x2
Y
P
ri
n
t
A
Pri
nt
A
i
s
A
>
B
PRINT
A
i
s
A
>
B
P
ri
n
t
B
PRINT
A
DECISION STRUCTURES
Y
PRINT
B
Pri
nt
B
Relational Operators
PRINT
B
Operat
or
Description
>
Greater than
<
Less than
Equal to
Not equal to
Example 5
Write an algorithm that reads two
values, determines the largest value
99
God Bless Everyone!
S
Input
T
Ais
Y VALU
N
VALUE1>
R
MAX E1,VA
VALUE2MAX
LUE2
T
Print
VALU The
VALU
S
E1 largest
E2
T
value
O is,
PRINT
MAX
PThe largest value is,
MAX
NESTED IFS
One of the alternatives within an IF
THENELSE statement
may involve further IFTHEN
ELSE statement
Example 6
Write an algorithm that reads three
numbers and prints the value of the
largest number.
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX N1 [N1>N2,
N1>N3]
else
MAX N3
[N3>N1>N2]
endif
else
if (N2>N3) then
MAX N2 [N2>N1,
N2>N3]
else
MAX N3
[N3>N2>N1]
endif
endif
Step 3: Print The largest number
is, MAX
Flowchart: Draw the flowchart of
the above Algorithm.
Example 7
Write and algorithm and draw a
flowchart to
a) read an employee name (NAME),
overtime hours worked (OVERTIME),
hours absent (ABSENT) and
b) Determine the bonus payment
(PAYMENT).
Bonus Schedule
OVERTIME
(2/3)*ABSENT
>40 hours
>30 but 40
hours
>20 but 30
hours
>10 but 20
hours
10 hours
Bonus Paid
$50
$40
$30
$20
$10
Control
Description
Provides a way to
Pointer
move and resize the
controls form
Displays
icons/bitmaps and
metafiles. It displays
PictureBox
text or acts as a
visual container for
other controls.
Used to display
TextBox
message and enter
text.
Serves as a visual
Frame
and functional
container for controls
Used to carry out the
CommandButton
specified action when
the user chooses it.
Displays a True/False
CheckBox
or Yes/No option.
OptionButton
OptionButton control
which is a part of an
option group allows
the user to select
only one option even
it displays mulitiple
99
God Bless Everyone!
ListBox
ComboBox
HScrollBar and
VScrollBar
Timer
DriveListBox
DirListBox
FileListBox
Shape
Line
Image
Data
choices.
Displays a list of
items from which a
user can select one.
Contains a TextBox
and a ListBox. This
allows the user to
select an ietm from
the dropdown
ListBox, or to type in
a selection in the
TextBox.
These controls allow
the user to select a
value within the
specified range of
values
Executes the timer
events at specified
intervals of time
Displays the valid
disk drives and
allows the user to
select one of them.
Allows the user to
select the directories
and paths, which are
displayed.
Displays a set of files
from which a user
can select the
desired one.
Used to add shape
(rectangle, square or
circle) to a Form
Used to draw straight
line to the Form
used to display
images such as
icons, bitmaps and
metafiles. But less
capability than the
PictureBox
Enables the use to
connect to an
existing database
and display
information from it.
OLE
Label
Used to link or
embed an object,
display and
manipulate data from
other windows based
applications.
Displays a text that
the user cannot
modify or interact
with.
Project Explorer
Docked on the right side of the screen,
just under the toolbar, is the Project
Explorer window. The Project Explorer
as shown in in figure serves as a quick
reference to the various elements of a
project
namely form, classes and modules. All
of the object that make up the
application are packed in a project. A
simple project will typically contain
one form, which is a window that is
designed as part of a program's
interface. It is possible to develop any
number of forms for use in a program,
although a program may consist of a
single form. In addition to forms, the
Project Explorer window also lists code
modules and classes.
Properties Window
The Properties Window is docked
under the Project Explorer window. The
Properties Window exposes the
various characteristics of selected
objects. Each and every form in an
application is considered an object.
Now, each object in Visual Basic has
characteristics such as color and size.
Other characteristics affect not just
99
God Bless Everyone!
10<=2
True
0
<>
Not Equal to
5<>4
True
Equal to
5=7
False
String
Use to store alphanumeric values. A
variable length string can store
approximately 4 billion characters
Date
Use to store date and time values. A
variable declared as date type can
store both date and time values and it
can store date values 01/01/0100 up
to 12/31/9999
Boolean
Boolean data types hold either a true
or false value. These are not stored as
numeric values and cannot be used as
such. Values are internally stored as -1
(True) and 0 (False) and any non-zero
value is considered as true.
Variant
Stores any type of data and is the
default Visual Basic data type. In
Visual Basic if we declare a variable
without any data type by default the
data type is assigned as default.
Integer
Long
Single
Operators
OR
AND
Description
Operation will be true if either
of the operands is true
Operation will be true only if
both the operands are true
Relational Operators
Logical Operators
Variables In Visual Basic 6
Variables are the memory locations
which are used to store values
temporarily. A defined naming
strategy has to be followed while
naming a variable. A variable name
must begin with an alphabet letter and
should not exceed 255 characters. It
must be unique within the same
scope. It should not contain any
special character like %, &, !, #, @ or
$.
Variable Declaration
There are many ways of declaring
variables in Visual Basic. Depending
on where the variables are declared
and how they are declared, we can
determine how they can be used by
our application. The different ways of
declaring variables in Visual Basic are
listed below.
Explicit Declaration
Declaring a variable tells Visual Basic
to reserve space in memory. It is not
must that a variable should be
declared before using it. Automatically
whenever Visual Basic encounters a
new variable, it assigns the default
variable type and value. This is called
implicit declaration. Though this
type of declaration is easier for the
user, to have more control over the
variables, it is advisable to declare
them explicitly. The variables are
declared with a Dim statement to
name the variable and its type. The As
type clause in the Dim statement
Operator
Description Example
s
Result
Add
5+5
10
Substract
10-5
Divide
25/5
Integer
Division
20\3
Multiply
5*4
20
Exponent
(power of)
3^3
27
Mod
Remainder
of division
20 Mod 6
&
String
"George"& "George
concatenatio
" "&"Bush" Bush"
n
allows to define the data type or
object type of the variable. This is
called explicit declaration.
Syntax
Dim variable [As Type]
For example,
Dim strName As String
Dim intCounter As Integer
Using Option Explicit statement
In Visual Basic, to prevent errors we
can declare a variable by adding the
following statement to the general
declaration section of the Form.
Option Explicit
This forces the user to declare all the
variables. The Option Explicit
statement checks in the module for
usage of any undeclared variables and
reports an error to the user. The user
can thus rectify the error on seeing
this error message.
Scope of variables
A variable is scoped to a procedurelevel (local) or module-level
variable depending on how it is
declared. The scope of a variable,
procedure or object determines which
part of the code in our application are
aware of the variable's existence. A
variable is declared in general
declaration section of a Form, and
99
God Bless Everyone!
99
God Bless Everyone!