45
Lecture in  Algorithm and Flowcharting ITEC113 Introduction to Programming Languages

Algorithm and Flow Charting

Embed Size (px)

Citation preview

Page 1: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 1/45

Lecture in

 Algorithm and Flowcharting

ITEC113

Introduction to Programming Languages

Page 2: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 2/45

What is system?

Is an organized set of related componentsestablished to accomplish a certain task.

Note: There are natural systems such asthe bodys cardiovascular system but manysystems have been planned and

deliberately put into place by people.

Page 3: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 3/45

What is system?

For example: a fast-food franchise has asystem of serving a customer includingtaking an order, assembling the food andcollecting the amount due.

Page 4: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 4/45

Page 5: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 5/45

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

Page 6: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 6/45

Computer Program

Is a set of detailed, step-by-step instructions that directs the computer what you want it to do. It is

a notation for expressing instructions to becarried out by a computer.

Programs are written in a programming language,which is a set of rules that provides a way of 

telling the computer what operations to perform. The person who creates computer program is

called computer programmer.

Page 7: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 7/45

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.

Page 8: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 8/45

Activities involve in

Programming

Understanding the Problem

Before writing the program the programmer must firstunderstand the problem.

Satisfy the user¶s 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.

Page 9: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 9/45

Activities Involve in

Programming

Plan the Logic

The process of planning for the solution of the

problem.

They are referred to as ³ALGORITHMS´

Two most common tools are FLOWCHARTS and

PSEUDOCODE.

Involves writing down steps (in English).

The programmer doesn¶t worry about syntax at thispoint, just the sequence of events that will lead from

available input to the desired output.

Page 10: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 10/45

Activities Involve in

Programming

Code the Program

There are over 400 programming languages that

exist.

Most programming languages have translators

also 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.

Page 11: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 11/45

Activities involve in

Programming

Compile the program into Machine Language

Machine language - the only language a machineknows.

Its consists of 0¶s and 1¶s.

Compiler is a special program that translates high-level

language instructions into machine language.

Compiler catches all syntax errors.

Page 12: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 12/45

Activities involve in

Programming

Test the Program

To execute and then run the program. The program

upon execution is being subjected to different kinds

of test data to anticipate and correct errors that might

occur before releasing the program to the users.

Errors can be classified into two: (1) syntactical or logical errors (2) compile or run-time errors.

Page 13: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 13/45

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 user¶s approval.

Page 14: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 14/45

Algor ithm

 A sequence of well-understood steps that one takes todo 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 a

particular problem.

It is a finite set of instructions that specify a sequence of operations 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

Page 15: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 15/45

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

Page 16: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 16/45

Character istics of an Algor ithm

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.

Page 17: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 17/45

Flowchar ting

What is Flowcharting?

 A graphical representation of the logical steps that solves aproblem.

Programmers use flowcharts to understand the logic

processing of data. To visualize how statements in a program are interrelated.

 Although time consuming, it¶s 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 (IPO¶s) function within a program.

 A diagram representing the logical sequence in which acombination of steps or operation is to be performed. It is ablueprint of the program.

Page 18: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 18/45

Flowchar ting

Types of Flowcharts

Program Flowcharts

Depicts 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 and

operations undertaken.

Highlights excessive information flow, duplication of 

information stored in different locations and reasons for 

delay of transactions.

Page 19: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 19/45

Basic Flowcharting Symbols

Terminal block

Input/Output block

Process symbol

Decision symbol

On-page Connector 

Off -page Connector 

Initialization symbol

Flow lines

Flowchar ting

Page 20: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 20/45

Basic Flowcharting Symbols

TerminalTerminal

- used to signify the beginningand end of flowchart 

Flowchar ting

OV ALOV AL

STARTSTART

Page 21: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 21/45

Basic Flowcharting Symbols

Preparation/Initialization

- signifies the preparation of data- Used to select initial conditions

- Used to represent instructions orgroup of instructions that willalter or modify a programscourse of execution

Flowchar ting

HexagonHexagon

SUM=0SUM=0PRODUCT=0PRODUCT=0

Page 22: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 22/45

Basic Flowcharting Symbols

Input/Output

-shows input and output.Data are to be read into thecomputer memory from aninput device or data are to

be passed from the memoryto an output device.

Flowchar ting

ParallelogramParallelogram

Read ARead APrint APrint A

Page 23: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 23/45

Basic Flowcharting Symbols

Process/Processing

-performs any calculations hat are to be done.

Flowchar ting

RectangleRectangle

Sum = A+BSum = A+B

Page 24: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 24/45

Basic Flowcharting Symbols

Decision

-signifies any decisions that are tobe done. Two alternativeexecution paths are possible.The path to be followed isselected during the execution by

testing whether or not thecondition specified within theoutline is fulfilled.

Flowchar ting

DiamondDiamond

If x>5If x>5T F

Page 25: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 25/45

Basic Flowcharting Symbols

On-page Connector

-show the entry or exit point of theflowchart. A non-processingsymbol used to connect one part of a flowchart to another without drawing flowlines. Conservesspace by keeping related blocksnear one another, reduces the

number of flowlines in complexprogram and eliminates crosslines from taking place.

Flowchar ting

 A A

Small CircleSmall Circle

Page 26: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 26/45

Basic Flowcharting Symbols

Off-page Connector

-designates entry or to exit from one page when aflowchart requires morethan one page.

Flowchar ting

