0% found this document useful (0 votes)
69 views12 pages

AS P2 Fundamental Problem Solving

The document describes an algorithm as a series of well-defined steps to solve a problem. It notes that the word algorithm comes from the name of the 9th century mathematician al-Khwarizmi. It then provides a more modern definition of an algorithm as a sequence of steps for a computer program to accomplish a task.
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)
69 views12 pages

AS P2 Fundamental Problem Solving

The document describes an algorithm as a series of well-defined steps to solve a problem. It notes that the word algorithm comes from the name of the 9th century mathematician al-Khwarizmi. It then provides a more modern definition of an algorithm as a sequence of steps for a computer program to accomplish a task.
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/ 12

9.

2 Algorithm

Algorithm Pseudo code

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”

An algorithm is a sequence of steps for a computer program to accomplish a task.


In general, an 'algorithm' is the name given to a defined set of steps used to complete a task.
For instance, you could define an algorithm to make a cup of tea. You start by filling the kettle, and
then place a tea bag in the cup and so on.
In computer terms, an algorithm describes the set of steps needed to carry out a software task. This
mini-web takes you through the topic of algorithm

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
10.1 Data Types & Records
Candidates should be able to: Notes and guidance
Select and use appropriate data types for a including integer, real, char, string, Boolean,
problem solution date (pseudocode will use the following data
types:
INTEGER, REAL, CHAR, STRING, BOOLEAN,
DATE, ARRAY, FILE)

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Variable:
Variable is memory container, where a value can be stored. The values stored in a variable are
changed during execution.
Like in computer game you have observed Lives and Score.
At the start if game Score has a value 0, while Lives has
some values like 3. During game values stored in Score are
increased with each success and values stored in Lives
decreases with failure. Here Lives and Score are variables, storing values, which are changed
during program execution.
Identifiers
Identifiers (the names given to variables, constants, procedures and functions) are in mix case. They
can only contain letters (A Z, a z) and digits (0 9). They must start with a letter and not a digit.
Accented letters and other characters, including the underscore, should not be used.
As in programming, it is good practice to use identifier names that describe the variable, procedure or
function they refer to. Single letters may be used where these are conventional (such as i and j when
dealing with array indices, or X and Y when dealing with coordinates) as these are made clear by the
convention.
Keywords identified elsewhere in this guide should never be used as variables. Keywords are written
in ALL CAPS.
Identifiers should be considered case insensitive, for example, Countdown and Countdown should
not be used as separate variables.

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:

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
A single character (may be letter, special character or number but number cannot be used in
calculation)
For example, ‘A’, ‘$’, ‘5’

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).

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Variable declarations
It is good practice to declare variables explicitly in pseudo code.
Declarations are made as follows:
DECLARE<identifier> : <data type>
Example
DECLARE Surname : STRING
DECLARE FirstName : STRING
DECLARE DateOfBirth : DATE
DECLARE Section : CHAR
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN

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

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
the same data type as the variable.
Example – assignments
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate

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.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Operator Precedence:
In mathematics and computer programming, the order of operations (or operator precedence) is a
collection of rules that reflect conventions about which procedures to perform first in order to evaluate
a given mathematical expression.

Should we follow BODMAS or PEMDAS?


They are basically the same

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.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
Relational operations
The following symbols are used for relational operators (also known as comparison operators):
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to
The result of these operations is always of data type BOOLEAN.
In complex expressions it is advisable to use parentheses to make the order of operations explicit.
Logic operators
The only logic operators (also called relational operators) used are AND, OR and NOT. The operands
and results of these operations are always of data type BOOLEAN.
In complex expressions it is advisable to use parentheses to make the order of operations explicit.
String operations
Syllabus requirements The AS & A Level (9618) syllabus specifically requires candidates to know
string manipulation functions in their chosen programming language. Pseudocode string manipulation
functions will always be provided in examinations.
User-defined data types
Syllabus requirements The AS & A Level (9618) syllabus requires candidates to understand that
data structures that are not available in a particular programming language need to be constructed
from the data structures that are built-in within the language. User-defined data types need to be
defined, the syllabus requires candidates to use and define non-composite data types such as
enumerated and pointer; composite data types record, set, class/object. Abstract Data Types (ADTs)
stack, queue, linked list, dictionary and binary tree are also defined as composite data types.
Defining user-defined data types
A composite data type is a collection of data that can consist of different data types, grouped under
one identifier. The composite type should be declared as follows:
TYPE <identifier1>
DECLARE <identifier2> : <data type>
DECLARE <identifier3> : <data type>
...
ENDTYPE
Example – declaration of composite type

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


This user-defined data type holds data about a student.
TYPE Student
DECLARE Surname : STRING
DECLARE FirstName : STRING
DECLARE DateOfBirth : DATE
DECLARE YearGroup : INTEGER
DECLARE FormGroup : CHAR
ENDTYPE
Using user-defined data types
When a user-defined data type has been defined it can be used in the same way as any other data
type in declarations.
Variables of a user-defined data type can be assigned to each other. Individual data items are
accessed using dot notation.
Example – using user-defined data types
This pseudocode uses the user-defined type Student, Season and TAddPointer defined in the
previous section.

DECLARE Pupil1 : Student


DECLARE Pupil2 : Student
DECLARE Form : ARRAY[1:30] OF Student

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

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
S17 P22 - 1 (b) (i) Complete the following two sentences.
A suitable operand type for an arithmetic operator is ........................................................
A suitable operand type for a logical operator is ...............................................................[2]
(ii) The following table shows the values of three variables.
Variable Value
FlagA TRUE
FlagB FALSE
FlagC TRUE
Evaluate these expressions. [3]
Expression Evaluates to
(FlagA AND FlagB) OR FlagC
FlagA AND (FlagB OR FlagC)
(NOT FlagA) OR (NOT FlagC)

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/

You might also like