flow charts and pseudo codes - zahira college colombo · when a flowchart is too long to fit on a...

40
Flow Charts And Flow Charts And Pseudo Codes Pseudo Codes Flow Charts And Flow Charts And Pseudo Codes Pseudo Codes Pseudo Codes Pseudo Codes Pseudo Codes Pseudo Codes Grade 12 Grade 12 GCE ( A/L )- ICT Teacher Training Program – 2011

Upload: votu

Post on 16-May-2018

258 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Flow Charts And Flow Charts And Pseudo CodesPseudo Codes

Flow Charts And Flow Charts And Pseudo CodesPseudo CodesPseudo CodesPseudo CodesPseudo CodesPseudo Codes

Grade 12Grade 12

GCE ( A/L )- ICT Teacher Training Program – 2011

Page 2: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

• An algorithm is a complete step-by-step procedure for solving a problem or accomplishing a task

Page 3: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

� Flowcharts

� Pseudo Codes

Page 4: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

� It is a step by step Diagrammatic representation of the program

� Each type of task is represented by a � Each type of task is represented by a symbol

Page 5: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Diagram Notation Representation

Oval Start / End of a Program

Parallelogram Input / Output of Data

Rectangle Processing Operation

Rhombus Decision Box

Page 6: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Diagram Notation Representation

Circle Connection

Flow Lines Direction of Flow

Rectangle Sub Process

Page 7: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Start

Inputs

Start

Input a , b

Eg :

Outputs

Stop

Calculations

Output Sum

Stop

Sum = a + b

Page 8: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

• Sequence

• Selection

• Iteration• Iteration

Page 9: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

� SEQUENCE is a linear progressionwhere one task is performed sequentially after another. START

Statement 1

Statement 3

Statement 2

STOP

Page 10: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Find the sum and

average of

two numbers

Start

InputNumber1

InputNumber2

Sum = 0Average = 0

two numbers

Average = Sum / 2

DisplaySum, Average

Stop

Sum = Numbe1 + Number2

Page 11: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

SELECTION - there may be alternative steps that could be taken subject to a particular condition

IF-THEN-ELSE is a decision (selection) in which a choice is made between two alternative courses of action

Page 12: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Condition?

FalseTrue

Statement sequence 1 Statement sequence 2

Page 13: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

• Input the length and width of a quadrilateral and state whether it is a square Start

LengthGet

Length

WidthGet

Width

Width ?

Is Length equal to Width ?

“Figure is Square”Display

“Figure is Square”

Stop

Y

N

Page 14: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Input the length and width of a quadrilateral and state whether it is a square or a rectangle.

StartStart

Get Length, width

Is Length equalWidth ?

Display“Figure is Square”

StopStop

Y

NDisplay“Figure is Rectangle”

Page 15: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Connectors

� When a flowchart is too long to fit on a pageand is to be continued on another page aconnector symbol is used

� A small circle is used to represent a connector� A small circle is used to represent a connectorin a flowchart

� An alphabet or a number is written inside thecircle to identify the pairs of connectors to bejoined

Page 16: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Set value of Counter to 0

Set value of Total to 0

StartAdd number to value of total

Increment counter by 1

A

A

Get Number

BIs

Counter = 10?

B

Write total

Stop

Y

N

Page 17: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

� ITERATION - certain steps may need to be repeated while, or until, a certain condition is true

• While

• For• For

• Repeat

Page 18: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

while

Condition

false

Start loop

?

Statement

sequence

trueEnd loop

Page 19: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

• Find the sum

of 10 numbersCounter< 10

?

Start

counter = 0

total = 0

No

Display total

?

Stop

Get Number

total = total+number

counter=counter+1

yes

Page 20: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

For

Control_variableControl_variable

Control_variable = Start Value

false

Start For

< end_value

Control_variable

< end_value

?

true

Control_variable = Control_variable+1Control_variable = Control_variable+1

Statement-sequence

End For

Page 21: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Display the numbers 1, 2, 3, 4, 5, ….., 100Start

no = 1

Stop

no <= 100

Display no

no = no + 1

false

true

Page 22: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Example -7

• Enter marks of 4 subjects and find the average. If the average is less than 50 then display “pass” else display “fail”.

Page 23: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

StartStart

Input m1, m2, m3, m4

average = 0

average = ( m1 + m2 + m3 + m4 ) /4

falseIF Average < 50

Display“Pass”

StopStop

true

NDisplay“Fail”

