Date post: | 14-Apr-2018 |
Category: |
Documents |
Upload: | louie-de-la-torre |
View: | 213 times |
Download: | 0 times |
of 59
7/27/2019 Algorithms_and_Flowcharting.pdf
1/59
AlgorithmandFlowcharting
7/27/2019 Algorithms_and_Flowcharting.pdf
2/59
LessonObjectives
Attheendofthelecturethestudentsshouldbe
ableto:
Definetermsinprogrammingsuchas:system,SDLC,
flowcharting,algorithm,compile,syntaxerrors,etc.
Knowthestepsinprogramming.
Knowthedifferentsymbolsforflowchartingandhow
touse
them.
7/27/2019 Algorithms_and_Flowcharting.pdf
3/59
Whatissystem?
Isanorganizedsetofrelatedcomponents
establishedto
accomplish
acertain
task.
Note:Therearenaturalsystemssuchasthe
bodys
cardiovascular
system
but
many
systems
havebeenplannedanddeliberatelyputintoplace
bypeople.
7/27/2019 Algorithms_and_Flowcharting.pdf
4/59
Whatissystem?
Forexample:afastfoodfranchisehasasystemof
servingacustomer
including
taking
an
order,
assemblingthefoodandcollectingtheamount
due.
7/27/2019 Algorithms_and_Flowcharting.pdf
5/59
Phases of SDLC
System Development Life CyclePreliminary Investigation: Determining the
problem
Analysis: Understanding the existing systemDesign: Planning the new system
Development: Doing the work to bring the newsystem into being
Implementation: Converting the new system
7/27/2019 Algorithms_and_Flowcharting.pdf
6/59
SDLC
Analysis-gather data-analyze data
-develop requirements
Implementation-training
-conversion-evaluation
-maintenance
Preliminary Investigation-problem definition
Design-preliminary design
-detail design
Development-scheduling
-programming-testing
7/27/2019 Algorithms_and_Flowcharting.pdf
7/59
ComputerProgram
Isasetofdetailed,stepbystepinstructionsthatdirects
the
computer
what
you
want
it
to
do.
It
is
a
notation
for
expressinginstructionstobecarriedoutbyacomputer.
Programsarewritteninaprogramminglanguage,which
isaset
of
rules
that
provides
away
of
telling
the
computerwhatoperationstoperform.
Thepersonwhocreatescomputerprogramiscalled
computerprogrammer.
7/27/2019 Algorithms_and_Flowcharting.pdf
8/59
Activities Involve in
Programming
Understand the problem.
Plan the logic.
Code the program.
Compile the program into machine
language.
Test the program.
Put the program into production.
7/27/2019 Algorithms_and_Flowcharting.pdf
9/59
Activities involve in
Programming
Understanding the Problem
Before writing the program the programmer must first
understand the problem.
Satisfy the users need
One of the most difficult task since description of what
is needed may be vague, or worse yet, the user may
not even know what he/she wants.
A good programmer is part counselor, and part
detective.
7/27/2019 Algorithms_and_Flowcharting.pdf
10/59
Activities Involve in
Programming
Plan the Logic
The process of planning for the solution of theproblem.
They are referred to as ALGORITHMS
Two most common tools are FLOWCHARTS andPSEUDOCODE.
Involves writing down steps (in English).
The programmer doesnt worry about syntax at this
point, just the sequence of events that will lead from
available input to the desired output.
7/27/2019 Algorithms_and_Flowcharting.pdf
11/59
Activities Involve in
Programming
Code the Program
There are over 400 programming languages that
exist.
Most programming languages have translatorsalso known as compiler and interpreters.
Pascal, C, C++, Visual Basic, Cobol, etc.
The part in which the programmer worry about thecorrect syntax; command, punctuation, spelling, etc.
The actual construction of the source code.
7/27/2019 Algorithms_and_Flowcharting.pdf
12/59
Activities involve in
Programming
Compile the program into Machine Language
Machine language - the only language a machine
knows.
Its consists of 0s and 1s.
Compiler is a special program that translates high-level
language instructions into machine language. Compiler catches all syntax errors.
7/27/2019 Algorithms_and_Flowcharting.pdf
13/59
Activities involve in
Programming
Test the Program
To execute and then run the program. The program uponexecution is being subjected to different kinds of test data
to anticipate and correct errors that might occur beforereleasing the program to the users.
Errors can be classified into two: (1) syntactical or logicalerrors (2) compile or run-time errors.
7/27/2019 Algorithms_and_Flowcharting.pdf
14/59
Activities involve in
Programming
Put the Program into Production
The organization can now use the program.
Training personnel to use the program.
Running the program using the actual data.
Test for users approval.
7/27/2019 Algorithms_and_Flowcharting.pdf
15/59
Algorithm
A sequence of well-understood steps that one takes to
do something. A set of instructions for a person to follow in order to
accomplish a certain task.
A sequence of instructions that tell how to solve aparticular problem.
It is a finite set of instructions that specify a sequence ofoperations to be carried out in order to solve a specificproblem or class of problems.
Examples: directions for going to a certain place
baking a chocolate cake
computing income tax searching for a name in a telephone directory
7/27/2019 Algorithms_and_Flowcharting.pdf
16/59
Example:
Prepare a Cup of Coffee Step 1: Fill kettle with water.
Step 2: Turn on stove. Step 3: Put kettle on stove.
Step 4: If water is boiling proceed to step#5 otherwise wait.
Step 5: Turn off stove
Step 6: Fill mug with boiled water
Step 7: Put 1 tsp. of coffee in mug.
Step 8: With sugar, put 2 tsp. of sugar in mug, otherwise proceedto step#9.
Step 9: With milk, pour 1 tbsp. of milk in mug, otherwise proceed tostep#10.
Step 10: Stir contents of mug
7/27/2019 Algorithms_and_Flowcharting.pdf
17/59
Characteristics of an Algorithm
Specify each step or instruction exactly.
There must be no ambiguity.
The instructions must be clear.
There must be a finite number of steps.
The algorithm must terminate.
There must be a stopping point.
There must be an output.
The algorithm must produce the correct result.
7/27/2019 Algorithms_and_Flowcharting.pdf
18/59
Flowcharting
What is Flowcharting?
A graphical representation of the logical steps that solves a
problem. Programmers use flowcharts to understand the logic
processing of data.
To visualize how statements in a program are interrelated.
Although time consuming, its the best tool for beginners.
Use of symbol and phrases to designate the logic of how aproblem solved.
A common method for defining the logical steps of flow within aprogram by using a series of symbols to identify the basicinput, process and output (IPOs) function within a program.
A diagram representing the logical sequence in which a
combination of steps or operation is to be performed. It is ablueprint of the program.
7/27/2019 Algorithms_and_Flowcharting.pdf
19/59
Flowcharting
Types of Flowcharts
Program FlowchartsDepicts the detailed logical flow of process in a program.
System Flowcharts
Shows the logic flow of data from one source to another.Divided into functions of departments or personnel.
Symbols are used to represent documents used andoperations undertaken.
Highlights excessive information flow, duplication ofinformation stored in different locations and reasons for
delay of transactions.
7/27/2019 Algorithms_and_Flowcharting.pdf
20/59
BasicFlowcharting
Symbols
Terminal block
Input/Output block
Process symbol
Decision symbol
On-page Connector
Off-page Connector
Initialization symbol
Flow lines
Flowcharting
7/27/2019 Algorithms_and_Flowcharting.pdf
21/59
BasicFlowcharting
Symbols
Terminal
Terminal
usedtosignifythebeginningand
endofflowchart
Flowcharting
OVALOVAL
STARTSTART
7/27/2019 Algorithms_and_Flowcharting.pdf
22/59
BasicFlowcharting
Symbols
Preparation/Initialization
signifiesthepreparationofdata
Used
to
select
initial
conditions Usedtorepresentinstructionsor
groupofinstructionsthatwillalter
or
modify
a
programs
course
of
execution
Flowcharting
HexagonHexagon
SUM=0SUM=0PRODUCT=0PRODUCT=0
7/27/2019 Algorithms_and_Flowcharting.pdf
23/59
BasicFlowcharting
Symbols
Input/Output
showsinputandoutput.Data
areto
be
read
into
the
computermemoryfroman
inputdeviceordataaretobe
passedfrom
the
memory
to
an
outputdevice.
Flowcharting
ParallelogramParallelogram
Read ARead A
Print APrint A
7/27/2019 Algorithms_and_Flowcharting.pdf
24/59
BasicFlowcharting
Symbols
Process/Processing
performsanycalculationshat
areto
be
done.
Flowcharting
RectangleRectangle
Sum = A+BSum = A+B
7/27/2019 Algorithms_and_Flowcharting.pdf
25/59
BasicFlowcharting
Symbols
Decision
signifiesanydecisionsthataretobe
done.Twoalternativeexecution
pathsarepossible. Thepathtobe
followedisselectedduringthe
executionbytestingwhetherornot
thecondition
specified
within
the
outlineisfulfilled.
Flowcharting
DiamondDiamond
If x>5If x>5T F
7/27/2019 Algorithms_and_Flowcharting.pdf
26/59
BasicFlowcharting
Symbols
OnpageConnector
showtheentryorexitpointoftheflowchart.Anonprocessingsymbolusedtoconnectonepartofaflowchart
to
another
without
drawingflowlines.Conservesspacebykeepingrelatedblocksnearoneanother,reducesthenumberofflowlines
in
complex
program
and
eliminatescrosslinesfromtakingplace.
Flowcharting
AA
Small CircleSmall Circle
7/27/2019 Algorithms_and_Flowcharting.pdf
27/59
BasicFlowcharting
Symbols
OffpageConnector
designatesentryortoexitfrom
onepage
when
aflowchart
requiresmorethanonepage.
Flowcharting
PentagonPentagon
BB
Fl h ti
7/27/2019 Algorithms_and_Flowcharting.pdf
28/59
BasicFlowcharting
Symbols
Flowlines
signifiestheprocessthatisto
beexecuted
next
Flowcharting
Fl h ti
7/27/2019 Algorithms_and_Flowcharting.pdf
29/59
Basic Flowcharting
Almost every program involves the steps of input,
processing and output.
Example:
Get number
Answer = number *2Print Answer
Most flowcharts need some way to separate each step.
Drawing different boxes around each statement is thestandard convention.
Input operation are represented in a parallelogram. A
statement in English is written inside the parallelogram.
Flowcharting
Fl h ti
7/27/2019 Algorithms_and_Flowcharting.pdf
30/59
Answer = number * 2
Getnumber
Arithmetic statements are
examples of processing
statements, are placed inside
the process box.
Output statements use the
same symbol as that of input
statements - parallelogram.
To show the correct sequenceof events arrow, flow lines and
the terminal symbol (for begin
and end) are used.
begin
Flowcharting
7/27/2019 Algorithms_and_Flowcharting.pdf
31/59
begin
Get number
Answer= number * 2
Print Answer
end
The next step the programmer
shall do is to find the necessary
language in which to code the
flowchart.
Fl h ti
7/27/2019 Algorithms_and_Flowcharting.pdf
32/59
What are Variables?
Memory allocations, the content of which may vary/change.
The capability of memory variables to change its content
makes the computers and programming worthwhile.
Because one memory location can be used over and overagain with different values, program instructions can be
written once and then used for thousands of problems.
Giving names (for identification) may vary from oneprogramming language to another; some may allow
hyphens; others allows underscores; some languages allow
dollar signs, others dont. There are languages that limits the
length of names to a variable.
Flowcharting
Fl h ti
7/27/2019 Algorithms_and_Flowcharting.pdf
33/59
Rules in naming variables...
Must be one word.
May contain letters, numbers, hyphens,
underscores.
Must not start with a number.
Must not contain a space.
Must have some meaning.
Invalid Variable names
interest rate - should be one word
rate! - contains ! an invalid character
xyxxx - although this is allowed, it doesnt mean
or imply anything that it may hold.
#total - should not start with a special character.
Flowcharting
Flowcharting
7/27/2019 Algorithms_and_Flowcharting.pdf
34/59
Types of Variables Numeric Variables
The type of variable that can hold a number.
In this example: Answer = number * 2 Some languages allow additional types of data; Pascal and C
distinguish between integer or whole number numeric variables and
floating point, decimal and fractional numeric variables.
Character, Text or String Variables
Variables that can hold letters of the alphabet and other special
character such as punctuation marks.
Example:NAME = WASHINGTON
Logical or Boolean Variables
A special type of variable that can only have two values - TRUE or
FALSE.
Flowcharting
Flowcharting
7/27/2019 Algorithms_and_Flowcharting.pdf
35/59
Arithmetic Operators and Mathematical Statements Addition (+)
Subtraction (-)
Multiplication (*) Division (/)
Modulus (%)
A mathematical statement is composed of a left variable, an
assignment operator (usually in the form of an equal sign) a
series of operands(may be a set of the same variables or a new
set of variables, and /or constants) and (arithmetic) operators.
Example:
x = x + 1
sum = a + b
Flowcharting
Flowcharting
7/27/2019 Algorithms_and_Flowcharting.pdf
36/59
Constants
A constant is a value that never changes during the
execution of the program. Constants can be any type-numeric, alphabetic or
special symbols.
Like variable, constants can be named. The computersets up a specific memory location to hold the value of
the constant.
During the execution of the program, the constant isreferred to by name. Once the constant is given a
value, it cannot be changed during the execution of
the program.
Flowcharting
Flowcharting
7/27/2019 Algorithms_and_Flowcharting.pdf
37/59
Relational Operator A relational operator compares two values and
determines the relationship between them.
Operator Use Returns true if
> op1 > op2 op1 is greater than op2
>= op1 >= op2 op1 is greater than or equal to op2
< op1 < op2 op1 is less than op2
7/27/2019 Algorithms_and_Flowcharting.pdf
38/59
Logical Operators
Use logical operators (also called Boolean operators) to
express relationships between search expressions.
Operator Use Returns true if
and cond1 and cond2 both condition should be true
or cond1 or cond2 either one of the condition is true
not (negation) not cond
! cond
Flowcharting
ADVANTAGES OF USING
7/27/2019 Algorithms_and_Flowcharting.pdf
39/59
ADVANTAGES OF USING
FLOWCHARTS
1. Communication: Flowcharts are better way of communicating the logic
of a system to all concerned.
2. Effective analysis: With the help of flowchart, problem can be analyzed
in more effective way.
3. Proper documentation: Program flowcharts serve as a good programdocumentation, which is needed for various purposes.
4. Efficient Coding: The flowcharts act as a guide or blueprint during the
systems analysis and program development phase.
5. Proper Debugging: The flowchart helps in debugging process.
6. Efficient Program Maintenance: The maintenance of operating program
becomes easy with the help of flowchart. It helps the programmer to put
efforts more efficiently on that part
LIMITATIONS OF USING
7/27/2019 Algorithms_and_Flowcharting.pdf
40/59
LIMITATIONS OF USING
FLOWCHARTS
1. Complex logic: Sometimes, the program logic is quitecomplicated. In that case, flowchart becomes complex andclumsy.
2. Alterations and Modifications: If alterations are requiredthe flowchart may require re-drawing completely.
3. Reproduction:As the flowchart symbols cannot be typed,
reproduction of flowchart becomes a problem.
4. The essentials of what is done can easily be lost in the
technical details of how it is done.
L i St t
7/27/2019 Algorithms_and_Flowcharting.pdf
41/59
Simple Sequence
Logic involves executing instructions one
statement after another-in the order presented by
the program.
This is the simplest and most-used pattern.
The computer assumes that all instructions are
to be executed in this order unless the programpresents other instructions.
Logic Structures
Logic Str ct res
7/27/2019 Algorithms_and_Flowcharting.pdf
42/59
A simple sequence pattern
The process is done onestep at a time
Logic Structures
Logic Structures
7/27/2019 Algorithms_and_Flowcharting.pdf
43/59
Selection Structure
Requires that the computer make a choice among
two or more items.
Each choice is based on one of three comparisons acomputer can make: equal to, less than or greater
than.
Logic Structures
Logic Structures
7/27/2019 Algorithms_and_Flowcharting.pdf
44/59
Selection pattern
A decision is
made orchoosingbetween a set of
options
Logic Structures
Logic Structures
7/27/2019 Algorithms_and_Flowcharting.pdf
45/59
Iteration Structure
causes an interruption in the normal sequence of
processing and directs that computer to loop back toa previous statement in the program, repeating the
same sequence over again, usually with new data.
By looping the programmer avoids having to repeatthe same set of instructions over and over.
Logic Structures
Logic Structures
7/27/2019 Algorithms_and_Flowcharting.pdf
46/59
Iteration pattern
A set of repeated
statements areexecuted until thedesired output is
acquired.
Logic Structures
Logic Structures
7/27/2019 Algorithms_and_Flowcharting.pdf
47/59
Linking pattern
An on-pageconnector is used to
redirect the flow ofthe chart.
Logic Structures
Logic Structures
7/27/2019 Algorithms_and_Flowcharting.pdf
48/59
Iteration structureexample...
Example 1
Draw a flowchart to find the
sum of first 50 natural
numbers.
Logic Structures
Logic Structures
7/27/2019 Algorithms_and_Flowcharting.pdf
49/59
Example 2Draw a flowchart
to find the
largest of three
numbers A,B,and C.
Selection structure
example...
Logic Structures
7/27/2019 Algorithms_and_Flowcharting.pdf
50/59
The CASE structure
Drawaflowchartthatwillacceptanddisplayanumber. WriteiDrawaflowchartthatwillacceptanddisplayanumber. Writeitsequivalenttsequivalent
algorithmsalgorithms
7/27/2019 Algorithms_and_Flowcharting.pdf
51/59
algorithms.algorithms.
Algorithm:
Step1:ReadinthevalueofN.
Step2:PrintthevalueofN.
START
READ N
PRINT N
END
7/27/2019 Algorithms_and_Flowcharting.pdf
52/59
FlowchartSamples
DrawaflowchartthatwillcomputeanddisplaythesumandproduDrawaflowchartthatwillcomputeanddisplaythesumandproductoftwoctoftwo
7/27/2019 Algorithms_and_Flowcharting.pdf
53/59
numbers. Writeitsequivalentalgorithm.numbers. Writeitsequivalentalgorithm.
Algorithm:
Step1:
Initialize
Sum
and
Product
into
0.
Step2:ReadinthevaluesofAandB.
Step3:ComputethesumbyaddingAandBthencomputetheproductby
multiplyingA
and
B.
Step4:PrintthecomputedvalueofSumandProduct.
START
Read A,B
Print Sum,
ProductEND
Sum = 0Product = 0
Sum = A + B
Product = A * B
ConstructaflowchartthatwillconvertaninputtednumberinFaConstructaflowchartthatwillconvertaninputtednumberinFahrenheittoitsequivalenthrenheittoitsequivalent
measure in Celsiusmeasure in Celsius
7/27/2019 Algorithms_and_Flowcharting.pdf
54/59
measureinCelsius.measureinCelsius.
Formula:C=(5/9)*(FFormula:C=(5/9)*(F32)32)
Algorithm:
Step1:
Initialize
Celsius
into
0.
Step2:ReadinthevalueofFahrenheit.
Step3:ComputethevalueofCelsius.
Step4:
the
computed
value
of
Celsius.
START
Read Fah
CelsiusEND
Celsius=0
Celsius=(5/9)*(Fah-32)
DrawaflowchartthatwillinputvaluesforAandB. ComparetwDrawaflowchartthatwillinputvaluesforAandB. Comparetwovaluesinputtedandprintovaluesinputtedandprint
h h f h l h h l d h k h l l h
7/27/2019 Algorithms_and_Flowcharting.pdf
55/59
whichofthevaluesishigherincludingtheremarkwhichofthevaluesishigherincludingtheremarkHigherHigher. Writeitsequivalentalgorithm.. Writeitsequivalentalgorithm.
Algorithm:
Step1:ReadthevaluesofAandB.
Step2:TestifAisgreaterthanB.
Step3:IfAisgreaterthanB,Aishigher. However,ifAislessthanB,Bishigher.
Step4:PrintthenumberandtheremarkHigher
START
Input A,B
Print A,
Higher
END
A > B
Print B,Higher
T
F
DrawaflowchartthatwillinputgradeofstudentanddetermineDrawaflowchartthatwillinputgradeofstudentanddeterminewhetherthegradeispassedorwhetherthegradeispassedor
f il d P i h d d k f d W i if il d P i h d d k f d W i i i l l i hi l l i h
7/27/2019 Algorithms_and_Flowcharting.pdf
56/59
failed. Printthename,gradeandremarksofstudent. Writeitfailed. Printthename,gradeandremarksofstudent. Writeitsequivalentalgorithm.sequivalentalgorithm.
Algorithm:
Step1:Initializenameandremarksintoblanks.
Step2:ReadinvaluesforGradeandName.
Step3:
Test
if
Grade
is
greater
than
or
equal
to
60.
Step4:IfGradeisgreaterthanorequalto60,remarkisPassed. However,ifGradeisbelow60,remarkisFailed
Step5:Printthename,gradeandremarks.
START
Read Name,Grade
END
If Grade>60
Print Name,Grade, Remarks
Name= Remarks =
Remarks =Failed
Remarks =Passed
SEATWORKSEATWORK
7/27/2019 Algorithms_and_Flowcharting.pdf
57/59
SEATWORKSEATWORK
DEIManufacturingCompanyplanstogiveayearDEIManufacturingCompanyplanstogiveayearendbonusendbonus
toeachofitsemployee. Drawaflowchartwhichwilltoeachofitsemployee. Drawaflowchartwhichwillcomputethebonusofanemployee. Considerthefollowingcomputethebonusofanemployee. Considerthefollowing
conditions:Iftheemployeeconditions:Iftheemployeesmonthlysalaryislessthan2,000smonthlysalaryislessthan2,000
pesos,the
bonus
is
50%
of
the
salary;
for
employees
with
pesos,
the
bonus
is
50%
of
the
salary;
for
employees
with
salariesgreaterthan2,000pesos,thebonusis1,500pesos.salariesgreaterthan2,000pesos,thebonusis1,500pesos.
PrintthenameandthecorrespondingbonusforeachPrintthenameandthecorrespondingbonusforeach
employee.Write
each
equivalent
algorithms.employee.
Write
each
equivalent
algorithms.
Pseudocode
7/27/2019 Algorithms_and_Flowcharting.pdf
58/59
Pseudocode
uses everyday language to prepare a brief set of instructions
in the order in which they will appear in the finished program. It is an abbreviated version of actual computer code (that's
why it is called pseudocode).
Pseudocode looks more like a real program than the flowchartdoes. It allows the programmer to focus on the steps required
to solve a program rather than on how to use the computer
language. An advantage of pseudocode is that it can be used to
describe a program to a nontechnical user and can still
provide guidelines for the writing of program code.
Pseudocode
7/27/2019 Algorithms_and_Flowcharting.pdf
59/59
Pseudocode
A textual presentation of a flowchart
Close to a natural language
The control structures impose the logic May become a part of the program documentation
Could be translated into a program