Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

7 Algorithm design and problem-solving

1 Understand the program development life cycle, limited to: analysis, design,
coding and testing
Definition - When a person, or organisation, creates a new computer program they will use a
structured, organised plan of how to create the program. This is called the program
development life cycle.
Stages -
– analysis:
• abstraction, decomposition of the problem, identification of the problem and
requirements
• the first stage of the program development life cycle that involves investigating the
problem.
• Abstraction
• simplifying the problem
• removing unnecessary details from the problem // selecting elements required
• filtering out irrelevant characteristics from those elements
• Decomposition - taking a system and splitting it into smaller sub-systems, which can
in turn be split into smaller sub-systems.

– design:
• decomposition, structure diagrams, flowcharts, pseudocode
• the second stage of the program development life cycle, which involves
decomposition of the problem and algorithms created ready for implementation.

– coding:
• writing program code and iterative testing
• the writing of a program using one or more programming languages.

– testing:
• testing program code with the use of test data
• the writing of a program using one or more programming languages.

2 (a) Understand that every computer system is made up of sub-systems, which


are made up of further sub-systems
(b) Understand how a problem can be decomposed into its component parts
• Including:
– inputs
– processes
– outputs
– storage

(c) Use different methods to design and construct a solution to a problem


• Including:
– structure diagrams - a hierarchical diagram that shows the decomposition of a system.
– flowcharts - a diagrammatic representation of an algorithm.
– pseudocode - code 1ike statements that are used to design an algorithm but do not follow
any specific language.

Que - A satellite navigation system is an example of a computer system that is made up of


sub-systems. Part of a satellite navigation system:
• allows the user to enter details for a new destination or select a previously saved destination
• displays directions in the form of a visual map or as a list.
Draw a structure diagram for this part of the satellite navigation system. [4]
3 Explain the purpose of a given algorithm
• Including:
– stating the purpose of an algorithm
– describing the processes involved in an algorithm

Que – Describe the processes involved in an algorithm


• Initialisation
• inputting
• storing in an array/Variable
• sorting in alphabetical order/ascending/descending order using a bubble sort
• displaying
• iteration

4 Understand standard methods of solution


– linear search
– bubble sort
FOR i 1 TO LENGTH(NUM)
FOR j 1 to i-1
IF Num[j]>Num[j+1]
Temp Num[j]
Num[j]  Num[j+1]
Num[j+1] Temp
ENDIF
NEXT
NEXT

– totalling
– counting
– finding maximum, minimum and average values

5 (a) Understand the need for validation checks to be made on input data and
the different types of validation check
• To test if the data entered is possible / reasonable / sensible.
• A range check tests that data entered fits within specified values.
• Example – range check , length check, type check, presence check, format check,
check digit

(b) Understand the need for verification checks to be made on input data and the
different types of verification check
• To test if the data input is the same as the data that was intended to be input.
• A double entry check expects each item of data to be entered twice and compares both
entries to check they are the same.
• Example - visual check, double entry check
6 Suggest and apply suitable test data
• Limited to:
– normal
– abnormal
– extreme
– boundary
• Extreme data is the largest/smallest acceptable value
• Boundary data is the largest/smallest acceptable value and the corresponding
smallest/largest rejected value

Que - A programmer has written an algorithm to check that prices are less than $10.00
These values are used as test data: 10.00 9.99 ten
State why each value was chosen as test data.[3]
• 10.00 boundary / abnormal data // the price should be rejected // value is out of range
• 9.99 boundary / extreme / normal data // the price should be accepted // value is
within normal range
• ten abnormal data // input should be rejected // value is wrong type

7 Complete a trace table to document a dry-run of an algorithm


• Including, at each step in an algorithm:
– variables
– outputs
– user prompts

