0% found this document useful (0 votes)
9 views

Computer Flow Charts

Uploaded by

Wynter mulyata
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Computer Flow Charts

Uploaded by

Wynter mulyata
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

MASALA SECONDARY SCHOOL

DAY 2
Mathematics CPD – 2019.

TOPIC: COMPUTER
(Pseudo-codes and Problem-solving)

PRESENTERS:

MR. BANDA S
Introduction
In the previous presentation we learnt how to draw simple
flow charts and how to solve simple logical problems. Such
problems did not involve making many decision but rather
doing task sequentially i.e. one after the other. This is known
as the sequence control structure.
Introduction Cont’d
In this presentation however, we will first learn how to write simple
problem solving programs in a language called pseudo-code. We will
then learn how to solve slightly advanced problems that involve a
step or steps to make decision to determine the next step.

We will also learn how to solve problems that involve repeating a
certain task until a certain condition is met.
PSEUDOCODE

What is a Pseudo-code?
Pseudo-code Cont’d
A Pseudo-code is a set of statement written in a human
readable language (usually English like phrases) but expressing
the processing logic of a program.

It is a generic way of describing an algorithm without use of


any specific programming language.

This is a derivative of a flow chart and its underlying factors


are the correct extraction of statements inside a flow chart
symbol, listing them vertically and preservation of the logical
steps also known as dentation.
Pseudo-code Cont’d
 Some of the words used in Pseudo-code may be drawn
directly from a programming language and then mixed
with English to form structured statements that are
easily understood by non-programmers but also make a
lot of sense to programmers too.

 Pseudo-codes are therefore not executable by a


computer.
Guidelines For Designing A Good Pseudo-code
1. The statement must be short, clear and readable.
2. Statements should not be ambiguous
3. Pseudo-code lines should be clearly outlined and indented
(Statements are indented but terminals are lined.)
4. Capitalize initial keywords. There are just a few keywords we
will use: READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE,
REPEAT, UNTIL
5. No punctuations at the end of the statements.
The logical operators used in
pseudo-code
 = is equal to

 > is greater than

 < is less than

 ≥ is greater than or equal

 ≤is less than or equal

 ≠ is not equal to
Example 1
Write a Pseudo code for calculating the Area of the
square:

Solution
Note that one may not be given the formula but it is important to
know it because after decision statement it must be shown (𝑨𝒓𝒆𝒂 =
𝒍^ 𝟐) as shown in the solution.
Solution 1 Cont’d
Example 2
Write a pseudo-code for calculating volume of a cone.
Solution 2
Example 3
Write a Pseudo code for calculating the area of a circle whose
radius is r:

Solution
For this one in some instances one may not be given the formula
but it is important to know it because after decision statement it
must be shown (𝑨𝒓𝒆𝒂 = 𝝅 *r*r) as shown in the solution.
Solution 3
Start
Enter radius
If radius<0
Then print error message
re-enter positive radius
Else Area = 𝜋*square radius
End if
Display Area
Stop
Group Activity

1. Construct a pseudo code for solving a quadratic equation


𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0.
SOLUTION 1
Start
ENTER 𝒂, 𝒃, 𝒄
COMPUTE 𝑫 = 𝒃∗ 𝒃 − 𝟒∗ 𝒂∗ 𝒄
IF 𝑫 < 𝟎, 𝑻𝑯𝑬𝑵
PRINT “no real solutions.”
ELSE
COMPUTE 𝒙𝟏 = (−𝒃 + 𝒔𝒒𝒓𝒕 𝑫)/𝟐 ∗ 𝒂
COMPUTE 𝒙𝟐 = (−𝒃 − 𝒔𝒒𝒓𝒕 𝑫)/𝟐 ∗ 𝒂
ENDIF
PRINT 𝒙𝟏 and 𝒙𝟐
Stop
SELECTION CONTROL STRUCTURES IN A
PROGRAM (DECISIONS)

 In pseudo-code, we represent a selection or a decisions


with an IF statement or CASE statement.

 A program often needs to decide whether something is


true or false in order to see which way to continue.

 Programs often use IF (or IF..THEN or


IF..THEN..ELSE) statements to show a decision.
DECISIONS CONT’D
 An IF statement always has a condition to check, often a
