AS P2 Fundamental Problem Solving
AS P2 Fundamental Problem Solving
2 Algorithm
An algorithm is a series of
well-defined steps which
gives a procedure for solving
a type of problem.
The word algorithm comes
from the name of 9th
century mathematician al-
Khwarizmi (Muhammad Bin
Musa Al-Khwarizmi).
In fact, even the word
algebra is derived from his
book “Hisab al-jebrw’al-muqabala”
Data types:
A data type is a classification that specifies which type of data a variable has and what type of
operations can be applied to it.
Most programming languages support various types of data, including integer, real, character or
string, and Boolean.
1. INTEGER:
A whole number (without fractional part) like COUNT which never requires fractional part
For example, 56, 89, 1
2. REAL:
A number capable of containing a fractional part like Weight may contain fractional Part
For example, 56.8, 89.0, 1.2
3. CHAR:
4. STRING:
A sequence of alphanumeric and special characters but number cannot be used in calculation
For example “Abdullah”, “0300-2724734”, “House No 56 Block 2, PECHS Karachi”
5. BOOLEAN: A data type with two possible values
For example TRUE and FALSE or YES or NO
6. DATE: To store a calendar date
For example 16/04/2010
Literals
Literals of the above data types are written as follows:
Data Type Literals
Integers: Written as normal in the denary system, e.g. 5, -3
Real: Always written with at least one digit on either side of the decimal point, zeros
being added if necessary, e.g. 4.7, 0.3, -4.0, 0.0
Char: A single character delimited by single quotes, e.g. x , C , @
String: Delimited by double quotes. A string may contain no characters (i.e. the empty
string) e.g. "This is a string", "Abdullah Patel"
Boolean: TRUE, FALSE
Date: This will normally be written in the format dd/mm/yyyy. However, it is good
practice to state explicitly that this value is of data type DATE and to explain the
format (as the convention for representing dates varies across the world).
Constant:
Constant is memory location where a value can be stored but the stored value remaining same during
execution.
It is good practice to use constants if this makes the pseudo code more readable, as an identifier is
more meaningful in many cases than a literal. It also makes the pseudo code easier to update if the
value of the constant changes.
Constant declaration
Constants are normally declared at the beginning of a piece of pseudo code (unless it is desirable to
restrict the scope of the constant).
Constants are declared by stating the identifier and the literal value in the following format:
CONSTANT<identifier> = <value>
Example
CONSTANT HourlyRate = 6.50
CONSTANT DefaultText = “N/A”
Only literals can be used as the value of a constant. A variable, another constant or an expression
must never be used.
Assignments
The assignment operator is .
Assignments should be made in the following format:
<identifier> ← <value>
The identifier must refer to a variable (this can be an individual element in a data structure such as an
array or an abstract data type). The value may be any expression that evaluates to a value of
Common operations
Input and output
Values are input using the INPUT command as follows:
INPUT <identifier>
The identifier should be a variable (that may be an individual element of a data structure such as an
array, or a custom data type).
Values are output using the OUTPUT command as follows:
OUTPUT <value(s)>
Several values, separated by commas, can be output using the same command.
5.2 Arithmetic operations
Standard arithmetic operator symbols are used:
+ Addition
- Subtraction
* Multiplication
/ Division
^ Raised to the power of
Care should be taken with the division operation: the resulting value should be of data type REAL,
even if the operands are integers.
B - Brackets = P - Parentheses
O - Order = E - Exponents i.e. Powers
D - Division != M - Multiplication
M - Multiplication != D - Division
A - Addition = A - Addition
S - Subtraction = S - Subtraction
So we see the only difference is between the multiplication and division operators. Both are acronyms
designed to be easy to remember but don’t quite tell the full story. The central idea the two acronyms
try to get across is that you should do multiplication and division before addition and subtraction.
Now there are some awkward cases like 6/2*3, 6*3/2 and 6/3/2. Neither rule works for all these case.
The real rule is that when the operations are of the same precedence, (like * and /) you do the
operations from left to right. Hence 6/2*3 = (6/2) * 3 = 3 * 3 = 9 and 6 * 3 / 2 = (6 * 3) / 2 = 18 / 2 =
9 and 6/3/2 = (6/3)/2 = 2/2 = 1.
The same happens with + and –. So 6 – 2 – 3 = (6 – 2) – 3.
Pupil1.Surname ← "Johnson"
Pupil1.Firstname ← "Leroy"
Pupil1.DateOfBirth ← 02/01/2005
Pupil1.YearGroup ← 6
Pupil1.FormGroup ← ꞌAꞌ
Pupil2 ← Pupil1
FOR Index ← 1 TO 30
Form[Index].YearGroup ← Form[Index].YearGroup + 1
NEXT INDEX