PentagonPentagon

BB

Page 27: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 27/45

Basic Flowcharting Symbols

Flowlines

-signifies the process that isto be executed next 

Flowchar ting

Page 28: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 28/45

Basic Flowcharting

 Almost every program involves the steps of input,

processing and output.

Example:

Get number 

 Answer = number *2

Print 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.

Flowchar ting

Page 29: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 29/45

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 over 

again with different values, program instructions can be

written once and then used for thousands of problems.

Giving names (for identification) may vary from one

programming language to another; some may allow

hyphens; others allows underscores; some languages allow

dollar signs, others don¶t. There are languages that limits the

length of names to a variable.

Flowchar ting

Page 30: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 30/45

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 doesn¶t mean

or imply anything that it may hold.

#total - should not start with a special character.

Flowchar ting

Page 31: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 31/45

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 andfloating 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.

Flowchar ting

Page 32: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 32/45

Arithmetic Operators and Mathematical Statements

Addition (+)

Subtraction (-)

Multiplication (*)

Division (/)

Modulus (%)

 A mat hematical 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

Flowchar ting

Page 33: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 33/45

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 computer 

sets 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.

Flowchar ting

Page 34: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 34/45

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

<= op1 <= op2 op1 is less than or equal to op2

== op1 == op2 op1 and op2 are equal

!= / not= op1 != op2 op1 and op2 are not equal

op1 not= op2

Flowchar ting

Page 35: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 35/45

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

Flowchar ting

Page 36: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 36/45

ADVANTAGES OF USING 

FLOWCH

ARTS

1. Communication: Flowcharts are better way of communicating the logicof 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 program

documentation, 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

Page 37: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 37/45

LIMITATIONS OF USING 

FLOWCH

ARTS

1.Complex logic: Sometimes, the program logic is quitecomplicated. In that case, flowchart becomes complex andclumsy.

2. Alterations and Modifications: If alterations are required

the 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.

Page 38: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 38/45

Draw a flowchart that will accept and display a number. WriteDraw a flowchart that will accept and display a number. Write

its equivalent algorithms.

its equivalent algorithms.

 Algorithm:

Step 1: Read in the value of N.

Step 2: Print the value of N.

START

READ N

PRINT N

END

Page 39: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 39/45

Page 40: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 40/45

Construct a flowchart that will convert an inputted number inConstruct a flowchart that will convert an inputted number inFahrenheit to its equivalent measure in Celsius. Fahrenheit to its equivalent measure in Celsius. 

Formula: C = (5/9)*(FFormula: C = (5/9)*(F--32)32)

 Algorithm:

Step 1: Initialize Celsius into 0.

Step 2: Read in the value ofFahrenheit.

Step 3: Compute the value ofCelsius.

Step 4: Print the computed value ofCelsius.

START

Read Fah

PrintCelsius

END

Celsius=0

Celsius=(5/9)*(Fah-32)

Page 41: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 41/45

Draw a flowchart that will input values for A and B. Compare twoDraw a flowchart that will input values for A and B. Compare twovalues inputted and print which of the values is higher including thevalues inputted and print which of the values is higher including the

remark Higher. Write its equivalent algorithm.remark Higher. Write its equivalent algorithm.

 Algorithm:Step 1: Read the values of A and B.Step 2: Test if A is greater than B.Step 3: If A is greater than B, A is higher. 

However, if A is less than B, B is higher.Step 4: Print the number and the remark

 Higher

START

Input A,B

Print A,

 Higher

END

 A > B

Print B, Higher

T

F

Page 42: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 42/45

Draw a flowchart that will input grade of student and determineDraw a flowchart that will input grade of student and determinewhether the grade is passed or failed. Print the name, grade andwhether the grade is passed or failed. Print the name, grade and

remarks of student. Write its equivalent algorithm.remarks of student. Write its equivalent algorithm.

 Algorithm:Step 1: Initialize name and remarks into blanks.Step 2: Read in values for Grade and Name.Step 3: Test if Grade is greater than or equal to 60.Step 4: If Grade is greater than or equal to 60,

remark is Passed.  However, if Grade is below

60, remark is Failed.Step 5: Print the name, grade and remarks.

START

Read Name,Grade

END

If Grade>60

Print Name,Grade, Remarks

Name=

Remarks =

Remarks = Failed

Remarks = Passed

Page 43: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 43/45

SEATWO

RKSEATWO

RK

DEI Manufacturing Company plans to give a yearDEI Manufacturing Company plans to give a year--end bonus to each of its employee. Draw aend bonus to each of its employee. Draw a

flowchart which will compute the bonus of anflowchart which will compute the bonus of anemployee. Consider the following conditions: Ifemployee. Consider the following conditions: Ifthe employees monthly salary is less than 2,000the employees monthly salary is less than 2,000pesos, the bonus is 50% of the salary; forpesos, the bonus is 50% of the salary; foremployees with salaries greater than 2,000 pesos,employees with salaries greater than 2,000 pesos,

the bonus is 1,500 pesos. Print the name and thethe bonus is 1,500 pesos. Print the name and thecorresponding bonus for each employee. Writecorresponding bonus for each employee. Writeeach equivalent algorithms.each equivalent algorithms.

Page 44: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 44/45

Page 45: Algorithm and Flow Charting

8/6/2019 Algorithm and Flow Charting

http://slidepdf.com/reader/full/algorithm-and-flow-charting 45/45

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

Mod2