comparison between a variable and a number.

 The IF statement also must specify what to do if the


condition or comparison is true.

 These instructions (for “true”) may come after the word


THEN, or they may simply be listed.
DECISIONS CONT’D
There are three forms of the IF statement.
(a)The IF……….THEN statement.
(b)The IF…..THEN……ELSE statement.
(c) The NESTED IF statement.
(a) The IF…THEN statement is used when
only one option is available.

In an IF…. THEN statement, when the condition


is false, the program simply ignores the THEN
commands and continues to the next line.
IF…THEN statement Cont’d
The structure is shown below.

IF <condition> THEN
sequence 1
ENDIF
Example:
IF a>0 THEN
Print a
IF…THEN statement Cont’d

Example 1

Given two points A (𝒙𝟏 , 𝒚𝟏 ) and B(𝒙𝟐 , 𝒚𝟐 ), construct


the pseudo-code for finding the gradients of the
line passing through two points.
SOLUTION 2
Start
ENTER 𝒚𝟏 , 𝒚𝟐, 𝒙𝟏 , 𝒙𝟐
IF 𝒙𝟐 − 𝒙𝟏 = 𝟎, 𝑻𝑯𝑬𝑵
PRINT undefined m
ELSE
COMPUTE 𝒎 = (𝒚𝟐 − 𝒚𝟏 )/(𝒙𝟐 − 𝒙𝟏 )
ENDIF
PRINT 𝒎
Stop
(b) The IF…..THEN……ELSE
The IF…..THEN….ELSE statement is used if only two options are available. It is a
condition that can be true or false. Its structure is as shown below:

IF a>b THEN
Print a
ELSE
Print b
The IF...THEN…ELSE Cont’d
Example 2
Write the pseudo-code and draw the flow diagram
for a program that displays pass when the scores
is greater than 40 and fail when it is 40 or below.
The IF…..THEN……ELSE Cont’d
Solution
(C) The NESTED IF statement is used if more than two
options are available.
STRUCTURES:
The NESTED IF statement Cont’d
The NESTED IF statement Cont’d

Example 3
Write the pseudo-code and draw the flow chart
for a program that would categorize pupils
performance in mathematics as follows:
80 and above: Excellent, 60-79: Good, 40-59: Fair,
0 – 39: poor.
Solution flow chart
Repetition(Looping) in a program
Some programs involve repeating a task until a certain
condition is met. This is known as looping or iteration.

The three main looping controls are:


(a)The WHILE loop
(b)The REPEAT ……..UNTIL loop.
(c)The FOR loop.
(a) The WHILE loop
The WHILE loop is used if a condition has to be met before the
statements within the loop are executed.
This type of conditional loop tests for terminating condition at
the beginning of the loop. In this case no action is performed at all
if the first test causes the terminating condition to evaluate as
false.
Structure for The WHILE loop

Pseudo code
The syntax is:
WHILE (a condition is
true)
A statement or block
of statements
ENDWHILE
Example 4
Write the pseudo code for finding the sum greater than or equal 50 of
consecutive prime numbers.
(b) The REPEAT…….UNTIL loop
unlike the while loop, repeat……until allows the statement within it to be
executed at least once since the condition is tested at the end of the loop.

Pseudo code

REPEAT
A statement or block of statements
UNTIL a true condition
Example 6.

The diagram below shows a flow chart


for a program to calculate tax on an
income.
Flow Chart
(C) The FOR loop

The FOR loop is used in circumstances where the execution


of the chosen statement has to be repeated a predetermined
number of times.
Example 8
write a pseudo-code and flow chart program that
can be used to calculate the sum of ten numbers
provided by the user.
GROUP ACTIVITIES

1.Given that 𝒇 𝒙 = 𝒙 − 𝟕 ,write the pseudo code for


finding the value of 𝒇 𝒌

2.Write the pseudo code for finding the gradient of a


straight line y=mx + c
CONCLUSION
This topic on computer in mathematics does not
replace the need for one to be computer literate via
the learning of computer studies but among others;
It provides the knowledge on stages of problem
solving (define a problem, analysis method of
solution) and knowledge on how to write a computer
program.
End.
God Bless
You!!

You might also like