Sie sind auf Seite 1von 57

Algorithm and Flowcharting

Learning Outcomes

At the end of the lecture the students


should be able to:

Define terms in programming such as:


program, flowcharting, algorithm,
compile, syntax errors, etc.
Know the steps in programming.
Know the different symbols for
flowcharting and how to use them.

What is ?
Program
Programming Language
Programmer

Computer Program

A set of detailed, step-by-step instructions


that directs the computer what you want it
to do.
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.

Steps 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

Steps in Programming
1. Understand 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.

Steps in Programming
2. Plan the Logic
The process of planning for the solution of the problem.
They are referred to as ALGORITHMS
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.

Steps in Programming
3. 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 the
correct syntax; command, punctuation, spelling, etc.
The actual construction of the source code.

Steps in Programming
4. Compile the program into Machine Language

Machine language - the only language a computer


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.

Steps in Programming
5. 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.

Steps in Programming
6. 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.

Algorithm
A sequence of well-understood steps for a
person to follow in order to accomplish a
certain task or 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 specific problem or class
of problems.

Algorithm

Examples:
directions for going to a certain place
baking a chocolate cake
computing income tax
searching for a name in a telephone
directory

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 proceed
to step#9.
Step 9: With milk, pour 1 tbsp. of milk in mug, otherwise proceed to
step#10.
Step 10: Stir contents of mug

Characteristics of an Algorithm

Specify each step or instruction exactly.


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.

Common Tools of Algorithm


FLOWCHARTS
PSEUDOCODE

Flowcharts

A graphical representation of the logical


steps that solves a problem.
A diagram representing the logical sequence
in which a combination of steps or operation
is to be performed. It is a blueprint of the
program.
Programmers use flowcharts to understand
the logic processing of data.
Although time consuming, its the best tool
for beginners.

Basic Flowcharting
Symbols
Terminal block
Process symbol

Input/Output block

Decision symbol

On-page Connector
Off-page Connector

Initialization symbol

Flow lines

Basic Flowcharting Symbols


Terminal
- used to signify the
beginning and end of
flowchart

OVAL

START

Basic Flowcharting Symbols

Hexagon

SUM
PRODUCT
NAME

Initialization

- signifies the initialization or


preparation of data.
used to represent
instructions or group of
instructions that will alter or
modify a programs course of
execution.

Basic Flowcharting Symbols

Parallelogram

Read A
Print A

Input/Output

- shows input and output.


Data are to be read into
the computer memory
from an input device or
data are to be passed
from the memory to an
output device.

Basic Flowcharting Symbols

Rectangle

Sum = A+B

Process

- performs any
calculations that are to
be done.

Basic Flowcharting Symbols

Diamond

If x>5

Decision

- signifies any decisions that are


to be done. Two alternative
execution paths are possible.
The path to be followed is
selected during the execution
by testing whether or not the
condition specified within the
outline is fulfilled.

Basic Flowcharting Symbols

Small Circle

On-page Connector

- show the entry or exit


point of the flowchart. A
non-processing symbol
used to connect one part
of a flowchart to another
without drawing a
flowlines.

Basic Flowcharting Symbols

Pentagon

Off-page Connector

- Sometimes a flowchart
will not fit on one page.
- Designates entry or to
exit from one page when
a flowchart requires
more than one page.

Basic Flowcharting Symbols

Flowlines

- signifies the process that


is to be executed next

Variable and Constant

- A variable is a temporary container to store


-

information, it is a named location in computer


memory where varying data like numbers and
characters can be stored and manipulated
during the execution of the program.
A constant is a value that never changes
during the execution of the program.

Rules in Naming Variables

Names may contain letters, numbers, and


underscores.
Names must start with a letter.
Names cannot contain spaces.
Names cannot contain special characters .

Valid or Invalid Variables

First Name

LastName

sUm

FirstNum.

Second_Num

PaYr00L2015

Salary!

2COMP

Comp_2

Operators Used in
Flowcharting
Arithmetic Operators are operators used to

perform mathematical operations.


Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus (%)

Operators in Flowcharting

Relational Operators an operators that


compares two values and determines the
relationship between them.

Operator
>

op1 > op2

>=
<

Use

Returns true if
op1 is greater than op2

op1 >= op2

op1 < op2

op1 is greater than or equal to 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

Four Flowchart
Structures

Four Flowchart
Structures
Sequence
Decision
Repetition
Case

Sequence Structure

