0% found this document useful (0 votes)
68 views25 pages

Unit 8 - Practical Problem Solving and Programming From ICT G9 Student Book-2

Uploaded by

Friday
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views25 pages

Unit 8 - Practical Problem Solving and Programming From ICT G9 Student Book-2

Uploaded by

Friday
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

UNIT 8 Practical problem solving and programming

Practical problem solving


8

Unit
and programming
Learning Objectives
By the end of Unit 8, learners should be able to:
• Draw flowcharts to solve simple problems
• Dry run flowcharts
• Write computer programs for simple problems

RECAP: Flowchart Grade 8

A flowchart is a diagram which uses symbols to represent problem solving steps. Lines and
arrows are used to show the sequences in which the steps occur. The shape of the symbol
indicates which type of information goes into the symbol.

Flowchart symbols

Symbols Description

Start This symbol indicates the start of a flowchart.

Stop This symbol indicates the end of a flowchart.

Input Input symbol is used to insert data for processing.

Output Output symbol is used to display data after processing.

It is used whenever there is a calculation, sorting, or matching


Process
to be carried out.

It refers to a decision which results in two possible choices.


Decision?
The answer to the Decision/Question can either be Yes or NO.

Flowlines

The flowlines indicates the movement of the data.

177
Dry run flowcharts
A dry run is a technique which is used to test a flowchart by recording different values in a
trace table. It is possible to see what happens to these values as they follow the sequence of a
flowchart. This method can be used to check for errors which may exist in the flowchart logic.
A dry run can be done on paper by simply following a program flowchart, recording the values
which are used at each stage and writing the changes in a trace table.

Trace table

A trace table enables the recording of values (variables) which are used to dry-run the flowchart.
It also shows the output after these values have been processed.

Variable

Values must be stored in memory so that they can be processed. There are many locations in
the memory. A variable is a memory location which is given a symbolic name and the content
of the location can change during the execution of a program.

A variable can be pictured like a box which can hold a value ( text or number). A label (variable
name) is placed on the box.

Value which
is entered while
the program is being
executed

15

Age Age

Label or Variable name Age = 15

178
UNIT 8 Practical problem solving and programming

The following is a flowchart which adds two numbers:


START
• INPUT: Num1, Num2

• PROCESS: SUM (Num1 + Num2)

• OUTPUT: SUM INPUT Num1

INPUT Num1

The symbols
in the flowchart above SUM (Num1 + Num2)
are arranged in a specific
order which is known
as a Sequence.
OUTPUT SUM

STOP

The following sets of numbers will be used to dry run this flowchart.

Set 1: 8, 6.
Set 2: 3, 10.

Trace table:

Variables

Num1 Num2 SUM OUTPUT


Set 1

8 6 8 + 6 = 14 14

Variables
Num1 Num2 SUM OUTPUT
Set 2

3 10 3 + 10 = 13 13

179
Activity 1
Try these values in the trace table above:

a) Set A: 58, 17

b Set B: 4, 7

Draw flowcharts to solve simple problems

Problem:

Create a flowchart which allows the user to enter a mark for an ICT test. If the mark is greater
than 49, then output Pass else output Fail.

Break down the problem into smaller steps which are easier to understand and solve.
This is called Step down refinement.

START
• INPUT: Mark

(Mark is a variable which can have


any value between 0 and 100)
INPUT Mark

• IF Mark > 49, OUTPUT “Pass”

IF Marks
Yes
OUTPUT "Pass"
> 49?

No

OUTPUT "Fail"
A decision symbol
is used to show a
condition which may have
two possible outcomes. STOP

180
UNIT 8 Practical problem solving and programming

Modify the flowchart to allow the insertion of marks for five students.

Problem break down:


START
The challenge is to repeat the INPUT
of marks five times. This can be done
by creating a loop.
Counter = 0

A new variable called Counter will be


used to count the number of times
that the steps are repeated. INPUT Mark

Each time that a new Mark is INPUT,


1 will be added to the Counter. Counter = Counter + 1

Counter = Counter + 1 is a new process


which must be added in the Sequence.
IF Marks
Yes
OUTPUT "Pass"
> 49?

