Computer Programming 1
Computer Programming 1
Learning Outcome 1:
Understand the differences between
common programming languages
DEFINITIONS
Computer program
Computer programmer
Computer programming
Common programming languages
Java
C
C++
Python
PHP
Perl
Ruby
Computer program
A computer program is a sequence of instructions
written using a Computer Programming Language to
perform a specified task by the computer.
OR
A Computer program is a sequence of symbols that
specifies a computation.
The two important terms that we have used in the
above definition are:
1. Sequence of instructions
2. Computer Programming Language
Computer Programmer
Computer programmer is Someone who can write
computer programs or in other words, someone
who can do computer programming.
Computer programming(Language)
A Computer programming language is an artificial language
designed to communicate instructions to a machine,
particularly a computer.
Programming languages can be used to create programs
that control the behaviour of a machine and/or to express
algorithms.
Any programming language is composed of a set of
predefined words that are combined according to
predefined rules (syntax) to generate a computer program.
The concepts of parsing and
tokenisation, byte-code, assembly
language, source code, machine
code
The concepts of parsing and
tokenisation
Parsing
Parsing is a frequently used term both in the realm of data
quality, and in computing in general.
It can mean anything from simply 'breaking up data' to full
Natural Language Parsing (NLP), which uses sophisticated artificial
intelligence to allow computers to 'understand' human language.
Parsing is designed to be used by developers of data quality
processes to create packaged parsers for the understanding and
transformation of specific types of data –
For example Names data, Address data, or Product Descriptions.
Tokenisation
Tokenization is the act of breaking up a sequence of strings
into pieces such as words, keywords, phrases, symbols and
other elements called tokens.
Tokenization is used in computer science, where it plays a
large part in the process of lexical analysis.
In the process of tokenization, some characters like
punctuation marks are discarded. The tokens become the
input for another process like parsing and text mining.
Tokens themselves can also be separators. For example, in
most programming languages, identifiers can be placed
together with arithmetic operators without white spaces.
A token is a piece of data that is recognized as a unit by the
Parse processor using rules
Byte-code
Bytecode are a type of programming language that fall under the
categories of both compiled and interpreted languages because they
employ both compilation and interpretation to execute code.
Java and the .Net framework are easily the most common examples of
bytecode languages.
In a bytecode language, the first step is to compile the current program
from its human-readable language into bytecode.
Bytecode is a form of instruction set that is designed to be efficiently
executed by an interpreter and is composed of compact numeric codes,
constants, and memory references.
The largest benefit of bytecode languages is platform independence
which is typically only available to interpreted languages,
Bytecode can also make use of this technique to enhance execution speed
Assembly language,
Assembly language is a low-level programming language for
a computer, or other programmable device, in which there is a
very strong correspondence between the language and
the architecture's machine code instructions.
Assembly language is converted into executable machine code
by a utility program referred to as an assembler
Assembly Language introduced a mnemonic representation,
using symbols such as ADD, MOV or POP to use rather than
the binary codes.
Source code
Source code is any collection of computer instructions, possibly
with comments, written using a human-readable programming
language, usually as plain text (i.e., human readable alphanumeric
characters).
The source code of a program is specially designed to facilitate the
work of computer programmers, who specify the actions to be
performed by a computer mostly by writing source code.
The source code is often transformed by
an assembler or compiler into binary machine code understood by
the computer.
The machine code might then be stored for execution at a later
time. Alternatively, source code may be interpreted and thus
immediately executed.
Machine code
Machine code or machine language is a set
of instructions executed directly by a computer's central
processing unit (CPU).
The CPU loads in one instruction at a time, and executes that
instruction. The next instruction in sequence is then loaded in.
Each instruction is represented as a fixed number of bits (in early
days, 8 bits were used, although currently 64 bit instructions are
common).
The first 4 bits represented the operator (
• e.g., 00 for NOOP, 01 for ADD, 02 for MOV, etc.
The remaining 4 bits represent the data to operate on.
• e.g. 01 01 Add 1 to the current sum.
The role of the compiler in
terms of input, output, and
intermediate representations
A compiler is a special program that processes statements written in a
particular programming language and turns them into machine language or
"code" that a computer's processor uses.
Typically, a programmer writes language statements in a language such
as Pascal or C one line at a time using an editor.
Traditionally, the output of the compilation has been called object code or
sometimes an object module.
The object code is machine code that the processor can execute one
instruction at a time.
The programmer then runs the appropriate language compiler, specifying
the name of the file that contains the source statements.
When executing (running), the compiler first parses (or analyzes) from the
input and then process the code to produce output for immediate
presentations all of the language statements syntactically one after the
other and then, in one or more successive stages or "passes", builds the
output code, making sure that statements that refer to other statements are
referred to correctly in the final code
The role of an interpreter
Definition
An interpreter is a computer program that is used
to directly execute program instructions written
using one of the many high-level programming
languages.
Interpreter transforms or interprets a high-level
programming code into code that can be
understood by the machine (machine code).
The interpreter reads each statement of code and
then converts or executes it directly
An interpreter generally uses one of the following strategies for
program execution:
1. parse the source code and perform its behavior directly;
2. translate source code into some efficient intermediate
representation and immediately execute this;
3. explicitly execute stored precompiled code[1] made by
a compiler which is part of the interpreter system.
The role of a virtual machine (such as the Java Virtual
Machine)
Definition
Virtual Machine is a software to emulate the other computer system so
that our computer can behave like that system.
The process of creating and running a virtual machine is known as
virtualisation.
Virtualisation software, known as an emulator
The emulator translates the instructions of the guest computer so that the
host computer can understand them.
The emulator also provides a bridge that allows the virtual machine to use
the user interface, via the host operating system (I/O – input/output),
allowing the user to interact with the software running on the virtual
machine.
Example of Virtual Machine
The Java Virtual Machine
The Java Virtual Machine is a virtual machine
that reads and translates byte code into different
machine codes for many different processor
architectures and operating systems.
This means that Java programs can be run on
any computer that has this virtual machine,
including desktop PCs, laptops, tablets,
smartphones and smart TVs.
Diagram show how Java virtual works
Benefits and limitations of
virtual machines
Using virtual machines brings several benefits:
They allow modern systems to use programs that run on
obsolete or unsupported platforms
They allow platform-independent programs to run on multiple
platforms
They allow one computer to behave as several computers
simultaneously
They can be easily backed up, copied and re-installed. This has
particular benefits in corporate environments where network
downtime can lead to loss of revenue. The virtual machines can
be installed on another computer and be up and running again
quickly.
Virtual machines also have limitations:
They emulate (copy) software, not hardware. Users trying to get the
full functionality from software for another platform may find that
they still cannot use the software correctly. For example, an
emulation of a driving video game that requires a steering wheel
might not be able to operate the game correctly
Emulation requires processing power and memory. Running a virtual
machine may take a substantial amount of these resources, resulting
in reduced performance from the host computer
If the host system lacks sufficient resources, the virtual machine will
also suffer from poor performance and be difficult to use.
Different methods of memory allocation
in computer programming
Memory Allocation
Definition - What does Memory Allocation mean?
Memory allocation is a process by which computer programs and
services are assigned with physical or virtual memory space.
Memory allocation is the process of reserving a partial or complete
portion of computer memory for the execution of programs and
processes. Memory allocation is achieved through a process known
as memory management.
Memory allocation is primarily a computer hardware operation but is
managed through operating system and software applications.
Memory allocation process is quite similar in physical and virtual
memory management.
Programs and services are assigned with a specific memory as per
their requirements when they are executed.
Once the program has finished its operation or is idle, the memory is
released and allocated to another program or merged within the
primary memory.
Memory allocation has two core types;
#include <stdio.h>
#include <conio.h>
int main() {
printf("Storage size for int : %d \n", sizeof(int));
getch();
}
A program to find a leap year
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
printf("Enter the year\n");
scanf("%d", & year);
if ((year%4)==0)
{
printf("Leap year");
}
else
{
printf("is not a year");
}
getch();
}
Booleans
The Boolean type represents the values true and false.
Although only two values are possible, they are rarely
implemented as a single binary digit for efficiency reasons.
Many programming languages do not have an explicit Boolean
type, instead interpreting (for instance) 0 as false and other
values as true.
Boolean data simply refers to the logical structure of how the
language is interpreted to the machine language.
In this case a Boolean 0 refers to the logic False. True is always a
non zero, especially a one which is known as Boolean 1.
Floating point type
Floating types are used to store real numbers.
Size and range of floating type on 16-bit machine
Type Size(bytes) Range
data-type variable-name[size];
/* Example of array declaration */
int arr[10];
Here int is the data type, arr is the name of the array and
10 is the size of array.
It means array arr can only contain 10 elements of int
type.
Initialization of an Array
After an array is declared it must be initialized.
Otherwise, it will contain garbage value(any random
value).
An array can be initialized at either compile time or
at runtime.
The general form of initialization of array is,
data-type array-name[size] = { list of values };
/* Here are a few examples */
int marks[4]={ 67, 87, 56, 77 }; // integer
array initialization
float area[5]={ 23.4, 6.8, 5.5 }; // float
array initialization
int marks[4]={ 67, 87, 56, 77, 59 };
// Compile time error
Example of array initialization
#include<stdio.h>
#include<conio.h>
void main()
{
int i; int arr[] = {2, 3, 4}; // Compile time array initialization
for(i = 0 ; i < 3 ; i++)
{
printf("%d\t",arr[i]);
}
}
Output: 234
Types of Array
One dimensional Arrays
Two dimensional Arrays
Multi dimensional Arrays
One dimensional Arrays
It is otherwise called as vector.
It is a list item contains or consists only one variable name.
Example
Declaration
Type variable name [row size] [column size]
Example
Int marks [3] [3]
Column size
Data Array Row size
type name
Multi dimensional Arrays
It is a list item consist of three or more
dimensions
Linked lists
A linked list is a linear data structure that consist of a
sequence of data records.
Such that each records there is a field that contains a
reference to the next in the sequence.
Linked list can be singly, doubly or multiply linked and
can either be linear or circular.
Basic Properties
Objects, called nodes, are linked in a linear
sequence
A reference to the first node of the list is always
kept. This is called the 'head' or 'front'
Example of link lists
.
A B c X Null
multiply linked list (circular linked list
It is a list data structure in which last node point
to first node
3 3 20
Dictionaries/Maps
Map or a Dictionary) is an abstract data type
composed of a collection of (key,value) pairs, such
that each possible key appears at most once in the
collection.
Operations associated with this data allow :
the addition of pairs (key, value) to the collection,
the removal of pairs from the collection,
the modification of the values of existing pairs, and
the lookup of the value associated with a particular key
Map Operations
The operations that are usually defined for an associative array are;
Add or insert: add a new (key,value) pair to the collection, binding the new key to its
new value. The arguments
to this operation are the key and the value.
• Reassign: replace the value in one of the (key,value) pairs that are already in the
collection, binding an old key to
a new value. As with an insertion, the arguments to this operation are the key and the
value.
• Remove or delete: remove a (key,value) pair from the collection, unbinding a given key
from its value. The
argument to this operation is the key.
• Lookup: find the value (if any) that is bound to a given key. The argument to this
operation is the key, and the
value is returned from the operation. If no value is found, some associative array
implementations raise an
exception.
Map Implementation
For dictionaries/map with very small numbers of bindings, it may make
sense to implement the dictionary using an association list, a linked list of
bindings.
With this implementation, the time to perform the basic dictionary
operations is linear in the total number of bindings; however, it is easy to
implement and the constant factors in its running time are small.
Another very simple implementation technique, usable when the keys are
restricted to a narrow range of integers, is direct addressing into an array:
The most frequently used general purpose implementation of an
associative array is with a hash table: an array of bindings, together with a
hash function that maps each possible key into an array index.
Tree
A tree is a widely-used data structure that
emulates a hierarchical tree structure with a set
of linked nodes.
Terminology of Tree
Node.
• A node is a structure which may contain a value, a
condition, or represent a separate data structure
(which could be a tree of its own).
• Each node in a tree has zero or more child nodes.
• A node that has a child is called the child's parent
node (or ancestor node, or superior).
• A node has at most one parent.
An internal node or inner node
An internal node or inner node is any node of a tree that
has child nodes.
Similarly, an external node (also known as an outer node,
leaf node, or terminal node), is any node that does not
have child nodes.
The topmost node in a tree is called the root node. Being
the topmost node, the root node will not have a parent.
All other nodes can be reached from it by following edges
or links.
Every node in a tree can be seen as the root node of the
subtree rooted at that node. A free tree is a tree that is not
rooted.
The height of a node
The height of a node is the length of the longest downward
path to a leaf from that node.
The height of the root is the height of the tree.
The depth of a node is the length of the path to its root (i.e.,
its root path). This is commonly
A subtree of a tree T is a tree consisting of a node in T and all
of its descendants in T. (This is different from the formal
definition of subtree used in graph theory.
Example of Tree data structure
.
Representations
There are many different ways to represent trees;
common representations represent the nodes as
dynamically allocated records with pointers to their
children, their parents, or both, or as items in an
array, with relationships between them determined
by their positions in the array (e.g., binary heap).
The process of adding, removing
and searching for data items
within common collection
structures