G11 Topic 4 - Computational Thinking (4)
G11 Topic 4 - Computational Thinking (4)
Thinking
Topic 4
Problem
A problem is a situation that needs attention and needs to be dealt with or solved.
Whenever we deal with problems, we have to take one or more decisions.
Decision making process
Algorithm
An algorithm is a series of unambiguous instructions designed in order to solve a problem and
achieve a certain goal in a finite number of steps.
According to Knuth (1968) an algorithm must possess the following properties” :
● Finiteness: "An algorithm must always terminate after a finite number of steps a very finite
number, a reasonable number"
● Definiteness: "Each step of an algorithm must be precisely defined; the actions to be carried
out must be rigorously and unambiguously specified for each case"
● Input: "...quantities which are given to it initially before the algorithm begins. These 0 inputs are
taken from specified sets of objects"
● Output: "...quantities which have a specified relation to the inputs“
● Effectiveness: "... all of the operations to be performed in the algorithm must be sufficiently
basic that they can in principle be done exactly and in a finite length of time by a man using
paper and pencil"
Algorithm
An algorithm may be expressed in a number of ways:
● Simple English (natural language)
● Flowchart:
○ A formalized graphic representation method which avoids most issues of ambiguity.
● Pseudocode:
○ is a generic artificial language that describes computer algorithms but does not use the
syntax of any particular programming language.
● Programming languages:
○ is an artificial language designed in such a way so that it may be used by humans in order to
communicate with a computer system.
Pseudocode
○ The main purpose of pseudocode is to help programmers develop computer programs.
○ Because pseudocode is written for humans the syntax used is not as strict as the one used in
computer languages.
○ Pseudocode normally omits details that are not important for human understanding of the
algorithm. Computers can’t interpret a solution in pseudocode form thus it should be
converted to a computer language. When a programmer develops pseudocode he/she does
not necessarily think of a particular computer language.
○ Normally a pseudocode could be converted to any computer language with relative ease. It
is important to understand that writing pseudocode involves paper and pencil.
Computer program
A computer program is a sequence of instructions, written to instruct a computer to
perform a specified task.
General principles
○ Thinking Procedurally:
○ This refers to breaking down a task into a series of steps or instructions. When
thinking procedurally, you focus on the specific sequence of actions needed to
complete a task. It's about following a set process in a structured way to reach a solution.
In programming, this could mean writing out the steps in an algorithm or designing a
flowchart to represent the process.
○ Thinking Logically:
○ Logical thinking is about using reasoning to make decisions, solve problems, or arrive
at conclusions. In computer science, this involves applying logical operations and reasoning
to ensure that an algorithm or program works correctly and efficiently.
○ For example, conditional statements (like if, else, and switch) rely on logical thinking to
determine which path to follow based on a condition.
General principles
○ Thinking Ahead:
○ Thinking ahead involves anticipating potential problems or considering the future
steps of a solution before you act. This could involve planning out algorithms and
programs to avoid errors and inefficiencies.
○ In software development, thinking ahead might involve considering edge cases, handling
exceptions, or predicting how a program might behave with different inputs.
○ Thinking Concurrently:
○ Thinking concurrently refers to managing multiple processes at the same time. In
computer science, this can relate to multitasking, parallel computing, or dealing with multiple
threads or processes running simultaneously.
○ For example, when developing a program with multiple users interacting at once, thinking
concurrently allows you to design systems that handle these interactions without conflict or
delay.
General principles
○ Thinking Abstractly:
○ Abstract thinking is the ability to focus on the high-level concept rather than the
detailed implementation. In computer science, this means dealing with ideas or structures
that are not directly tied to specific examples or concrete details.
○ For example, when designing a class in object-oriented programming, you may think
abstractly about its properties and behaviors, without worrying about the specifics of how it
will be implemented or used right away.
○ Thinking Recursively:
○ Recursive thinking involves solving a problem by breaking it down into smaller instances of
the same problem. It’s a method of problem-solving where a function calls itself to handle
sub-problems.
○ In computer science, recursion is used in algorithms like sorting or searching, where a
function solves a problem by repeatedly calling itself until a base case is reached.
Fundamentals operation
○ Fundamental Operations
○ These are the basic, low-level operations that a computer can perform directly. They are the
building blocks of more complex actions.
○ Examples include:
■ Addition, subtraction (arithmetic operations)
■ Comparison (e.g., greater than, equal to)
■ Data movement (e.g., moving data between memory and registers)
■ Logical operations (AND, OR, NOT)
Compound operation
Compound Operations
These are combinations of fundamental operations that perform more complex tasks.
They are often created to simplify programming and improve readability.
Examples include:
● Calculating the average of a list of numbers
● Searching or sorting a list
● Copying files or folders
}
}
Concatenation
public class ConcatenationExample {
public static void main(String[] args) {
// Declare two strings
String firstName = "John";
String lastName = "Doe";
}
}
Array
○ A set of data values of the same type, stored in a sequence in a computer program.
○ Also known as a list
Pre-planning in a suggested problem and solution
○ Pre-planning is the process of planning in advance.
○ Prefetching in broad terms means getting data or instructions from memory into the cache
before they are actually needed.
■ When a program requests data that was previously prefetched, it can use the prefetched data
and continue with execution, instead of waiting for the data from RAM.
○ This is a typical example of preplanning an action so as to save time and improve efficiency.
Gantt Chart
○ A Gantt chart is a type of bar chart, named after Henry Gantt. It is widely used for project
schedule and project management, as a way of showing activities, tasks and events against
time.
○ On the left of the chart is a list of the tasks, activities and events. Along the top is an
appropriate time scale.
○ A” tasks, activities and events are represented by bars. Each bar represents the duration,
start day and end day of the task, activity or event.
○ A Gantt chart allows easy inspection of the project’s activities, overlapping activities,
the total duration of the project etc.
Pre-conditions and post-conditions
○ Pre-conditions are the things that must be true before a method is called. The method tells
clients "this is what I expect from you".
○ Post-conditions are the things that must be true after the method is complete. The method
tells clients "this is what I promise to do for you".
Subroutine
○ Subroutine is a sequence of program instructions that performs a specific task, packaged
as a unit. It may return one or more values, but does not have to.
○ In different programming languages, a subroutine may be called a procedure, a function, a
routine, a method, or a subprogram.
○ The generic term 'callable unit' is sometimes used.
○ It may be easier to think of them as mini-programs within a large program.
○ Subroutines consist of modules of code that perform different tasks.
○ If these tasks are repeated throughout the program, they can be written as subroutines.
○ Each subroutine is given a unique name so that it can be called and executed quickly
throughout the program, without having to write the code again.
○ This reduces the size of the code, making the program more logical and easier to read and
maintain.
Subroutine
Subroutine advantages
○ Breaking down or decomposing a complex programming task into smaller sub-tasks and
writing each of these as subroutines, makes the problem easier to solve.
○ Subroutines can be used several times within a program.
○ It saves the programmer time as it reduces the amount of code that needs to be
written or amended by allowing you to reuse code without having to write it again.
○ If you are working as part of a team you can divide a large program into smaller
sections and allow individuals to simultaneously work on those sections.
○ It makes the code easier to read if you use sensible subroutine labels as the headings
tell the reader what that section of code is doing.
○ By reducing the amount of repeating tasks you also reduce the risk of introducing errors
in a program.
○ Easy to maintain as each subroutine can be tested separately.
Methods, Function and procedures
○ A method in Java is a subroutine that is part of a class.
○ The subroutine is like a miniature program that can execute in other parts of the program.
Methods promote code reuse and maintainability.
○ There are two different types of subroutines that mainly use:
● Functions.
● Procedures
○ Functions return values back to the main program.
○ Procedures do not return a value back to the main program.
○ A function would return the returning value/control to the code or calling function.
The procedures perform certain tasks in a particular order on the basis of the given
inputs.
Functions
Procedures
Exception
○ Exception is an act or event that disrupts the anticipated flow of program’s execution.
○ The exceptions take place during the execution of the program and can be effectively
handled by specific mechanisms that most modern programming languages provide.
○ An exception is an event that disrupts the normal flow of execution of a program. It
typically occurs due to an error (like dividing by zero or accessing an invalid array
index) or other exceptional conditions that the program should handle.
○ Occur within the program itself due to certain errors or exceptional conditions during
the program's execution.
○ These errors arise from the logic of the program, such as:
■ Dividing by zero
■ Trying to access an index in an array that doesn't exist
■ Null references
■ File not found (in file operations)
Concurrent
○ 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.
Abstract Thinking
○ 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.
○ Abstraction is used to hide background details or any unnecessary implementation
about the data so that users only see the required information. It is one of the most
important and essential features of object-oriented programming.
○ The user is only given access to the details that are relevant. Vehicle operations or ATM
operations are classic examples of abstractions in the real world.
Compiler
● A compiler is a translator that executes the translation process only once. It
normally translates the whole source program into the object program.
● The object program is saved and next time a programmer wants to use the object
program no recompilation is necessary.
● Compilers issue error messages for all syntax error found and all errors are
communicated to the programmer after the entire program is checked.
● The compilation ends only when all syntax errors have been corrected. Compilers are
much faster than interpreters.
● Examples: C, C++.
Interpreter
● An interpreter is a translator that goes through the process of translation every
time the program is run.
● Interpretation refers to the process of reading each line (instruction) of the source
program, analyzing it, translating it into the corresponding line of the object
program and executing the line.
● Syntax errors are communicated to the programmer for every instruction that is
interpreted. Example: BASIC.
Interpreter and Compiler
● Java combines compilation and interpretation. Source code is compiled to Java
Virtual Machine bytecode.
● When a Java program is compiled from .java file to a .class file, the class file is Java
Virtual Machine bytecode.
● This Java Virtual Machine bytecode can be interpreted by the Java Virtual Machine
interpreter.
● The Java architecture allows code to run on any machine to which the Java Virtual
Machine interpreter has been installed.
● In Java architecture, all details of making the code function on a specific hardware
platform are handled by the Java Virtual Machine (JVM).
Object-oriented programming
● Object-oriented programming uses abstraction, and is based on the principle that all
everyday tasks éan be considered as entities. These entities are either objects or
events.
● 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.
Collection
○ A collection is a data structure that consists of the data and the predefined methods
which operate on the data.
○ A collection in computer science is an abstract data type like queue and stacks.
○ In programming, a collection is a class used to represent a set of similar data type
items as a single unit. These unit classes are used for grouping and managing related
objects.
Collection advantages:
○ The methods of collections are predefined algorithms that the programmer can immediately
use
○ Performance is increased by the data management capabilities provided by the collection
○ Software reuse is facilitated because the use of methods is based on a common language
and a standard interface
Maps
○ 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.
○ 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.
Parallel Array
○ Parallel array also known as structure of an array, multiple arrays of the same size such
that i-th element of each array is closely related and all i-th elements together represent
an object or entity.
○ An example parallel array is two arrays that represent x and y coordinates of n points.
○ Two of the most essential applications performs on an array or a record are searching and
sorting.
○ Examples:
■ first_name = ['Bones', 'Welma', 'Frank', 'Han', 'Jack']
■ last_name = ['Smith', 'Seger', 'Mathers', 'Solo', 'Jackles']
■ height = [169, 158, 201, 183, 172]
Parallel Array - Searching
○ Each index in a parallel array corresponds to the data belonging to the same entity in
a record.
○ Thus, for searching an entity based on a specific value of an attribute, e.g. we need to find
the name of a person having height >200 cms in the above example.
○ Thus, we search for the index in the height array having value greater than 200. Now,
once we have obtained the index, we print the values of the index in the first_name
and last_name arrays. This way searching becomes an easy task in parallel array.
Parallel Array - Sorting
○ Now, using the same fact that each index corresponds to data items in different arrays
corresponding to the same entity.
○ Thus, we sort all arrays based on the same criteria.
○ For example, in the above-displayed example, we need to sort the people in increasing order
of their respective heights. Thus, when we swap the two heights, we even swap the
corresponding values in other arrays using the same index.
○ The arrays can be sorted in any way, numerical, alphabetical or any other way but the
elements are placed at equally spaced addresses. Any of the sorting techniques can be
used to sort the record based on a specified criteria.
Arrays of objects
○ An array of objects is an array of reference variables. Each reference variable is an
element of the array and it’s a reference to an object.
○ Suppose a programmer wants to create an array of vehicle objects named A.
■ Each vehicle object has the following data fields: Colour, Type and Engine.
○ The programmer wants to construct an array of vehicle objects with the following
objects:
■ Vehicle1[Colour: "red”—Type: ”car”-Engine:2000]
■ VehicIe2 [Colour: ”green”-Type: ”bus”-Engine:4000]
■ Vehicle3 [Colour: "blue"-Type: ”motorcycle”-Engine:800]
One dimensional array
○ One-dimensional array in Java programming is an array with a bunch of values having
been declared with a single index.
Two dimensional array
○ A two-dimensional array is an
array which technically has
one row of elements,
however, each row has a
bunch of elements defined by
itself.
○ Suppose we were to search for the value 2 in this list. The search would start at position 0 and
check the value held there, in this case 3. 3 does not match 2, so we move on to the next
position.
○ The value at position 1 is 5. 5 does not match 2, so we move on to the next position.
○ The value at position 2 is 2 - a match. The search ends.
○ A linear search can be quite inefficient. Suppose the data set contained 100 items of data, and the
item searched for happens to be the last item in the set. All of the previous 99 items would have to
be searched through first.
○ However, linear searches have the advantage that they will work on any data set, whether it is
ordered or unordered.
Linear Search
○ A linear search in pseudo-code might look like this:
Binary Search
○ Binary search (or half interval search) algorithm is a searching method used only in
sorted arrays.
○ It relies on divide and conquer strategy to accomplish its purpose. In every search
iteration, half of the elements of the array are eliminated as possible solutions.
○
○ Binary search is very efficient for large arrays. Its performance makes it ideal when resorting
is not required.
Binary Search
○ A binary search is an efficient method of searching an ordered list. It will not work on a
list that has not been sorted first.
In Big O notation, we express the complexity in terms of the size of the input (denoted by
n), but we don’t specify the actual number (like 10, 100, etc.). We just express it in general
terms as O(n), O(1), O(n²), etc., to indicate the growth rate of the algorithm.
Big O-notation