Grade 11 Problem Solving - Content

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

IPO (Input, Processing and Output) Chart

An IPO Chart normally breaks a problem into three parts.

• The inputs or what should be used or what is given to solve the problem
• The process, actions or the activities to be carried out
• the output or the results of processing
Another name for the IPO Chart is a Defining Diagram.
Diagram

Create an IPO Chart for a program that should calculate the GCT and Final Price of an item after
15% Tax is added:

Input Processing Output


ItemPrice Accept ItemPrice GCT
Calculate GCT
Store GCT FinalPrice
Calculate FinalPrice
Store FinalPrice
Display GCT and FinalPrice

Narrative
A narrative is very similar to writing IPO charts. Narratives involve writing the various steps to
solve a problem in plain English.
Example : Write an algorithm to read/accept two numbers, calculate and output their sum.

Solution:
Step1: Start
Step 2: Get/read/accept/input the two numbers
Step 3: Add the two numbers
Step 4: Store the sum
Step 5: Output the sum
Step 6: Stop

Pseudocodes
Pseudo code: An imitation computer program written using mathematical notations and English-
like statements to describe the logic to solve a problem or carry out a procedure.

A pseudo-code may use one or more of the following statements:


• Input Statements
• Assignment Statements
• Output Statements
• Control Statements (Sequence, Selection, Repetition/Iteration)

In writing pseudocodes, the programmer must use variables to store the values inputted, to store
the results of processing as well as to make reference to when output is required by the user.

Variable: A variable reserves a location in memory to store a value to be used in the program. The
value that will be stored in this variable location is unknown and has the ability to change
throughout the execution of the program.
Examples of variable names include: ‘number’, ‘age’, ‘name’, ‘length’, ‘price’, ‘GCT’, ‘area’, ‘A’,
‘B’, ‘C’ and so on

Key Points about variables:


• A variable must NEVER begin with a number.
• Variables should NOT have spaces between characters.
• ONLY the underscore (__) symbol should be used when creating a variable name. A
variable can be a combination of letters and numbers, for example, number1 or
num_1.

Constant: This contains a value that remains the same throughout the running of the program. For
example, assigning ‘pi’ to be 3.14.

Data Types
There are several data types that variables use to store values. These include:

 Integer:
Integer Includes non-decimal numeric values, these values may be negative or positive
whole numbers.

 Real:
Real Real numbers, sometimes referred to as float/floating point are ALL numbers, both
negative and positive, inclusive of decimals. Normally used for variables that are decimals
or can work out to be decimals, for example, a variable that stores the result after a division
operation.

 Character (char):
(char) A single letter or symbol, for example ‘A’.

 String:
String A string is a group of characters, for example a name like ‘ANDREW’.

Important facts to consider when writing/preparing to write pseudocode algorithms:


• Pseudocode algorithms ALWAYS begin with the word ‘Start’ and end with the word ‘End’.
• Select appropriate variable and constant names.
• Use the assignment symbol ( or :=) in assignment statements instead of the equal (=)
sign.
• Define variables by stating exactly what the variable or constant represents and the data
type.
• Decide on ALL processing activities necessary to solve the given problem.

Operators
These are used in pseudocodes to enable calculations and comparisons to be made. The different
operators are:
1. Arithmetic operator
2. Relational Operators
3. Logical/ Boolean Operators

Arithmetic Operators: used to perform mathematical calculations.

Arithmetic Operators Symbols


- Subtraction
+ Addition
/ Division for real numbers
* Multiplication
div Division for integers
mod (%) Remainder for div operations
Relational Operators: used to make comparisons and determine relationship between two things.

Relational Operators Symbols


= Equal to
<>,# Not equal to
< Less than
> Greater Than
>= Greater Than or Equal to
<= Less than or Equal to

Boolean Operators: Used to combine two or more conditions.

AND:
AND Both conditions presented must be TRUE
OR:
OR At least one of the conditions must be TRUE
NOT:
NOT This is true only if a variable is absent. (Not present)

Input Statement
The input statement is used to get data from outside the computer from a particular input device
into a variable for the pseudocode to perform some tasks or operations.

