Introduction To Programming Latest
Introduction To Programming Latest
COMPUTATIONAL THINKING
Introduction to programming
4.2.7 Suggest suitable algorithms to solve a specific problem
Efficiency of an algorithm refers to the amount of the computer resources required to perform its functions.
Minimizing the use of various resources such as the CPU and computer’s memory is very important.
Correctness of an algorithm refers to the extent to which the algorithm satisfies its specification, is free from faults,
and fulfils all objectives set during the design and implementation phase.
Reliability refers to the capability of the algorithm to maintain a predefined level of performance and perform all
required functions under stated conditions, having a long mean time between failures.
Flexibility of an algorithm refers to the effort required to modify the algorithm for other purposes than those for
which it was initially developed.
Connecting computational thinking and
4.2.8 Deduce the efficiency of an algorithm in the context of its use
• When we say an algorithm is O(n) what is meant is that the growth rate of the instructions in this particular
algorithm shall be executed n times.
Connecting computational thinking and
4.2.8 Deduce the efficiency of an algorithm in the context of its use
The following algorithms finds the sum of numbers 1+2+3+4+5=15 when n=5. If n=7 the algorithms will calculate
1+2+3+4+5+6+7 and the output will be 28. Which algorithm is more efficient?
Connecting computational thinking and
4.2.8 Deduce the efficiency of an algorithm in the context of its use
The first algorithm does not use a loop (constant time requirement). This algorithm requires time proportional to 1,
and its Big O notation is O(1). The time requirement is constant, and is independent of the problem’s size. For this
particular algorithm, it is independent of the size of n.
The second algorithm uses one loop (linear algorithm-linear time required). This algorithm requires time
proportional to n and its Big O notation is 0(n). The time requirement for this algorithm increases directly with the
size of the problem. For this particular algorithm, it is dependent on the size of n.
The third algorithm use two loops the second inside the first (nested loops (quadratic algorithm-quadratic time
required)). This algorithm requires time proportional to n2 and its Big O notation is 0(n2). The time requirement for
this algorithm increases rapidly depending on the size of the problem. For this particular algorithm it is dependent of
the size of n.
Suppose the first algorithm takes 1 time unit to output the result for n=10.
The second algorithm will take 10 time units and the third 102 time units.
For n=100 the first algorithm takes 1 time units to output the result, the second algorithm will take 100 time units
and the third 1002 time units.
Connecting computational thinking and
4.2.8 Deduce the efficiency of an
algorithm in the context of its use
O(n)
Connecting computational thinking and
4.2.8 Deduce the efficiency of an
algorithm in the context of its use
where Constant depends on the computer system that runs the program. For the same computer system Constant
should always be considered to be the same.
The following table shows some useful common complexities.
Connecting computational thinking and
4.2.8 Deduce the efficiency of an algorithm in the context of its use
A flag in programming is a variable used to indicate a condition. When the condition is changed the value of the flag
is changed. The flag is usually a Boolean variable and is used to terminate a loop.
Lists in computer science are used to store a sequence of values under a single name. Lists allow duplicates to act as
containers and to be typically implemented either as linked lists or as arrays.
Connecting computational thinking and
4.2.9 Determine number of iterations for given input data
Int Temp in reference to the bubble sorting algorithm, for the array G
Int J
Int I = 0 G={50,20,30,10}
Loop while I < size -1
J = 0 After how many iterations will the number (50) be sorted?
Loop while J < size-I-2
It will be sorted after 1 iteration of I,I will loop once
If G[J] > G[J + 1] then
While J will loop three iterations 02
Temp = G[J]
G[J] = G[J + 1] After how many iterations will the number (30) be sorted?
G[J + 1] = Temp
End If It will be sorted after 2 iterations of I,I will loop twice
J=J+1 While J will loop two iteration, 01
end loop
I=I+1
end loop
Connecting computational thinking and
4.2.9 Determine number of iterations for given input data
Int I=0
Int J in reference to the selection sorting algorithm, for the array E
Int min
Int Temp G={90,80,30,10}
Loop while I <= size -1 After how many iterations will the number (10) be sorted?
min = I
J=I+1 It will be sorted after 1 iteration of I,I will loop once
Loop while J <= size -1
If E[J] < E[min]
While J will loop three iterations 13
min = J
End-If
J=J+1
After how many iterations will the number (80) be sorted?
End loop
Temp = E[I] It will be sorted after 2 iterations of I,I will loop twice
E[I] = E[min]
E[min]= Temp While J will loop two iteration, 23
I=I+1
End loop
Nature of the programming language
4.3.1 the fundamental operations of a computer
A programming language is described as the combination of its semantics, which refers to the meaning of every
construction that is possible in the language and its syntax, which relates to its structure.
Grammar is a meta-language that is used to define the syntax of a language, while the rules of statement
construction are called syntax.
Each high-level language (JAVA, C++, Object oriented) has a unique syntax and a specific limited vocabulary.
The words import, class, int etc. in Java are reserved keywords with special meanings in the Java language.
A command written in a computer language always means the same and there is no ambiguity of meaning.
Nature of the programming language
4.3.4 Explain the need for higher level languages
Computers can only process 0s and 1s. Each computer has its own machine language, which is made up of 0s and 1s.
Machine language is a low-level language and is the only language that can be understood directly by a computer.
Machine language programs are hard to write, difficult to debug and to maintain.
A machine language programmer has to keep track of memory locations and write from the very beginning all the
mathematical functions required by a program.
Machine language programs written for a computer of one kind are not suitable for a computer of another kind.
The next evolutionary step in programming came with the replacement of binary code for development of instructions
and reference to address locations with symbols called mnemonics, These low-level computer languages were referred
as mnemonic and later as assembly languages.
An assembler was used to convert the assembly language mnemonics to machine code. The development of computer
programs was now much easier, but each assembly language was still specific to a specific computer system. The lack of
abstraction, the need to focus on problem—solving and to improve efficiency led to the development of
high—level languages.
A high-level programming language is a programming language that uses elements of natural language, is easy to use,
facilitates abstraction by hiding significant areas of computing systems, and makes the program development simpler,
faster and more understandable.
Nature of the programming language
4.3.5 Outline the need for a translation process from a higher—level language to machine-executable code
Most applications today are written in one of the high-level languages. To execute (RUN) the program on a computer
system and get the required job done, a translation method is required. This translation process will convert the
program into the machine language of the computer on which it will run.
The original program developed in a selected high-level language is called the source program or source code.
The translated program in machine language is called the object program or target program.
1. 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++.
2. 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, Python, MATLAB.
3. 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. all details of making the code function on a specific hardware platform are handled by the Java Virtual
Machine (JVM).
Use of the programming language
4.3.6 Define the terms Variable, constant, operator, object
Variable: A variable is used to store a data element of a program. The stored value can be changed during the program
execution. A variable has a name (or identifier) and a type. The name of the variable should not clash with reserved keywords
and literals of the language. It is highly recommended to use meaningful variable names (e.g. roomsize).The type of the
variable could be an integer, double, string etc. A variable in most programming languages
can only store a data element of the particular type.
Constant: Constants represent things and quantities that don’t change. They can be seen as non-modifiable variables. The
data element stored in a constant cannot be modified during the execution of the program
(e.g. in Java: final double PI = 3 . 14159).
Operator: Operators are used to manipulate operands. The expression 2+3 has as an operator the ”+" (sign of addition) and
two operands ”2”, ”3”. Operands can be variables, literals(int, float, double), Boolean values, numerical values, text etc.
Operators can be arithmetic, relational, logical etc.
Object: An object is a comprised of data and actions. Actions refer to the operations that can be performed by the object.
Object data may include a number of data members (arrays, collections), while actions may also be referred to as methods.
Data members are used to store the current state of an object and methods are used to change or access those data
members.
Use of the programming language
4.3.6 Define various operators
= Defined as ”is equals to”. It is also used to assign a value to a variable. Min=6 means that the value of 6 is assigned to
variable Min
≠ Defined as ”not equal to”. Min ≠ Max means that Min is not equal to Max
mod Defined as “modulo operation”, it calculates the remainder of division of one number by another
Use of the programming language
4.3.10 Describe the characteristics and applications of a collection
Frequently in computer programming there is a need to group and store data that will be used for the problem solution.
A collection or container is consisted of zero or more elements such as objects and values and it is equipped with the
necessary operations to handle data.
Collections allow duplicate elements and may contain ordered and unordered data elements.
Each type of collection is equipped with its own specific operations. The elements of a particular collection are typically
all of the same type and represent an entity. A fixed-size array is usually not considered a collection because it holds a
fixed number of items.
Use of the programming language
4.3.12 Discuss the importance of sub-programs and collections within programmed solutions.
A sub-program is a unit that contains a sequence of computer instructions that perform a specific and predefined task.
Code reuse is very useful because it allows programmers to take advantage of existing code and solutions developed by other
programmers (or by themselves) to speed up their tasks. Code reuse saves time and resources and allows the completion of demanding
projects in the shortest period of time.
Authors of new programs can take advantage of software libraries, which contain subprograms that can be used by different types of
programs
1. Construct the pseudocode for the procedure Average, that takes an input number of grades and it returns the
average, for the array students