Directs the computer to


process the program
instructions, one after
another, in the order listed
in the program.
Series of actions are
performed in sequence

Decision Structure
Makes a decision and then takes
an appropriate action based on
that decision
Example: waiting or crossing at
railroad tracks depending on
signal lights

Decision Structure

One of two possible actions is taken,


depending on a condition.

Decision Structure

A new symbol, the diamond, indicates a yes/no


question. If the answer to the question is yes, the
flow follows one path. If the answer is no, the flow
follows another path
NO

YES

Decision Structure

In the flowchart segment below, the question is x


< y? is asked. If the answer is no, then process A
is performed. If the answer is yes, then process B
is performed.
NO

YES
x < y?

Process A

Process B

Decision Structure

The flowchart segment below shows how a


decision structure is expressed in C++ as an if/else
statement.
Flowchart
C++ Code
NO

YES

if (x < y)

x < y?

a = x * 2;
else

Calculate a as
x plus y.

Calculate a as
x times 2.

a = x + y;

Decision Structure

The flowchart segment below shows a decision


structure with only one action to perform. It is
expressed as an if statement in C++ code.
Flowchart
C++ Code
NO

YES
x < y?

if (x < y)
a = x * 2;

Calculate a as
x times 2.

Repetition Structure

A repetition structure represents part of the


program that repeats. This type of structure
is commonly known as a loop.

Repetition Structure

Notice the use of the diamond symbol. A loop tests


a condition, and if the condition exists, it performs
an action. Then it tests the condition again. If the
condition still exists, the action is repeated. This
continues until the condition no longer exists.

Repetition Structure

In the flowchart segment, the question is x < y?


is asked. If the answer is yes, then Process A is
performed. The question is x < y? is asked again.
Process A is repeated as long as x is less than y.
When x is no longer less than y, the repetition
stops and the structure is exited.
x < y?

YES
Process A

Repetition Structure

The flowchart segment below shows a repetition


structure expressed in C++ as a while loop.
Flowchart

C++ Code
while (x < y)

x < y?

x++;

YES
Add 1 to x

Controlling a Repetition
Structure

The action performed by a repetition structure


must eventually cause the loop to terminate.
Otherwise, an infinite loop is created.
In this flowchart segment, x is never changed.
Once the loop starts, it will never end.
QUESTION: How can this
flowchart be modified so
it is no longer an infinite
YES
x < y?
loop?
Display x

Controlling a Repetition
Structure

ANSWER: By adding an action within the


repetition that changes the value of x.

YES
x < y?

Display x

Add 1 to x

Case Structure

One of several possible actions is


taken, depending on the contents of a
variable.

Case Structure

The structure below indicates actions to


perform depending on the value in
years_employed.
CASE
years_employed

1
bonus = 100

bonus = 200

bonus = 400

Other
bonus = 800

Case Structure
If years_employed = 2,
bonus is set to 200
If years_employed = 1,
bonus is set to 100

1
bonus = 100

If years_employed = 3,
bonus is set to 400
If years_employed is
any other value, bonus
is set to 800

CASE
years_employed

bonus = 200

bonus = 400

Other
bonus = 800

Pseudocode

A detailed description of what a computer program


or algorithm must do.
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

Sample Pseudocode
1. Input a name and a set of 4 scores
2. Calculate their average by summing
and dividing by 4
3. If average is greater than or equal to
60
4. Print the Name, Grade and PASSED,
5. Else Print Name, Grade and FAILED

Knowledge Check

Go to kahoot.it website
Enter the game pin

Knowledge Check

It is a finite set of instructions that specify a


sequence of operations to be carried out in
order to solve a specific problem.
Two most common tools in Algorithm
Steps in Programming
True or False: Almost every program and
constructing a flowchart involves the steps
of input, processing and output.

Knowledge Check

What are the Four Flowchart Structures?


This type of structure is commonly known
as a loop.
Directs the computer to process the
program instructions, one after another, in
the order listed in the program.
One of several possible actions is taken,
depending on the contents of a variable.

Knowledge Check
Identify the
flowcharting
symbols.

Knowledge Check

A graphical representation of the logical


steps that solves a problem.
Memory allocations, the content of which
may vary/change.
What are the rules in naming a variable?
A value that never changes during the
execution of the program.
A textual presentation of a flowchart.

Thank You!
Prepared By: Engr. LRQ Natividad

Das könnte Ihnen auch gefallen