8 Identify errors in given algorithms and suggest ways of correcting these errors
Que - An algorithm has been written in pseudocode to input some numbers. It only outputs
any numbers that are greater than or equal to 100. The number 999 is not output and stops the
algorithm. [4]
Identify the four errors in the pseudocode and suggest corrections.
• Numbers should be Number
• IF Number > 100 should be IF Number >= 100
• INPUT Number is missing from inside the loop, insert INPUT Number after the
ENDIF statement.
• The final OUTPUT Number is not needed, remove it.
Que - Write a pseudocode statement to change the corrected algorithm to output all numbers
between 100 and 200 inclusive. You do not need to rewrite the whole algorithm. [2]
Ans - The test should be IF Number >= 100 AND Number <= 200

9 Write and amend algorithms for given problems or scenarios, using:


pseudocode, program code and flowcharts
• Precision is required when writing algorithms,
e.g. x > y is acceptable but
x is greater than y is not acceptable

---------------------------------------------------------------------------------------------------------

8 Programming
8.1 Programming concepts
1 Declare and use variables and constants
 DECLARE Counter : INTEGER
 DECLARE TotalToPay : REAL
 DECLARE GameOver : BOOLEAN
 CONSTANT HourlyRate ← 6.50
 CONSTANT DefaultText ← "N/A"
 Variables and constants should be used when creating and running a program
 Variables and constants should have meaningful identifiers so that
programmers/future programmers are able to understand their purpose
 They are both used for data storage
 Constants store values that never change during the execution of a program e.g.
CONSTANT Pi  3.142
 Variables contain values that have been calculated within the program / can change
during the execution of the program e.g. Count Count + 1

2 Understand and use the basic data types


• INTEGER a whole number
• REAL a number capable of containing a fractional part
• CHAR a single character
• STRING a sequence of zero or more characters
• BOOLEAN the logical values TRUE and FALSE

Literals
Literals of the above data types are written as follows:
• Integer 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", ""
• Boolean TRUE, FALSE

Identifiers
Identifiers (the names given to variables, constants, procedures and functions) are in mixed
case using Pascal case, e.g. FirstName. They can only contain letters (A–Z, a–z) and digits
(0–9). They must start with a capital 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 to which they refer. 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 should never be used as identifier names.
Identifiers should be considered case insensitive, for example, Countdown and CountDown
should not be used as separate variables.

3 Understand and use input and output


INPUT Answer
OUTPUT Score
OUTPUT "You have ", Lives, " lives left"

4 (a) Understand and use the concept of sequence


(b) Understand and use the concept of selection
• Including:
– IF statements
– CASE statements
IF statements may or may not have an ELSE clause.
IF statements without an ELSE clause are written as follows:
IF <condition>
THEN
<statements>
ENDIF
IF statements with an ELSE clause are written as follows:
IF <condition>
THEN
<statements>
ELSE
<statements>
ENDIF
Note that the THEN and ELSE clauses are only indented by two spaces. (They are, in a sense,
a continuation of the IF statement rather than separate statements.)
When IF statements are nested, the nesting should continue the indentation of two spaces.

Example – nested IF statements


IF ChallengerScore > ChampionScore THEN
IF ChallengerScore > HighestScore THEN
OUTPUT ChallengerName, " is champion and highest scorer"
ELSE
OUTPUT Player1Name, " is the new champion"
ENDIF
ELSE
OUTPUT ChampionName, " is still the champion"
IF ChampionScore > HighestScore THEN
OUTPUT ChampionName, " is also the highest scorer"
ENDIF
ENDIF
CASE statements
CASE statements allow one out of several branches of code to be executed, depending on the
value of a variable.
CASE statements are written as follows:
CASE OF <identifier>
<value 1> : <statement>
<value 2> : <statement>
...
OTHERWISE <statement>
ENDCASE
It is best practice to keep the branches to single statements as this makes the pseudocode
more readable. Similarly, single values should be used for each case. If the cases are more
complex, the use of an IF statement, rather than a CASE statement, should be considered.
Each case clause is indented by two spaces. They can be considered as continuations of the
CASE statement rather than new statements.
Note that the case clauses are tested in sequence. When a case that applies is found, its
statement is executed, and the CASE statement is complete. Control is passed to the
statement after the ENDCASE. Any remaining cases are not tested.
If present, an OTHERWISE clause must be the last case. Its statement will be executed if
none of the preceding cases apply.