The most common keyword used to perform input in a psuedocode algorithm is READ.
READ Other
words used for this operation are INPUT or ACCEPT.
ACCEPT

Example; Write an algorithm fragment to ACCEPT two numbers from the user.

Solution: Read num1,


Read num2 (where ‘num1
num1’
num1 will be the variable used to store the first
number and ‘num2
num2’
num2 will store the second number)

Please note that entering of both variables can be done in one input statement. For example,
Read Num1, Num2

Assignment Statement
An assignment statement is used to assign a value to a variable. A value may be in the form of an
expression or a number.

Examples of assignment statements are:


GCT ← 0.165 (where the value 0.165 is assigned to the variable ‘GCT’. In this case, the variable
GCT can be regarded as a constant as it is given a specific value)

Prod ← Num1 * Num2 (the result of multiplying the values stored in the variables ‘Num1’ and
‘Num2’ will be stored in the variable ‘Prod’)

Output Statement
The output statement is used to get information to the programmer or to the user. The keywords
used to perform output are ‘PRINT
PRINT’,
PRINT ‘DISPLAY’,
DISPLAY’, or ‘OUTPUT’.
‘OUTPUT’
Format for an output statement:
Print “literal”, variable (the word Print, followed by the literal, displayed in quotations. A comma
is placed after the literal then any variable being referenced should follow)
Example 1: Write an algorithm fragment to display the sentence “Have a good day”:

Solution: Print “Have a good day”

Example 2: Write an algorithm fragment to display the value of the variable PROD, that represents
the Product of two numbers:

Solution: Print “The product of the two numbers is”, PROD

 In the example above a literal is displayed and a variable is also referenced. To reference a
variable put a comma after the literal, then write the variable name.

 Please note that the sentence or phrase enclosed in quotations “The product of the two
numbers is” will be printed/outputted exactly as is by the program. If 100 is the value
stored in the variable location PROD, the user will see (The product of the two numbers is
100) as the output on their computer screen.

 If the user only require the product without the use of the literal, the statement would be
Print PROD

Example of a Full Pseudocode Algorithm

A pseudocode algorithm code structure may be in Sequence, Selection or Repetition:

Sequence
Codes or statements are written and executed in the designed sequence 1st, then 2nd, then 3rd etc..

Example: Write a pseudocode algorithm to accept two numbers, find and output the sum of two
numbers:

Let us examine how the narrative would appear

Narrative
Step 1: Start
Step 2: Get the two numbers
Step 3: Add the two numbers
Step 4: Store the sum
Step5: Output the sum
Step 6: Stop

Let us now follow the guide of the narrative and create a pseudocode with the use of variables with
the steps to match indicated in bracket.

In creating the pseudocode, you can make up your own variable names but make them as
meaningful as possible based on the requirements of the program.
Start (step1) (Beginning of program)

Read Num1 (Step 2) (A variable or location in memory that will be used to store the first
number)

Read Num2 (step 2) (A variable or location in memory that will be used to store the
second number)

Sum ← Num1+Num2 (step 3&4) (The result of adding the two numbers entered is to be stored in
the variable Sum)

Print Sum (step 5) (Displays the value of the variable Sum)

End (step 6) (End of program)


End

With the use of a literal in the output


statement, the pseudocode would be:
The final solution will be reflected as:
Start
Start Read Num1
Read Num1 Read Num2
Read Num2 Sum ← Num1+Num2
Sum ← Num1+Num2 Print “The total of the two numbers is”, Sum
Print Sum End

Example2: Write an algorithm that will accept the price of an item, calculate the GCT Value (16.5%)
and the final cost of the item after GCT is added. Display the GCT value and Final price
of the item.

Narrative Pseudocode
Step 1: Start Start
Step 2: Accept/Get GCT GCT← 0.165
Step 3: Accept/Get Item Price Read It_Price
Step 4: Calculate GCT Value GCT_Val ← GCT * It_Price
Step5: Store GCT Value F_Price ← GCT_Val + It_Price
Step 6: Calculate Final Price Print “The GCT Value is”, GCT_Val
Step 7: Store Final Price Print “The price of the item is”, F_Price
Step 8: Output GCT Value End
Step 9: Output Final Price
Step 10: Stop

