4 Computational Thinking Latest
4 Computational Thinking Latest
Thinking
The concept of a Problem
• Identification
• Identify the problem
• Understand the problem
• Formulate the problem
• Development
• Explore various alternatives
• Selection
• Choose the best alternative
• Implementation
• Implement the selected solution
• Problem solving is not linear. Sometimes it is necessary to return to a previous stage.
• Computers can only solve problems that are solvable by humans, but also that computers can’t solve
some problems that are solvable by humans. (CAPTCHA – program used to prevent bots or automated
software from distorting online polls, registering etc)
• Some problems need human intelligence, human logic, human perception and human intuition.
• Only Advanced Artificial Intelligence programs can come close to imitating human performance in
these areas.
Algorithm
• A series of unambiguous instructions designed in order to solve a problem and achieve
a goal in a finite number of steps.
• Properties of Algorithm
• Finiteness – Finite number of steps, it should end
• Definiteness – Each step precisely defined
• Input -
• Output -
• Effectiveness –
• Simpler problem Simpler Algorithm
• Complex problem bigger & more complicated algorithm
• Writing an algorithm is in essence just problem solving
• Representations
• Simple English
• Flow chart
• Pseudocode
• Programming language
Flowcharts
• Diagrammatically describes an algorithm
Pseudocode
• Steps of an algorithm written in a particular notation
• Does not have a strict syntax
• Should be converted to equivalent High level language statements
Thinking Procedurally
Procedure appropriate to solving a
problem
• When a particular problem has to be solved, an effective method or
procedure should be identified.
• This procedure reduces the solution to a series of simple steps.
• These steps have to be followed in the correct order to obtain the
desired output.
Order of activities and required
outcome
• A company wishes to develop a new information system.
• The analysis phase will be the one that the company should complete
before proceeding to the next stages of the project.
• If the company tries to implement the new system without first
analyzing the problem domain, then a total waste of money, effort
and resources will almost certainly occur.
The role of sub-procedures in
solving a problem
• Breaking up the problem into smaller sub-problems.
• This method is very effective and efficient because it is much easier to attack a number of sub-problems instead of a
big complex problem.
• The resulting sub-problems can be further divided to smaller and smaller sub-problems until finally they can be dealt
individually.
• This strategy is often called top—down design or stepwise refinement.
• The very same approach could be used when developing complex computer programs.
• Using top-down program design, the complex problem is decomposed and for each sub-problem, an appropriate sub-
procedure is developed.
• A sub—procedure contains a series of commands that perform a task.
• When a sub procedure is called, all statements included in the particular sub-procedure are executed.
• All the sub-procedures that represent different parts of the problem solution can be used at the right time using their
identifiers.
• So the procedure is divided into a series of sub—procedures; this process is referred as a modular programming
approach.
• An identifier is the name that is used by the programmer to uniquely identify a variable, an object a sub-procedure etc.
Sub-Procedures
Thinking Logically
Decision-making in a specified
situation
• The addition process does not require any decision to be taken.
• The following algorithm is straight forward:
Input A
Input B
C=A+B
Output C
• Some problems require the use of decisions.
Decisions required for the solution
to a specified problem
Input A Input B Output Input A
Input B
Off Off Off
If A = 1 or B = 1 then
Off On On
Output 1
On Off On Else
On On On Output 0
End if
• Where as exception is a runtime error that might occur during the execution of your program.
• For example you are trying to login into your application and application throws an error for database
connectivity lost or something like Connection closed or license expired.
• These are exceptions which are unpredictable errors during runtime.
• Error handling is to put your logic to handle an expected error in your program, and Exception handling is to
handle an unexpected error thrown by application /program at runtime.
• A company has the following policy to calculate the end—of—year
bonus for its employees:
• If the employee has worked for the company for 9 months or more,
then the bonus equals to 30% of his/her monthly salary.
• If the employee has worked for the company for less than 9 months
and his/her salary is less than €2000 then the bonus equals to 20% of
his/her monthly salary.
• If the employee has worked for the company for less than 9 months
and his/her salary is equal or more than €2000 then the bonus equals
10% of his/her monthly salary.
MONTHS =0
SALARY =0
BONUS = 0
TOTAL = 0
SALARY = input("what is the salary of the employee?")
MONTHS = input(“How many months did he/she work?")
if MONTHS >= 9 then
BONUS = (30/100)*(SALARY)
TOTAL = BONUS +(SALARY * MONTHS)
else if MONTHS < 9 AND SALARY < 2000 then
BONUS = (20/100)*(SALARY)
TOTAL = BONUS+(SALARY * MONTHS)
else if MONTHS < 9 AND SALARY >= 2000 then
BONUS = (10/100)*(SALARY)
TOTAL = BONUS+(SALARY * MONTHS)
end if
output "TOTAL IS:" , TOTAL ,"Euros"
output "BONUS IS:" , BONUS ,"Euros"
Thinking concurrently
Parts of a solution that could be implemented
concurrently
• Concurrent means something that happens at the same time as something
else.
• In computer science, concurrent processing means the execution of different
instructions simultaneously by multiple processors so as to achieve the best
performance.
• A simplified explanation of this process is that programs are broken down
into procedures and procedures are broken down to sub-procedures.
• These are then assigned to separate processing units to perform
simultaneously.
• Sequential processing is the execution of all sub—procedures one after the
other by a single processor.
Decision to use concurrent
processing in solving a problem
• Concurrent processing requires better planning and coordination of resources.
• Without this, concurrent processing may cause serious problems and the decision to use serial or
concurrent processing in solving a problem should be carefully examined.
• For example, an accountant is making changes to an electronic financial database.
• As he is doing so, a second accountant opens the same database which includes all the
modifications made so far by the first accountant, and uses it to retrieve information.
• The first accountant then decides the changes made so far are invalid and brings the database
records to the previous condition.
• The first accountant saves the database.
• The information retrieved by the second accountant no longer exists, but the second accountant is
not aware of this crucial information.
• This problem could have been avoided if no one could read the altered database until the first
accountant decided that the edits and amendments were final
Evaluate the use of designing and developing
different parts of software products concurrently.
• different stages of programming run simultaneously (rather than
consecutively)
• decreases product development time
• improves productivity
• reduces costs
• requires more resources & more software developers
Thinking abstractly
Examples of abstraction
• Abstract thinking means reflecting on events, ideas, attributes and relationships in a general
manner that hides all unnecessary details of specific objects.
• All information, that is not necessary to accomplish a goal, is removed and ignored and a
generalization technique is implemented.
• The explanation of the various components of the motherboard requires the use of abstraction.
• Although the RAM and the CPU are considered as fundamental physical parts, it is known that
they are abstractions of gates and integrated circuits which contain millions of transistors.
• Decades ago, a programmer had to work with the low-level circuitry instructions of the CPU and
the computer used.
• Nowadays, high-level programming languages allow the user to use English like commands and
syntax in which one command statement corresponds to many machine instructions.
• In Java computer language, System.out.println displays a message on the screen.
• The user is not required to understand the function of the monitor and the various procedures,
interfaces, graphic card drivers and libraries used to create the corresponding pixels on the screen.
Why abstraction is necessary?
• Abstraction allows us to create a general idea of what the problem is and how to
solve it
• Abstraction removes all specific detail, and any patterns that will not help in solving
a problem.
• This helps in forming a “model” (If designers don’t abstract they may end upwith
the wrong solution to the problem they are trying to solve)
• Abstraction is widely used because there exist a number of “patterns” in
programming that keeps repeating in every application/program
• The pattern corresponding to an issue can be found, then the abstract solution to it
can be found and implemented, and the problem is solved
• Most programming languages provide some built-in abstract patterns, which are
easy to use (some API provides more advanced patterns)
Virtual machine and abstraction
• Virtual machines provide a software emulation/virtualization of other
operating systems;
• While hiding/abstracting the physical environment/OS/execution
environment of the host machine;
• Virtual machines do not directly correspond to any real platform so
software may be tested/executed on any platform (portability);
• This is more practical/economical than having to test software on
several computers with different OS;
• The software to be tested is compiled into the language of the virtual
machine, and this is interpreted into the language of the host machine;
Abstraction and computational
solutions for a specified situation
Object-oriented Programming
• Abstraction is the process of taking away or removing characteristics from something in order to reduce it
to a set of essential characteristics
• In object-oriented programming, abstraction is one of three central principles (along with encapsulation
and inheritance);
• Through the process of abstraction, a programmer hides all but the relevant data about an object in order
to reduce complexity and increase efficiency;
• The resulting object itself can be referred to as an abstraction, meaning a named entity made up of
selected attributes and behavior specific to a particular usage of the originating entity.
• Abstraction is related to both encapsulation and data hiding;
• Object-oriented programming uses programming objects that describe data (properties) and behavior
(methods) of real objects, and facilitates code reusability and abstraction.
• It makes complex software faster and easier to develop, and facilitates maintenance.
• It is an evolution of procedural (structural) programming, which uses procedures that are able to interact
and exchange data as building blocks of programs.
Object car1 Object car2
Data: Data:
integer speed = 0; String Colour = Black
String Equipment = Silver
integer gear = 1;
String Availability = True
Methods :
Integer Manufacturerstock = 0
changeGear Methods:
Accelerate changeColour
Brake changeEquipment
• The role of abstraction is profound; Availability
• the first object (car1) could be used in a program that simulates the driving experience of such a car,
• while the second (car2) could be used in a program suitable for sales purposes.
• In each case, the programmer hides the unnecessary details and concentrates only on properties and
behaviors that are important to the particular implementation.
• An object hides the details; Yet preserves the functionality;
• Objects combine abstractions of data and code; While hiding away implementation of details;
• Suppose a salesman uses a program that facilitates his/her job.
• When a customer chooses a car and finalizes the deal, a new object is
created.
• Each deal has some unique attributes.
• The salesman deals with a collection of items (cars).
• This collection is organized in a particular way to represent the sale of
cars.
• Some common operators could be applied to all items in the
collection (add a car, read details of a car etc.).
• So a collection is a data structure that consists of the data and the
predefined methods which operate on the data.
• A collection is an abstract data type like queues and stacks.
• An Abstract Data Type, or ADT, is a group of operations and data.
• In object oriented languages, a collection is an object that assembles
and contains a lot of elements in a single structure.
• A collection is used to add, store, manage, retrieve, manipulate and
communicate the data using predefined methods.
Modelling and simulation
• Mathematical modelling refers to a process where a system is understood well
enough and scientists describe it using mathematical language.
• A set of mathematical rules is used to describe the function of the particular system.
• It is clear that the mathematical model is an abstraction of the real system.
• A mathematical model contains only the necessary details, rules and objects for
studying the real system or an aspect of it.
• A mathematical model could be transformed into an algorithm and then to a
program that replicates the behavior of a real system.
• A computer simulation runs on a computer and reproduces the behavior of a real
system.
• The simulation uses an abstract mathematical model that is expressed as a computer
model (computer program) to simulate the system.
• A computer simulation is always based on a computer model.
Abstraction from a specified
situation
• A modular program is easier to understand and facilitate the use of abstraction.
• The programmer is able to focus on the important things and ignore all
unnecessary details.
• Procedures and sub-procedures facilitate abstraction.
• For example, the following program can call a sub—procedure named
computeSomething().
• When this sub-procedure is called from the main program, it executes a
sequence of instructions and returns a value.
• The programmer does not need to know the inner details of this sub-procedure,
they only need to know the name, parameters and the return data type.
• So: modularization facilitates abstraction.
• A sub-program is a named section of code that performs a specific
task (in a program) / can be called by name / referred by the identifier
when needed;
• without knowing the details (of code and data structures) as these are
wrapped / hidden within the sub-program;
Real-world and abstraction
• A thematic map is an abstraction of reality that shows the spatial
distribution and emphasizes a particular theme, such as the average
distribution of income in a specific geographic area.
• Topographic maps show abstractions of selected physical features of
the three—dimensional real world at a reduced scale in two-
dimensions, paper or a screen.
• Political maps are designed to show on data such as the boundaries
of countries and states and the location of major cities.
• These maps are an abstraction of political territory.
• In these cases, various levels of abstraction occur.
Read the algorithm and state the
sequence of numbers that it
produces.
• (b) Draw a flowchart for an algorithm that ADDS UP the following
numbers and outputs the total.
• 2 + 4 + 8 + 16 + 32 + 64 + …. + 65536
• An array, numbers[], has
been declared and contains
the following values.
• 49 11 16 72 2 37 14 26
• Consider the following
code fragment.
• All variables have been
declared.
• (a) By using a trace table, determine the values of h and l after the c
loop has been completed (for all values of c) for the first time.
• (b) Deduce the purpose of this loop.
• (c) Copy and complete the following trace table for the whole
algorithm.
i j l h Numbers[]
• From the above table we can state that the algorithm with complexity
O(N2) is better than the one with complexity O(2N).
O(1)
• O(1) describes an algorithm that will always execute in constant time
(or space) regardless of the size of the input data.
O(N)
• O(N) describes an algorithm whose performance will grow
linearly and in direct proportion to the size of the input data
set.
• Example – Linear Search
• The example below also demonstrates how Big O favours the
worst-case performance scenario
• a match could be found during any iteration of the for loop and
the function would return early, but Big O notation will always
assume the upper limit where the algorithm will perform the
maximum number of iterations.
O(log N)
• Binary search is a technique used to search sorted data sets.
• This type of algorithm is described as O(log N).
• The iterative halving of data sets described in the binary search
example produces a growth curve that peaks at the beginning and
slowly flattens out as the size of the data sets increase e.g. an input
data set containing 10 items takes one second to complete, a data set
containing 100 items takes two seconds, and a data set containing
1000 items will take three seconds.
A Comparison of n and log n
106
O(N2)
• O(N2) represents an algorithm whose performance is directly
proportional to the square of the size of the input data set.
• This is common with algorithms that involve nested iterations over
the data set.
• Example – Bubble Sort, Selection sort
• Deeper nested iterations will result in O(N3), O(N4) etc.
Comparison of n and n2
108
Fundamental Operations of a
computer
• Computers normally have simple operations
• Add, compare, store and retrieve
• These operations cannot be broken down any further
• Conditional instructions and Boolean operations (AND, OR, XOR, NOT)
• Data handling and memory operations
•Set a register to a fixed constant value.
•Copy data from a memory location to a register, or vice versa
• Arithmetic and logic operations
• Add, subtract, multiply, or divide the values of two registers, placing the result in a register
• Perform bitwise operations
• Compare two values in registers
Complex / Compound Operations
• Complex operations are a combination of the basic operations.
• Multiple compare / add / store / retrieve instructions are used to
accomplish a given task (for example, finding the max of n given
numbers)
• Programming languages build on the basic operations to complete
complex tasks.
• Searching, Sorting, Data modification
Essential features of a programming
language
• Consistent grammar(semantics) and syntax
• Syntax – related to the structure of a command
• Semantics – defines the meaning of every command possible in the language