Professional Documents
Culture Documents
General problem-solving
• Every problem requires a solution to
resolve it. For any problem, you might
have more than one solution. Finding the
correct solution to a problem can be
considered a problem-solving. Problem-
solving involves identifying, analysing
and resolving problems using logic,
reasoning skills and analytical skills.
3
In the above, Num1 and Num2 are identifiers for two memory
locations and they store the values 10 and 15. Num1 and Num2
are variables as they are identifiers of storage locations that can
store any values.
Data types
Determines the type of data that a variable can store, a data type
that treats a variable as a single unit is called an elementary data
type.
Control Structures
Selection
Used in problems with instructions to be carried out if a certain
condition is met. The choice of options will be dependent on
whether the condition is true or false.
How to create algorithms for problem statements
Problem statement 1
Write an algorithm that will accept three numbers add them and
print their sum.
1. First gather the user values. In the problem statement, “three
numbers” is the input and ‘sum’ is the output required.
‘Accept’, ‘add’ and ‘print’ represent the processing that needs to
be done. Storage will take place when the calculated values of
the three numbers are assigned to sum.
23
2. Now break down the problem into steps and write it in the
form of single specific tasks to form the algorithm:
Start
Accept three numbers
Add the numbers entered
Display the sum
Stop
24
•Narrative
•Pseudocode
•Flowchart
25
Narrative
Narrative is also called general algorithm where each step in the
algorithm is written in clear simple language. It does not use any
computer language or coding.
Example if you want to write an algorithm to read three scores then
find and display their product, the narrative could be:
Start
Get the three scores
Multiply the scores
Store the results in product
Display product
Stop
Narrative is a representation of an algorithm where each instruction
is written in everyday language.
26
Pseudocode
Narrative steps are very simple and useful for when you start
learning about programming but when you are comfortable with
programming you can create algorithms using instructions with
words and symbols that closely resemble computer programming
language instructions. This form of representation is called
pseudocode.
Pseudocode language
It can contain variables, constants, operators and terminology used in
programming languages.
Variables
As we learnt, variables are used to store values that can change.
These values can be a user input or result of a calculation and are
stored in a location in the memory with a name chosen by the
programmer called a variable name. Examples of variable names
num1, num2, sum etc.
27
Pseudocode
Constants
These are fixed values used when you need to keep a value fixed.
Example to calculate area of triangle ½*base*height, the value 1/2
will be a constant.
Operators
These are symbols used for performing calculations or making
comparisons. Commonly used operators in pseudocode are:
1 Arithmetic operators – operators used to perform mathematical
operations
28
Pseudocode
2. Relational operators
Operators used to check for comparisons.
29
Pseudocode
3. Logical operators – operators used to make comparisons with
multiple criteria
Anything in brackets will be done first, followed by powers and
square roots, then division and multiplication, addition and
subtraction will be done last.
30
Pseudocode
Pseudocode terminology
•The general programming language terms used in pseudocode are:
•Terms used for the input step: input, read, get, accept
•Terms used for the output step: output, write, display
•Terms used for the assignment step: set, store
•Terms used for selection: if-else-endif
•Terms used for iteration(bounded): for-endfor
•Terms used for iteration (unbounded): while-endwhile, repeat-until
31
Pseudocode
Pseudocode algorithm 1
Write a pseudocode algorithm to find the average of three numbers.
Using the narrative then the pseudocode form
1 narrative
Start
Get the three numbers
Add the three numbers, divide by 3 and store the result
Display the results
Stop
32
Pseudocode
2 Pseudocode
Start
Read num1
Read num2
Read num3
Average = (num1+num2+num3)/3
print average
stop
Rules for Variable Names
Start Start
read num1, num2, num3
product = num1 * num2 * num3
Print product
Read num1,num2,num3
stop
Product=num1*num2*num3
l
Print product
Stop
40
Start
stop
41
Evaluating Boolean
Expressions to Make Comparisons
• The typical if-then decision in Figure 5-4 shows
the employee’s paycheck being reduced if the
employee participates in the dental plan
• No action is taken if the employee is not in the
dental plan
• A Boolean expression is one that represents only
one of two states, usually expressed as true or
false
46