The Prompt Statement

A prompt statement is an output statement. It is normally found at the beginning of a pseudocode


as it provides the user with some information in the form of an instruction to allow the program to
carry out some actions. Being an output statement, it normally uses “Print”, “Display”, “Output”,
followed by a literal in the form of a statement.

Example
Write an algorithm to prompt the user to enter three numbers where the computer will accept the
three numbers, calculate and print the sum and average of the three numbers.
Start
Print (“Please enter the first number”)
Read Num1
Print (“Please enter the second number”) Please note that the average is calculated using the
Read Num2 variable “sum” as the numbers were added and stored in
Print (“Please enter the third number”) that variable location. It could have also been calculated
Read Num3 as: Average (num1 + num2 + num3)/3
Sum Num1 + Num2 + Num3
Average sum/3
Print “The sum is”, Sum
Print “The Average of the three numbers is”, Average

Selection/Conditional Statements (Decision)

When we write pseudocodes, we may need to include selection statements. There are times when
we are presented with certain conditions (situations) and we need to make certain decisions or
perform some action based on whether the condition that exist is true or false. A simple example
can be explained with our daily activities, For example, if you are sleepy you will go to bed but if
you are not sleepy, you will not go to bed. The decision will be made based on the condition of
whether or not “you
you are sleepy”.
sleepy

The if-
if-then-
then-else statement

The if-then-else statement is the main conditional statement used in pseudocodes. This statement
enables some action to be performed based on whether a condition is true or false. Using the
situation above, the if-then-else statement would appear as:

Condition

if you are sleepythen

go to bed Statement executed


after then only if
else conditions is true

do not go to bed

endif Statement executed


after else only if
condition false

It should be noted that both statements can never be executed at the same time. If the condition is
true,
true then the statements immediately after “then” are executed and the (else) section ignored by
the program; if the condition is false then the statements after (then) are ignored and only the ‘else’
statements are executed, NEVER both.

There are times when the pseudocode does not concern itself with whether or not a condition is
false, therefore the else section of the if-then-else statement is not necessary. For example:
if you are sleepythen

go to bed

endif

It should be outlined that the ‘if


if statement’ begins with an “if” and ends with the word “endif”.
endif”. It is
also recommended that you practice indentation so the logic of the program can be easily followed.

Example:: Write a pseudocode algorithm that will prompt the user for two numbers then store the
larger number in the variable Larger. The program should display the value stored in
the variable Larger.
Narrative Step 4: Check to see which of the two
Step 1: Start numbers is larger
Step 2: Ask the user to enter two different Step5: Store the larger of the two numbers
numbers Step 6: Output the larger of the two numbers
Step 3: Accept the two numbers Step 7: Stop

Start (Beginning of program)

Print “Enter a number” (Prompts the user to enter the first of the two numbers)
Read Num1 (A space in memory called “Num1” is reserved to store the first
number)

Print “Enter a different number” (Prompts the user to enter the second number)
Read Num2 (A space in memory called “Num2” is reserved to store the second
number)

If Num1>Num2 then (Test to see if the value stored in Num1 is greater than the value
stored in
Num2)

Larger ← Num1 (If the statement above is TRUE the value stored in the variable
location “Num1” will be assigned to the variable location “Larger”)

Else (Considering that the condition above is FALSE)


FALSE

Larger ← Num2 (If the statement above is FALSE the value stored in the variable
location “Num2” will be assigned to the variable location “Larger”)

Endif (End of the If statement)

Print ‘The larger number is”, Larger (Displays the value stored in the variable Larger)

End (End of program)

The final pseudocode would appear as:


Start
Print “Enter a number’”
Read Num1
Print “Enter a different number”
Read Num2
If Num1>Num2 then
Larger ← Num1
Else
Larger ← Num2
Endif
Print ‘The larger number is”, Larger
End

Example2: Write a pseudocode algorithm that will accept a student’s score and display the word
’EXCELLENT’ if the score is 80 or greater, ‘AVERAGE’ if less than 80 but greater than 55
and ‘POOR’ if less than 55:

