Understanding Computational Thinking Concepts
Understanding Computational Thinking Concepts
Page | 2
9.1 Computational Thinking Skills complexity.
Q 1) What is meant by computational thinking? The benefits of abstraction:
Computational Thinking - is used to study a • the time required to develop the program is
problem and formulate an effective solution reduced so the program can be delivered to
that can be provided using a computer. There the customer more quickly
are several techniques used in computational • the program is smaller in size so takes up
thinking, including abstraction, decomposition, less space in memory and download times
and pattern recognition. are shortened
• customer satisfaction is greater as their
Q 2) Define Abstraction, Decomposition and requirements are met without any extraneous
pattern recognition. features.
Abstraction – the process of extracting
information that is essential, while ignoring Q 3c) Stepwise refinement is often used in the
what is not relevant, for the provision of a development of an algorithm.
solution. Describe stepwise refinement.
Decomposition – the process of breaking a Stepwise refinement – is the practice of
complex problem into smaller parts. subdividing each part of a larger problem into
Pattern recognition – the identification of a series of smaller parts, and so on, as
parts of a problem that are similar and could required.
use the same solution. Benefits of Stepwise refinement:
Increases the level of detail of the algorithm
Q 3a) Give reason/benefits for Make steps easier to solve
decomposition (breaking down a problem). to be directly translated into lines of code
Program code is easier to implement.
Different people can be assigned to work on Q 4) What is system and sub-system?
different modules A system is a set of rules, an arrangement of
Program code is easier to test and debug. things, or a group of related things that work
Modules are re-usable. together to perform a function.
A system is made up of a number of
Q 3b) Describe need for and benefits of using subsystems. Each subsystem can be further
abstraction, Describe the purpose of divided into subsystems and so on until each
abstraction. sub-system just performs a single action.
Abstraction involves filtering out information Computer system is often divided up into sub-
that is not necessary to solve a problem. systems. This division can be shown using top-
Abstraction gives us the power to deal with down design to produce structure diagrams
Page | 3
that demonstrate the modular construction of programmer can transfer from one language
the system. to another.
Programmer should be able to recognise /
Q 5) Define System and Sub-system. understand in another language:
System is a set of principles or procedures • Declaration, assignment, sequence,
according to which something is done; an selection, repetition (iteration)
organized scheme or method. Subroutines, Parameters passed between
A system is a set of rules, an arrangement of modules
things, or a group of related things that work program structure, Input and Output
together to perform a function.
A system is made up of a number of Q 8) Describe Structure Diagrams
subsystems. Each subsystem can be further The STRUCTURE DIAGRAM shows the
divided into subsystems and so on until each design of a computer system in a hierarchical
sub-system just performs a single action. way, with each level giving a more detailed
breakdown of the system into sub-systems.
Q 6) What is meant by computer system?
A COMPUTER SYSTEM is made up of
hardware, software & data, communications
and people; each computer system can be
divided up into a set of sub-systems. Each
subsystem can be further divided into sub-
systems and so on until each sub-system just
performs a single action.
Q 9) A structure chart is often used in modular
Q 7) Define Top-Down Design program design.
Top-down design is the breaking down of a State four features of structure diagram:
computer system into a set of subsystems, Feature 1: Sequence of operation
then breaking each sub-system down into a Feature 2: Selection of modules
set of smaller sub-systems, until each sub- Feature 3: Repetition
system just performs a single action. Feature 4: Parameters
instructions for a given task that forms a PSEUDO CODE is a simple method of
subsystem, not the whole system. Sub- showing an algorithm, using English-like
routines written in high-level programming words and mathematical operators that are
9.2 Algorithm
Q 1) Define algorithm.
Page | 5
Q 4) What are User-Defined Data Types? special characters but number cannot be
A user-defined data type is a data type for used in calculation.
which the programmer has included the For example, “Inqilab Patel”, “0300-2724734”,
definition in the program. Here user means “House No 56 Block 2, PECHS Karachi”
user of programming language i.e. the 5. BOOLEAN: A data type with two possible
programmer. values
Once the data type has been defined, For example, TRUE and FALSE or YES or NO
variables can be created and associated with 6. DATE: To store a calendar date
the user-defined data type. For example, 16/04/2010
Q 5) Why are User defined data types are Q 7) Differentiate variable and constant.
needed? Variable is memory location where a value can
Sometime built-in data types are not fulfilling be stored. The values stored in a variable are
the requirements of a programmer then needs changed during execution.
to define own data type. Constant is a memory location that stores
values, but values once stored cannot be
Q 6) Describe different data types with changed during program execution.
examples.
The following keywords are used to designate Q 8) What are identifier, also describe rules
atomic data types: for deciding identifiers?
1. INTEGER: A whole number (without Identifiers are the names given to variables,
fractional part) can be positive or negative constants, procedures and functions.
and used in calculations. They are in mix case. They can only contain
For example, COUNT which never requires letters (A–Z, a–z), digits (0–9) and underscore
fractional part and may store 56, 89, 1 (_). They must start with a letter and not a
2. REAL: A number capable of containing a digit.
fractional part can be positive or negative
and used in calculations.
For example, Weight may contain fractional
Part like 56.8, 89.0, 1.2
3. CHAR: 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
Page | 6
Q 9) What are Arithmetic operators? Count
Standard arithmetic operator symbols are
used: Terminator used at START
and END of the flowchart
+ : Addition
- : Subtraction
Process Box: Rectangle
* : Multiplication
/ : Division
MOD : Division to calculate remainder Input / Output Box:
Parallelogram
DIV : Integer division
Q 10) What are logic operators? What are Decision Box (IF and
their operands? CASE Selection, Loops)
Rhombus
The only logic operators used are AND, OR
and NOT. The operands and results of these
operations are always of data type
Function / Procedure CALL
BOOLEAN.
Q 21) Describe what is meant by sequence. IF statements are used when there are one or
after another in the order they are written. When there is only one option IF statements
In following example statement number ‘i’ will without an ELSE clause is written as follows:
III. Total Num1 + Num2 When there are two options IF statements with
statements. ELSE
Assignments should be made in the following to deal with many possible outcomes.
TotalToPay ← NumberOfHours *
HourlyRate
Q 23) What is meant by Selection/Condition,
Page | 8
CASE statements are written as follows: condition evaluates to TRUE. After the
CASE Marks OF statements have been executed the condition
>=90: Grade “A*” is tested again. The loop terminates when the
>=80: Grade “A” condition evaluates to FALSE.
>=70: Grade “B” The statements may not be executed even
>=60: Grade “C” once if, on the first test, the condition
>=50: Grade “D” evaluates to FALSE.
>=40: Grade “E” Example: To input a series of numbers and
OTHERWISE : : Grade “U” calculate total and stops if a –ve number is
ENDCASE entered:
The condition is checked at the beginning of
Q 24) What is meant by Loop (Repetition or the loop. If condition is true loop statements are
Q 25) Describe Count Controlled Loop. loop in which condition is given at the end of
Count-controlled (FOR) loops loop and which is executed only when the
number of repetition is already known. The statements in the loop will be executed at
For example to input 5 numbers: least once. The condition is tested after the
Q 26) What are pre-condition (WHILE) loops? to reject it if a negative number is entered and
A loop in which condition is given at the start ask to re-enter another number
of loop and which is executed only when the The condition is checked at the end of the loop.
condition is true, is called pre-condition loop. If condition is false, loop statements are
The condition is tested before the statements, executed again and again.
FOR … WHILE … REPEAT … input data with the original data to make sure
TO… NEXT ENDWHILE UNTIL that there have been no transcription errors
(transcription means copying the data).
Count- Pre-Condition Post
Verification methods include:
Controlled Loop Condition
loop Loop At the time of entry At the time of
transmission
Number of Executed Executed
repetition is when when Double entry Parity check
Data type – a classification attributed to an comparing adjacent items and swapping them
item of data, which determines the types of if they are in the wrong order.
value it can take and how it can be used. File – a collection of data stored by a
Identifier – a unique name applied to an item computer program to be used again.
of data. Abstract data type (ADT) – a collection of
Record (data type) – a composite data type data and a set of operations on that data.
comprising several related items that may be Stack – a list containing several items
of different data types. operating on the last in, first out (LIFO)
Composite data type – a data type principle. Items can be added to the stack
constructed using several of the basic data (push) and removed from the stack (pop). The
types available in a particular programming first item added to a stack is the last item to
language. be removed from the stack.
Linear search – a method of searching in • Queue – a list containing several items
which each element of an array is checked in operating on the first in, first out (FIFO)
order. principle.
Bubble sort – a method of sorting data in an Items can be added to the queue (enqueue)
array into alphabetical or numerical order by and removed from the queue (dequeue). The
Page | 13
first item added to a queue is the first item to diagram , ndex of 90 in 1D array is [3], while
be removed from the queue. in 2D is [3,1].
• Linked list – a list containing several items Type: Data type of all elements in a single
in which each item in the list points to the next array have same data types.
item in the list. In a linked list a new item is Dimension: Dimension is the organisational
always added to the start of the list. structure of array. It may be 1D that has single
Arrays index or 2D that has two indexes.
Arrays are data structure used to store Lower Bound – the smallest index number
multiple data items of same data type Upper Bound – the largest index number
(homogenous) under one identifier name. Q 2) The following diagram shows four data
Square brackets are used to indicate the structures and four descriptions. [3]
array index. Draw a line to connect each data structure to
Each element in the array is identified using the correct description.
its subscript or index number. The largest Data Description
and smallest index numbers are called the structure
upper bound and lower bound of the array. A collection of related
Constant
For illustration, let's take array declaration to data
store marks of 10 students.
Marks[10] Marks[1:10] Marks[0:9] A value that can change
Q 1) Describe The terms associated with Array whilst a program is
Arrays running
Name: The identifier of the array is called Array t
Name. E.g. StudentName[] A value that never
Element: Each data item stored in array is Table changes whilst a
called element. Array can store only single program is running
types of elements.
Size: The number elements the array can A series of elements of
store. E.g. StudentName[1:30] can store 30 Variable
the same data type
names while StudentName[30] can store 31
names as by default it is 0 to 30.
after any existing data.
Index: The position of each element is
referred as Index Number. In the above
Page | 14
Q 3) Winter 21 P21
3 A programmer is writing a program to help manage clubs in a school.
Data will be stored about each student in the school and each student may join up to three clubs.
The data will be held in a record structure of type Student.
The programmer has started to define the fields that will be needed as shown in the following
table.
Field Typical value Comment
StudentID "CF1234" Unique to each student
Email “[email protected]” Contains letters, numbers and certain symbols
Club_1 1 Any value in the range 1 to 99 inclusive
Club_2 14 Any value in the range 1 to 99 inclusive
Club_3 27 Any value in the range 1 to 99 inclusive
(a) (i) Write pseudocode to declare the record structure for type Student. [3]
TYPE Student
DECLARE StudentID : STRING
DECLARE StudentID : STRING
DECLARE Club_1 : INTEGER
DECLARE Club_2: INTEGER
DECLARE Club3 : INTEGER
ENDTYPE
(ii) A 1D array Membership containing 3000 elements will be used to store the student data.
Write pseudocode to declare the Membership array. [2]
DECLARE Membership : ARRAY[1:3000] OF Student
(iii) Some of the elements of the array will be unused. [1]
Give an appropriate way of indicating an unused array element.
Any value of correct data type outside the normal range
In this example “empty” can be assigned in empty ID or email
(iv) Some students are members of less than three clubs. [1]
State one way of indicating an unused club field.
A number outside the range like here 1 to 99 is the range so any number outside the
range can be assigned.
Page | 15
Linear Search
Linear Search - is a method in which each element of the array is checked in order, from the lower
bound to the upper bound, until the item is found or the upper bound is reached.
For example, to search marks in an 1D array Marks[1:30], user has to input data to be searched.
Algorithm compares this search data with all elements of the array, starting from lower bound. If
search data matches with the element, then it displays the location of data first found and if data is
not found, it is searched at next location in the conditional loop until the data is found or it reaches
at the upper bound.
Example Code for Single Searching using Linear Search
DECLARE SearchData: REAL
DECLARE Found: BOOLEAN
DECLARE MaxIndex : INTEGER
INPUT SearchData
Found FALSE
MaxIndex 30
Index 1
WHILE Index <=30 AND Found = FALSE
IF SearchData = Marks[Index]
THEN
Found TRUE
ELSE
Index Index + 1
ENDIF
ENDWHILE
IF Found = TRUE
THEN
OUTPUT “Found at”, Index
ELSE
OUTPUT “Not found”
ENDIF
Page | 16
Example Code for Multiple Searching, using Linear Search
To search and output all locations where the search data is found, a count controlled loop is used
instead of conditional loop.
DECLARE SearchData: REAL
DECLARE Found: BOOLEAN
DECLARE MaxIndex : INTEGER
INPUT SearchData
Found FALSE
FOR Index 1 TO 30
IF SearchData = Marks[Index]
THEN
Found TRUE
OUTPUT Index
ENDIF
NEXT Index
IF Found = FALSE
THEN
OUTPUT “not found.”
ENDIF
Bubble sort – is a method of sorting data in an array into alphabetical or numerical order by
comparing adjacent items and swapping them if they are in the wrong order.
Each element of the array is compared with the next element and swapped if the elements are in
the wrong order, starting from the lower bound and finishing with the element next to the upper
bound. The element at the upper bound is now in the correct position. This comparison is repeated
with one less element in the list, until there is only one element left or no swaps are made.
Page | 17
File Handling
Q 1) Why files are needed?
When a program is being executed data is stored in variables, constants, arrays etc. These data
structures use RAM, making them temporary, when program is closed Files stored data in
secondary storage devices. These are permanent and non-volatile. They retain data even after
computer is switched OFF, so the data can be accessed next time program is run.
For example to read 1st line from text file Teachers.txt and to store in a variable “TextLine”
OPENFILE “Teachers.txt” FOR READ
READFILE “Teachers.txt”, TextLine
PRINT TextLine
CLOSEFILE “Teachers.txt”