No

OUTPUT "Fail"

IS Counter
= 5?

Yes

STOP

A loop is used to show


steps that are repeated
many times.

181
Activity 2: C
 omplete the trace table below with the following marks:
10, 80, 45, 78, 60

Mark Mark> 49 Output

Activity 3: Complete the flow chart below by using


the words from the list:

List:

OUTPUT “cool weather”

INPUT Temp

START

OUTPUT “hot weather” Yes No


IF Temp
< 32?

STOP

182
UNIT 8 Practical problem solving and programming

Write computer programs for simple problems

Before moving to Programming you may consider the following:

Structured English is an intermediate language which makes use of


English language and programming logic in order for non-technical
users to understand the program steps. It uses straight forward English
words and capitalise keywords to show the logical steps.

Some common keywords which are used in Structured English are:

BEGIN/END Shows the beginning and the end of statements

READ, INPUT Used to show input

PRINT, OUTPUT Used to show output

CREATE, CLOSE,
Used for processes
UPDATE, DELETE

A construct is used to control the order or flow by which instructions


are executed. There are three logic constructs which are commonly
used:

Sequence
The order by which the steps occur. In structured English each line is
called a statement and they are executed from top to bottom.

Selection
A selection is used when a statement or a set of statements is executed
only if a certain condition is met.

Repetition/Iteration
It refers to a set of statements which are executed multiple times in the
program.

183
Introducing PYTHON

A program is a set of instructions that the computer executes. To create programs we use
special programs known as programming languages.

Programming refers to the action of writing program instructions. These instructions or lines
of codes are called program statements.

Python is an easy to use programming language. The Python software is free and it can be
downloaded from the following URL:

https://www.python.org/downloads/

On the Python web site you can choose the version which is the most appropriate for your
operating system.

184
UNIT 8 Practical problem solving and programming

With the help of your teacher download the Python installer, double click on its icon and follow
the instructions:

1. Select install for all users and click next.

2. Leave the default directory unchanged and click next.

3. Ignore the customize Python section and click next.

After installing the program, create a shortcut on the task bar to access it easily.

1. Click on

2. Click on

3. Move your pointer across the screen until you locate Python in the list.
Right click on the Python icon. Then click on Pin to taskbar.

To start Python, click on the icon which is found on the task bar.

185
The Python Screen

The 3 greater- than sign >>> is called the prompt. Commands are typed at the place which is
indicated by the prompt and the Enter key is used to execute the command.

Menu

Prompt

Saving your file

To save the file for future use, Click on File, Save.

186
UNIT 8 Practical problem solving and programming

Type a file name and select a location.

PYTHON Commands

OUTPUT print ( ) command

The print command is used to output/display information. Type the command and strike Enter.

Displaying Strings/Characters

A string is a sequence of characters that the computer can process. Single quotes or double
quotes can be used. The quotes indicate to Python that what is inside the bracket is a string.

print (‘c’) The program will output c

187
print (‘computer’) The program will output computer

Displaying Numbers

To display numbers, no quotes are used.

print (1) The program will output 1

Print (6+9) The program will output 15

188
UNIT 8 Practical problem solving and programming

Activity 4: Try the following commands in Python.

a) print (2+5)

b) print (10 – 8)

c) print (40 * 3)

d) print (100/4)

e) print (5+6-2)

Displaying Strings using operators

print (‘computer’ + ‘studies’ ) The program will output computerstudies

print (‘computer’ * 2) The program will output computercomputer

189
Working with Variables John

As we have seen above, a variable is a memory location


which stores a value. The value can change when Python
is running. You can imagine it like a box on which a label
(variable name) is written. A number or string can be
inserted inside the box.

The variable name is Name and the string which it stores


is John
e
Nam
Commands:

Name = (‘John’)

Print (Name)

The program will output John the string which is found in the variable Name

Variable name

output Value inside the variable

Activity 5: Work out the following commands in Python

a) Age = 15

print (Age)

b) Text = ‘New variable created’

print ( Text )

190
UNIT 8 Practical problem solving and programming

INTPUT- input () command