false

Page 24: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Example -8

• A company gives discounts for the total bill paid by the customers. If the Bill amount is above Rs. 5000/-, a discount of 10 % is given. Otherwise discount of 10 % is given. Otherwise 5% is given. Input the Bill amount and calculate the discount amount.

Page 25: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

StartStart

Input Bill_Amount

IF Bill_Amount> = 5000

discount = 0

false

> = 5000

StopStop

true

DisplayDiscount

Discount = Bill_Amount * 0.10 Discount = Bill_Amount * 0.05

Page 26: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Example - 9

• A company pays a basic salary of Rs. 8000/- to the salesmen. If a salesman does sales of Rs. 50,000/-or above, he is given a 25% commission. Otherwise only 10%. commission. Otherwise only 10%.

Input the sales done by a salesman and calculate his salary for the month.

Page 27: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

StartStart

Input Sales_Amount

> = 50000

IF Sales_Amount

> = 50000

true

Commission = 0, Tot_Salary = 0

false

StopStop

true

Display Tot_Salary

Commission = Sales_Amount * 0.25 Commission = Sales_Amount * 0.10

Tot_Salary = 8000 + Commission

Page 28: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

• Pseudo codes use every day language…to prepare a

brief set of instructions…in the order…in which they

will appear in a finished program

• It is an abbreviated version of actual computer code • It is an abbreviated version of actual computer code

(that’s why it is called Pseudocode)

• Once pseudocode is created, it is simple to translate

into real programming code.

Page 29: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

• Sequence– Use set of instructions one after the other

• Selection• Selection– Use IF … THEN … ELSE

• Repetition– Use WHILE, FOR, REPEAT…UNTILL

Page 30: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

SequenceSequence

Statement 1

Statement 2

START Pseudocode;

statement 1

statement 2

statement 3

Statement 3

Statement 2

STOP

Page 31: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Example - 10

• Write a pseudo code that inputs two numbers (a and b) and calculates the sum of the numbers and output the sum

INPUT aINPUT b sum = a + bOUTPUT sum

Page 32: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

•• Compare and Select One of Two Compare and Select One of Two Alternative ActionsAlternative Actions

• Select one path according to the condition– IF …. THEN

• If the condition is true do the statements inside IF• If the condition is true do the statements inside IF• No operation if the condition is false

– IF …. THEN …. ELSE• If the condition is true do the statements inside IF• If the condition is false do the statements inside ELSE

Page 33: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

IF

Condition?

FalseTrue

Pseudocode:

IF condition

THEN

sequence-1(statements)

Statement sequence 1

ENDIF

Statement sequence 2

ELSE

sequence-2(statements)

ENDIF

Page 34: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

IF <condition> THENsequence 1

ENDIF

IF <condition> THEN

• Example1:IF a>0 THENPrint a

END IF

• Example2:IF a>b THEN

IF <condition> THENsequence 1

ELSEsequence 2

ENDIF

IF a>b THENPrint a

ELSEPrint b

END IF

Page 35: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Example-11

• Write a pseudo code that inputs two numbers (a and b) and output the largest number.

INPUT aINPUT bIF a < b THENIF a < b THEN

OUTPUT bELSE

OUTPUT aEND IF

Page 36: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

– WHILE … ENDWHILE

Page 37: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

WhileWhile

Condition

?

falsefalse

Pseudocode:

WHILE <Condition>

Statement- Sequence

END WHILE

Statement

sequence

truetrue

EndWhile

Page 38: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Example - 12

• Inputs 5 numbers and outputs the sum and average of them.

count = 1sum = 0WHILE count <= 5 Do

INPUT numINPUT numsum = sum + numcount = count + 1

END WHILEaverage = sum / 5DISPLAY sum, average

Page 39: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

Exercise• Draw the flow chart and write the pseudo code for following scenario.

1. Calculate the sum of odd numbers between 1 to 100

2. Calculate the sum of first ten 2. Calculate the sum of first ten triangular numbers

3. Find the largest number among three distinct integers

Page 40: Flow Charts And Pseudo Codes - Zahira College Colombo · When a flowchart is too long to fit on a page ... • Draw the flow chart and write the ... triangular numbers 3. Find the

4. Input ten positive integer values and Calculate the average.

5. Input 30 students name and ICT mark, calculate the subject average and print “Good Class” if average is greater than or equal 50, otherwise greater than or equal 50, otherwise print “Bad Class”.