flowcharts. 2 problem solving using computers. flowcharts : a language for expressing...

9
Flowcharts Flowcharts

Upload: fay-carr

Post on 03-Jan-2016

220 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Flowcharts. 2 Problem Solving using computers. Flowcharts : A language for expressing ‘Algorithms’

FlowchartsFlowcharts

Page 2: Flowcharts. 2 Problem Solving using computers. Flowcharts : A language for expressing ‘Algorithms’

2

Problem Solving using computers.

Flowcharts: A language for expressing ‘Algorithms’

Page 3: Flowcharts. 2 Problem Solving using computers. Flowcharts : A language for expressing ‘Algorithms’

3

Flowcharts A pictorial representation of algorithms;A pictorial representation of algorithms;

Flowcharts are not a programming language, rather it is an Flowcharts are not a programming language, rather it is an

algorithm specification language;algorithm specification language;

In an algorithm we specify the computation operations In an algorithm we specify the computation operations

(arithmetic, logical, data input, data output) to be done, and (arithmetic, logical, data input, data output) to be done, and

their sequence;their sequence;

Each type of computation operation is denoted by a special Each type of computation operation is denoted by a special

symbol in the flowchart language;symbol in the flowchart language;

Symbols are connected by arrows to represent the order of the Symbols are connected by arrows to represent the order of the

operations (sequence);operations (sequence);

Flowchart are useful during algorithm design;Flowchart are useful during algorithm design;

A flowchart can be relatively easily translated to a C++ A flowchart can be relatively easily translated to a C++

programprogram..

Page 4: Flowcharts. 2 Problem Solving using computers. Flowcharts : A language for expressing ‘Algorithms’

4

FlowchartsSymbols

Calculation Calculation (square or rectangle)(square or rectangle)

You use it to specify a calculation, e.g. You use it to specify a calculation, e.g. arithmetic expression.arithmetic expression.

Examples:Examples: x = z+y;x = z+y; a = pi * r*r;a = pi * r*r; z = z + 1 (add one to the current z = z + 1 (add one to the current

value of z and make that the new value of z and make that the new value of z);value of z);

c = SQRT(x*x+y*y);c = SQRT(x*x+y*y);

x, z, y, a, c, pi, r are called variables.x, z, y, a, c, pi, r are called variables.

z = z +1z = z +1

c = SQRT(x*x +y*y)c = SQRT(x*x +y*y)

Page 5: Flowcharts. 2 Problem Solving using computers. Flowcharts : A language for expressing ‘Algorithms’

5

FlowchartsSymbols

Data Input/OutputData Input/Output

(parallelogram)(parallelogram) Use it to specify an input or an output Use it to specify an input or an output

operation;operation; By an input operation we mean a By an input operation we mean a readread

operation of data from a peripheral device operation of data from a peripheral device to Main Memory (e.g. user typing on a to Main Memory (e.g. user typing on a keyboard );keyboard );

By an output operation we mean a By an output operation we mean a writewrite operation of data to a peripheral device operation of data to a peripheral device from Main Memory (e.g. data displayed on from Main Memory (e.g. data displayed on the monitor, or sent to the printer);the monitor, or sent to the printer);

Examples:Examples:

Read a value for the radius Read a value for the radius rr from the from the keyboard; print value of the area keyboard; print value of the area aa on the on the screen.screen.

input ainput a

print aprint a

Page 6: Flowcharts. 2 Problem Solving using computers. Flowcharts : A language for expressing ‘Algorithms’

6

FlowchartsSymbols

Decision or condition TestingDecision or condition Testing

(Diamond)(Diamond) Use it to specify a condition to be tested. Based on Use it to specify a condition to be tested. Based on

the condition being true or false the next operation the condition being true or false the next operation will be determined.will be determined.

ExamplesExamples

If (Adjusted Gross Income) > 40000 then If (Adjusted Gross Income) > 40000 then

taxRate = 0.31taxRate = 0.31

else else

taxRate = 0.28taxRate = 0.28

oror

if(r < 0 ) thenif(r < 0 ) then

print “invalid value for the radius”print “invalid value for the radius”

elseelse

continue.continue.

AGI>40000AGI>40000

??

TR = 0.31TR = 0.31

TR = 0.28TR = 0.28

You do it as an exerciseYou do it as an exercise

yesyes

nono

Page 7: Flowcharts. 2 Problem Solving using computers. Flowcharts : A language for expressing ‘Algorithms’

7

FlowchartsSymbols

Decision or condition Testing cnt’dDecision or condition Testing cnt’d A decision is composed of :A decision is composed of :

1.1. A condition;A condition;

2.2. An operation to be done if condition is true;An operation to be done if condition is true;

3.3. An operation to be done if condition is false;An operation to be done if condition is false;

We use decisions to enable repeating a set of We use decisions to enable repeating a set of operations multiple times; we call this operations multiple times; we call this construct a loop.construct a loop.

An example of a loop isAn example of a loop is

let sum = 0;let sum = 0;

do { do {

input a;input a;

sum = sum+a;sum = sum+a;

} until (a <0);} until (a <0);

a < 0a < 0

??

sum = 0sum = 0

yesyes

nono

sum = sum +asum = sum +a

input ainput a

Page 8: Flowcharts. 2 Problem Solving using computers. Flowcharts : A language for expressing ‘Algorithms’

8

FlowchartsSymbols

Start/EndStart/End Every algorithm starts somewhere, and Every algorithm starts somewhere, and

terminates somewhere;terminates somewhere;

Every flowchart must have one start Every flowchart must have one start symbol and one or more end symbols;symbol and one or more end symbols;

A start symbol denotes the start of the A start symbol denotes the start of the algorithm;algorithm;

When an end symbol is encountered this When an end symbol is encountered this indicates the algorithm has terminated.indicates the algorithm has terminated.

a < 0a < 0

??

sum = 0sum = 0

yesyes

nono

sum = sum +asum = sum +a

input ainput a

startstart

print sumprint sum

stopstop

Page 9: Flowcharts. 2 Problem Solving using computers. Flowcharts : A language for expressing ‘Algorithms’

9

FlowchartsExercise

Let’s Develop the flow chart for the algorithm that Let’s Develop the flow chart for the algorithm that computes the circumference of a circle whose radius computes the circumference of a circle whose radius rr is is given as input.given as input.

Don’t forget to check if the radius is positive number!Don’t forget to check if the radius is positive number!

Fig.2Fig.2