Start (Beginning of program)

Print “Enter the students score” (Prompts the user to enter the student’s score)
Read Score (A space in memory is reserved for the user to enter the student’s
score)

IF Score >=80 then Test if the score entered is greater than 80)

Print “EXCELLENT’” (Prints EXCELLENT if the score is 80 or greater)

ElseIF Score >=55 then (Test if the score entered is greater than 55, considering that the
condition above was false. Note this test will not take place if
condition 1 was TRUE)

Print “AVERAGE” (Prints AVERAGE if the score is greater than 80)

Else
Print ‘POOR’ (Prints POOR if the previous conditions are both False)

Endif (Ends the if structure)

End (End of program)

Loops/Repetition/Iteration:
Loops/Repetition/Iteration Repetition statements are used in situations where a fragment of
code is to be executed more than once. The use of loops within psuedocodes instruct the computer
to repeat a set of statements for a certain number of times or until a situation (condition) becomes
true or false.

Loops may be definite (bounded iteration) or indefinite (unbounded iteration).

♦ Definite Loops (bounded iteration)is used whenever the number of times a set of
instructions or statements to be repeated is known or certain.

♦ Indefinite repetition (unbounded iteration) is used whenever the number of times a set
of instructions or statements to be repeated is unknown or uncertain. For repetition a
statement(s) is repeated until a condition is met or becomes false.
There are three main types of loop or iterations: For Loop, While Loop and Repeat Until.
The For Loop
The “FOR” loop is a definite loop or bounded iteration. It enables a set of instructions or statements
to be repeated a set (certain) number of times. When you know exactly how many times the
instructions or statements are to be repeated, the “for loop” should be used.

Depending on the requirements of the program, “for loops” will require the use of counters and
accumulators. These are just variables that will be used to track the various changes being made in
the program or to control the number of times instructions are repeated.

 Counters: are used for counting the number of times a value is entered or a statement is
carried out.

 Accumulators: are used for storing the updated results as each set of instructions are
repeated.

Example of a Definite Repetition Code (For Loop):


Loop) Write a pseudocode algorithm that will accept the
age, in years, of 10 students and find the average age:

Start (Beginning of program)

SumAge ← 0 (The variable SumAge is an accumulator that is set to a start value


of zero. The ages of ten students are required to find the average.)

counter ← 0 (The variable counter is initialized to zero)

For counter ← 1 to 10 do (The code between For and Endfor will be executed 10 times. The
counter counts and control the number of times the statements are
repeated. It starts counting at 1, and do the same actions until it
reaches 10.)

Print “Enter the age of a student” (Prompts the user to enter the age of a student)
Read StuAge (The value of age will be accepted and stored in the variable
‘StuAge’)

SumAge ← SumAge + StuAge (The variable SumAge will be updated with the value entered by
the user. The variable SumAge is acting as an accumulator by
keeping a running total each time an age is entered. This will occur
ten times as counter starts at 1 and ends at 10.)

counter ← counter + 1 (The variable ‘counter’ is keeping track of how many times the user
enters an age. That is, when counter starts at 1, (counter ←
counter + 1) means counter is now 2, so the instructions should
repeat because it has not reached the maximum of 10.

Endfor (Ends the loop)

AvgAge ← SumAge/counter (Calculates the average age and stores it in the variable AvgAge.
The average is calculated outside the loop because the instructions
must be repeated ten times before the average can be calculated)

Print “The average age is”, AvgAge (Displays/Prints the value stored in the variable AvgAge)
End (Ends Program)
NB The for loop is a pretest loop. In a pretest loop the condition is tested before the instructions are
carried out.

The final solution would appear:

Start
SumAge ← 0
counter ← 0
For counter ← 1 to 10 do
Print “Enter the age of a student”
Read StuAge
SumAge ← SumAge + StuAge
counter ← counter + 1
Endfor
AvgAge ← SumAge/counter
Print “The average age is”, AvgAge
End

NB The for loop is a pretest loop. In a pretest loop the condition is tested before the instructions are
carried out.

The While Loop


The “While” loop is an example of an indefinite loop or unbounded iteration. It is not known exactly
how many times the statements (instructions) will be carried out. These instructions will be
carried out until a condition is met or a situation becomes true or false, which will end the loop.

Example of an indefinite Repetition Code (While Loop):


Loop) Write a pseudocode algorithm that will
accept the age of persons in a class until the age 19 is entered. The algorithm must then find the
average age.