Example – formatted CASE statement


INPUT Move
CASE OF Move
ꞌWꞌ : Position ← Position – 10
ꞌEꞌ : Position ← Position + 10
ꞌAꞌ : Position ← Position – 1
ꞌDꞌ : Position ← Position + 1
OTHERWISE OUTPUT "Beep"
ENDCASE

(c) Understand and use the concept of iteration


• Including:
– count-controlled loops (FOR…TO…NEXT)
– pre-condition loops (WHILE…DO...ENDWHILE)
– post-condition loops (REPEAT…UNTIL)

Que - Four pseudocode descriptions and five pseudocode statements are shown.
Draw a line to link each pseudocode description to the most appropriate pseudocode
statement. Some pseudocode statements will not be used. [4]
Que - Using a single loop, write an algorithm in pseudocode to output 50 names that have
been stored in the array, Name[] [3]
Count ← 0
WHILE Count < 50 DO
OUTPUT Name[Count]
Count ← Count + 1
ENDWHILE

(d) Understand and use the concepts of totalling and counting


Total  Total +Num
Count  Count + 1

(e) Understand and use the concept of string handling


• Including:
– length
– substring
– upper
– lower
• The first character of the string can be position zero or one
LENGTH(<identifier>)
• Returns the integer value representing the length of string. The identifier should be of
data type string.
LCASE(<identifier>)
• Returns the string/character with all characters in lower case. The identifier should be
of data type string or char.
UCASE(<identifier>)
• Returns the string/character with all characters in upper case. The identifier should be
of data type string or char.
SUBSTRING(X,Y,Z)
• Returns a substring of X starting at position Y and Z characters long.
• X should be of data type string, Y and Z should be positive and data type integer.
• Generally, a start position of 1 is the first character in the string.
Example –
LENGTH("Happy Days") will return 10
LCASE(ꞌWꞌ) will return ꞌwꞌ
UCASE("Happy") will return "HAPPY"
SUBSTRING("Happy Days", 1, 5) will return "Happy"
Que - The variables X, Y and Z are used to store data in a program:
• X stores a string
• Y stores a position in the string (e.g. 2)
• Z stores the number of characters in the string.
(a) Write pseudocode statements to declare the variables X, Y and Z.
Ans - DECLARE X : STRING
DECLARE Y : INTEGER
DECLARE Z : INTEGER

(b) The function LENGTH(X) finds the length of a string X.


The function SUBSTRING(X,Y,Z) finds a substring of X starting at position Y and Z
characters long. The first character in X is in position 1.
Write pseudocode statements to:
• store the string "Programming is fun" in X
• find the length of the string and output it
• extract the word fun from the string and output it. [6]
Ans - X ← "Programming is fun"
OUTPUT LENGTH(X)
Y ← 16
Z←3
OUTPUT SUBSTRING(X,Y,Z)

(f) Understand and use arithmetic, logical and Boolean operators


• Arithmetic, limited to: • Logical, limited to: • Boolean, limited to:
+ –= – AND
– –< – OR
/ – <= – NOT
* –>
^ (raised to power of) – >=
MOD – <> (not equal to)
DIV

5 Understand and use nested statements


• Including nested selection and iteration
• Candidates will not be required to write more than three levels of nested statements

6 (a) Understand what is meant by procedures, functions and parameters


Procedures
 Enable the programmer to write a collection of programming statements under a
single identifier
 Allow modular programs to be created // to allow procedures to be re-used within the
program or in other programs
 To make program creation faster because procedures can be re-used // to enable
different programmers to work on different procedures in the same project
 To make programs shorter (than using the repeated code) / using less duplication of
code // to make programs easier to maintain due to being shorter.
Parameters
 To pass values from the main program to a procedure / function so that they can be