The input () command allows you to read the data which is typed on the screen. This data is
then sent to a variable. Data which is stored in variables can be further processed or displayed.

Problem: Writing codes to allow the user to enter a Name in a variable and then output the
content of that variable.

Commands:
Name = input(‘Enter a name’)

Print (Name)

Steps: 1

input (‘Enter a name’) is the command


which will display a message asking
the user to type a name.
The variable Name is used to store
the name which the user types.

Steps: 2 - After typing the above command press enter.

Python displays a message for the user


to type a name.

191
Steps: 3 – Type the string Paul which will be sent to the variable Name.

The user types a name example Paul.

Steps: 4 – Use the print command to display the content of the variable Name

The print(Name) command displays the content of variable


Name which is Paul.

192
UNIT 8 Practical problem solving and programming

Activity 6

 rite Python statements to allow the user to enter a value in the variable Age and
a) W
then output its content.

Selection - IF STATEMENT

In programming we use a selection statement to do a comparison. The if statement is


constructed as follows:

if expression :
print statement or statements for true

else:
print statement or statements for false

Consider the flowchart created above to check marks and output Pass if the mark is greater
than 49 else Fail if the mark is 49 or less.

Expression to evaluate the mark which is


Command to display output stored in the variable. It is important to
if comparison is true. type: after the if and the else.

The alternative expression.


Command to display output
if comparison is false.

193
Loop

A loop is used when some steps/statements are repeated a number of times.

Example: 1

To print a string a number of times. The while command can be used to do this.

while expression:

print statement or statements

This statement refers to the condition


Add 1 to the variable counter which
which allows the print command to be
keeps track of how many times the
executed 3 times. Note that the counter
statement is executed.
starts at 0.

The string in the variable word is printed 3 times.

194
UNIT 8 Practical problem solving and programming

Example: 2

The for command can also be used to create loops.

for expression:

print statement or statements

To print a list of names, we use the for loop to print all the elements which are in the list one
after the other. The list contains 3 names John, Anne, Paul.

Variable or list containing the names. The for loop will take each item (x) in the
Remember to use ‘ ‘ for strings. list Names in turn.

Print (x) will cause the program to print


each value of X in the list Names.

195
Loops can also be used to do operations inside a string. For example, the variable Word contains
the string computer. The following codes display each character which makes up the string.

Variable containing the string ‘Computer’. For each value of (x) in the string ‘computer’.

Print (x) will cause the program to print


each character in the string ‘Computer’.

196
UNIT 8 Practical problem solving and programming

Loops can also be done to display values which falls in a range.

The range function returns a sequence of numbers starting from 0 which is the default value.

output
Statement which defines
the range for the loop.
Print (x) will cause the program to print
numbers in the range.

Note: range (10) starts with 0 and ends with 9.

Activity 7

a) W
 rite Python statements to allow the user to input an Age and print “Adult” if the
age is greater than 21 and “Youngster” if the age is 20 or less.

b) Write Python statements to allow the user to input his name and display it five times.

c) W
 rite Python statements to allow the user to input his name and output each
alphabets separately.

197
END OF UNIT QUESTIONS
1. Fill in the blanks in the sentences below by using the following keywords:

selection statement loop construct


program flowchart programming

i) Set of instructions which allows the computer to do a specific task.

ii) A line of code in a program.

iii) It is used to control the flow by which instructions are executed.

iv) Allows the program to execute a set of statements instead of another.

v) A set of instructions which are executed several times.

vi) The use of symbols to represent a program sequence.

2. a) Complete the flowchart by using the words from the list below.

START
List:

OUTPUT "Is a college student"


INPUT Year of
INPUT Current year

OUTPUT "Must leave school"

IF Age > 19?

Age = Current year - Year of birth

Yes IF Age
No
> 19?

STOP
198
END OF UNIT QUESTIONS UNIT 8 Practical problem solving and programming

b) Complete the trace table for the above flow chart with the following year of birth:
2000, 1998, 2005, 2010

Year of birth Age > 19 Output

C ) Write Python statements for the flowchart at A above.

199
Additional Notes

200
UNIT 8 Practical problem solving and programming

Additional Notes

201

You might also like