Start

SumAge ← 0 (The variable SumAge is an accumulator that is set to a start value


of zero. The ages of all students entered must be added to find the
average.)

StCount ← 0 (The variable StCount will be used to count the number of students
entered in order for the average to be calculated)

Print “Enter the age of a student” (Prompts the user to enter the age of a student)
Read StuAge (The value of age will be accepted and stored in the variable
‘StuAge’)

WhileStuAge<>
While 19 do (The code between While and EndWhile will be executed until the
value 19 is entered for ‘StuAge’. This is testing whether or not the
age entered is 19. As long as the age entered is not equal to (<>)
19, the instructions will be repeated. When the user enters 19, this
will end the repetition.

SumAge ← SumAge + StuAge (The variable SumAge will be updated with the value entered by
the user. The variable SumAge is acting as an accumulator by
keeping a running total each time an age is entered.)
StCount ← StCount + 1 (The variable ‘StCount’ is keeping a running total of the number of
student ages entered)

Print “Enter the age of a student” (Prompts the user to enter the age of a student once more so it can
be tested by the loop, this will determine whether the loop will
continue and instructions executed)
Read StuAge (The value of age will be accepted and stored in the variable
‘StuAge’, which will be checked against 19)

EndWhile (Ends the loop)

AvgAge ← SumAge/StCount (Calculates the average age and stores it in the variable AvgAge)

Print “The average age is”, AvgAge (Displays/Prints the value stored in the variable AvgAge)

End (Ends Program)

Solution
Start
SumAge ← 0
StCount ← 0
Print “Enter the age of a student”
Read StuAge
WhileStuAge<>
While 19 do
SumAge ← SumAge + StuAge
StCount ← StCount + 1
Print “Enter the age of a student”
Read StuAge
EndWhile
AvgAge ← SumAge/StCount
Print “The average age is”, AvgAge
End
The Repeat - Until Loop

The Repeat – Until loop operates similarly to the while loop with the only exception being that the
condition is tested at the end of the loop. As a result, the instructions or statements will be carried
out as long as the specified condition in the UNTIL statement is false.

Question: A variable Num is assigned a value of 15. Add 5 to Num and display the result until the
value is 40

Start
Num 15
Repeat
NumNum + 5
Print “The new number is”, Num
Until (Num = 40)

NB The Repeat-
Repeat-Until loop is a post-test loop. In a post-test loop the condition is tested after the
instructions are carried out (at the end of the loop).

Flow Charts

A graphical tool that diagrammatically depicts the steps and structure of an algorithm or program

Symbols (the most commonly used ones):


Examples of Flow Charts
Sequence

Start
Start
Read Num1, Num2

Sum ← Num1+Num2
Input Num1, Num2
Print ‘The result of adding the two numbers is’ , Sum

End

Sum ← Num1+Num2

Print Sum

End

Selection
Start
Start

Read Score Read Score

If Score >= 60 then


Result ← ‘Pass’
True False
Score>= 60
Else
Result ← ‘Fail’
Result ← ‘Pass’ Result ← ‘Fail’
Endif

Print Result

End
Print Result

End
Repetition

Start Start
SumAge ← 0
StCount ← 0
SumAge ← 0
StCount ← 0 Read StuAge

While StuAge<> 19 do
Input Stu_Age

StCount ← StCount + 1

False While StuAge<> 19 SumAge ← SumAge + StuAge

True
Read StuAge

StCount ← StCount + 1 EndWhile


SumAge ← SumAge + StuAge

AvgAge ← SumAge/StCount
Input Stu_Age
Print ‘The average age is’, AvgAge
End
AvgAge ← SumAge/StCount

Print Avg Age

Stop

You might also like