used in the procedure / function
 Allow the procedure / function to be re-used with different data

(b) Define and use procedures and functions, with or without parameters
• Procedures and functions may have up to two parameters

Defining and calling procedures


A procedure with no parameters is defined as follows:
PROCEDURE <identifier>
<statements>
ENDPROCEDURE

A procedure with parameters is defined as follows:


PROCEDURE <identifier>(<param1>:<datatype>, <param2>:<datatype>...)
<statements>
ENDPROCEDURE
The <identifier> is the identifier used to call the procedure. Where used, param1, param2, etc.
are identifiers
for the parameters of the procedure. These will be used as variables in the statements of the
procedure.
Procedures should be called as follows:
CALL <identifier>
CALL <identifier>(Value1,Value2...)
These calls are complete program statements.
When parameters are used, Value1, Value2... must be of the correct data type as in the
definition of the procedure.
When the procedure is called, control is passed to the procedure. If there are any parameters,
these are substituted by their values, and the statements in the procedure are executed.
Control is then returned to the line that follows the procedure call.

Example – use of procedures with and without parameters


PROCEDURE Add ( )
Sum  0
OUTPUT Sum
ENDPROCEDURE
PROCEDURE Add(Num1, num2 : INTEGER)
Sum  Num1 + Num2
OUTPUT Sum
ENDPROCEDURE
CALL Add( )
CALL Add(4,3)

Defining and calling functions


Functions operate in a similar way to procedures, except that in addition they return a single
value to the point at which they are called. Their definition includes the data type of the value
returned.

A function with no parameters is defined as follows:


FUNCTION <identifier> RETURNS <data type>
<statements>
ENDFUNCTION
A function with parameters is defined as follows:
FUNCTION <identifier>(<param1>:<datatype>, <param2>:<datatype>...) RETURNS <data
type>
<statements>
ENDFUNCTION
The keyword RETURN is used as one of the statements within the body of the function to
specify the value to be returned. Normally, this will be the last statement in the function
definition.
Because a function returns a value that is used when the function is called, function calls are
not complete program statements. The keyword CALL should not be used when calling a
function. Functions should only be called as part of an expression. When the RETURN
statement is executed, the value returned replaces the function call in the expression and the
expression is then evaluated.

Example – definition and use of a function


FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER
RETURN Number1 * Number1 + Number2 * Number2
ENDFUNCTION
OUTPUT "Sum of squares = ", SumSquare(10, 20)

Que - Tick (√) one box to show the named section of a program that performs a specific task.

Ans - B

Que - Describe what happens when a function is called during the execution of a program.
 A call statement is used in order to make use of a function // the function is called
using its identifier
 Parameters are / may be passed (from the main program) to the function (to be used
within the function)
 The function performs its task and returns a value / values to the main program
Que - A function is declared using pseudocode.
FUNCTION ConvertToCm(Inches: REAL) RETURNS REAL
RETURN Inches * 2.4
ENDFUNCTION
Tick (√) one box which accurately describes the use of the variable Inches

Ans – C

(c) Understand and use local and global variables


• local variables
- scope is a defined block of code/subroutine/procedure/function
- value cannot be changed elsewhere in the program
• global variables
– scope is the whole program
– value can be changed anywhere in the program

7 Understand and use library routines


RANDOM ()
 To generate (pseudo) random numbers within a specified range
 Example RANDOM(10,12) returns a random number between 0 and 10 inclusive
 RANDOM() returns a random number between 0 and 1 inclusive.

ROUND(X,Y)
 Returns the value of the X rounded to Y places of decimal places.
 X should be of data type real, Y should be data type integer.
 To return a value rounded to a specified number of digits / decimal places
 The result will either be rounded to the next highest or the next lowest value
 … depending on whether the value of the preceding digit is >=5 or <5
 Example - ROUND(4.56, 1) returns 4.6
DIV(X,Y)
 Returns the quotient of X divided by Y with the fractional part discarded.
 To perform integer division. Meaning only the whole number part of the answer is
retained
 Examples - DIV(10, 3) returns 3

MOD(X,Y)
 Returns the remainder of X divided by Y. The identifiers are of data type integer.
 This is a library routine returns the remainder of a division
 To perform (integer) division when one number is divided by another and find the
remainder
 Examples – MOD(10, 3) returns 1

8 Understand how to create a maintainable program


• Appropriate use of:
– meaningful identifiers
– the commenting feature provided by the programming language
– procedures and functions
– relevant and appropriate commenting of syntax
• Use meaningful identifiers for:
– variables
– constants
– arrays
– procedures and functions

8.2 Arrays

1 Declare and use one-dimensional (1D) and two-dimensional (2D) arrays


DECLARE StudentNames : ARRAY[1:30] OF STRING
DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR

How to initialise and populate the array Days[] at the start of the program?
Days[1]  "Sunday"
Days[2]  "Monday"
Days[3]  "Tuesday"
Days[4]  "Wednesday"
Days[5]  "Thursday"
Days[6]  "Friday"
Days[7] "Saturday"
OR
Days [“Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”]

Example – assigning a group of array elements


FOR Index ← 1 TO 30
StudentNames[Index] ← ""
NEXT Index

2 Understand the use of arrays


• Storage: Arrays provide a systematic way to store multiple elements of the same data
type under a single variable name.
• Access: Elements in an array are accessed using an index.
• Iteration: Arrays facilitate iteration over the entire collection of elements using loops.
• Data Structures and Algorithms: They are used in the implementation of dynamic data
structures like stacks, queues, and trees, as well as in algorithms for sorting and
searching
• Efficiency: Arrays offer efficient memory utilization and access to elements. They
allocate contiguous memory blocks, enabling direct memory addressing and efficient
random access to elements based on their indices. This results in faster retrieval and
manipulation operations compared to other data structures.
• Applications: They are used in database management, numerical computation,
graphical rendering, gaming, and scientific simulations.

3 Write values into and read values from an array using iteration
• Including the use of variables as indexes in arrays
• The first index can be zero or one
• Including nested iteration

8.3 File handling


1 Understand the purpose of storing data in a file to be used by a program
Que - Explain why a program might need to store data in a file.
 Data is stored permanently. //Data is not lost when the computer is switched off
 Data can be moved to another computer. //Data can be transported from one place /
system to another.
 Another copy of data can be made and stored//accessed elsewhere // backup copy//
data can be backed up or archived
 Data can be used by more than one program or reused when a program is run again

2 Open, close and use a file for reading and writing


Notes and guidance
• Including:
– read and write single items of data
– read and write a line of text

It is good practice to explicitly open a file, stating the mode of operation, before reading from
or writing to it.
OPENFILE <File identifier> FOR <File mode>
 The file identifier will be the name of the file with data type string. The following file
modes are used:
• READ for data to be read from the file
• WRITE for data to be written to the file. A new file will be created and any
existing data in the file will be lost.
 A file should be opened in only one mode at a time.

Data is read from the file (after the file has been opened in READ mode) using the
READFILE command.
READFILE <File Identifier>, <Variable>
When the command is executed, the data item is read and assigned to the variable.

Data is written into the file after the file has been opened using the WRITEFILE command.
WRITEFILE <File identifier>, <Variable>
When the command is executed, the data is written into the file.

Files should be closed when they are no longer needed using the CLOSEFILE command.
CLOSEFILE <File identifier>
Example – file handling operations
This example uses the operations together, to copy a line of text from FileA.txt to FileB.txt
DECLARE LineOfText : STRING
OPENFILE FileA.txt FOR READ
OPENFILE FileB.txt FOR WRITE
READFILE FileA.txt, LineOfText
WRITEFILE FileB.txt, LineOfText
CLOSEFILE FileA.txt
CLOSEFILE FileB.txt

You might also like