[go: up one dir, main page]

0% found this document useful (0 votes)
82 views30 pages

Unit 01: Programming Assignment Title: Algorithms and Programming Paradigms

This document discusses algorithms and programming paradigms. It begins with definitions of algorithms, their characteristics, types and how to design and analyze them. The main types discussed are brute force, divide and conquer, dynamic and greedy algorithms. The document then covers programming paradigms like procedural, object-oriented, parallel and event-driven paradigms. It provides examples and compares the paradigms. The document contains sections on algorithms and programming paradigms with definitions, examples and comparisons of the concepts.

Uploaded by

ammarah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views30 pages

Unit 01: Programming Assignment Title: Algorithms and Programming Paradigms

This document discusses algorithms and programming paradigms. It begins with definitions of algorithms, their characteristics, types and how to design and analyze them. The main types discussed are brute force, divide and conquer, dynamic and greedy algorithms. The document then covers programming paradigms like procedural, object-oriented, parallel and event-driven paradigms. It provides examples and compares the paradigms. The document contains sections on algorithms and programming paradigms with definitions, examples and comparisons of the concepts.

Uploaded by

ammarah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

The Millennium Universal College

Unit 01: Programming


Assignment Title: Algorithms and Programming Paradigms

Amara Nasim [040-35958]

Assessor Name: Waqas Ahmed


Department: HND Computing

October 28, 2022


Table of Contents
.................................................................................................................................................................... 1
List of Tables................................................................................................................................................4
List of Figures...............................................................................................................................................4
Section 1: Algorithms..................................................................................................................................5
What is Algorithm?..................................................................................................................................5
Characteristics of Algorithm....................................................................................................................7
Why do you need an Algorithm?.............................................................................................................7
Properties of Algorithm...........................................................................................................................7
How does algorithm work?......................................................................................................................8
Algorithm vs. Program.............................................................................................................................8
Factors of an algorithm............................................................................................................................8
How to design an algorithm?...................................................................................................................9
How to analyze an Algorithm?...............................................................................................................10
What is algorithm complexity and how to find it?.................................................................................11
Advantages........................................................................................................................................12
Disadvantages....................................................................................................................................12
Types of Algorithm................................................................................................................................12
Brute Force Algorithm...........................................................................................................................12
Example of brute force algorithm......................................................................................................13
Divide and Conquer Algorithm..............................................................................................................13
Example of Divide and Conquer.........................................................................................................14
Advantages of using Divide and Conquer Algorithm.........................................................................15
Disadvantages....................................................................................................................................15
Dynamic Algorithm................................................................................................................................16
Greedy Algorithm..................................................................................................................................17
Example of Greedy Algorithm................................................................................................................18
Section 2: Programming Paradigms...........................................................................................................20
What is Programming Paradigm?..........................................................................................................20
Declarative Programming Paradigms....................................................................................................20
Imperative Programming Paradigms.....................................................................................................20
Procedural programming...................................................................................................................21

2
Example of Procedural Programming....................................................................................................21
Characteristic.....................................................................................................................................22
Pros and Cons of Procedural Programming.......................................................................................23
Object-Oriented Programming..............................................................................................................23
Pros and Cons of OOP........................................................................................................................23
Example of OOP.....................................................................................................................................24
Parallel Paradigm...............................................................................................................................24
Event –Driven Paradigm........................................................................................................................25
What is event-driven programming?.................................................................................................25
Characteristics of Event-Driven Programming.......................................................................................25
What makes an event-driven programming special?............................................................................26
Advantages of Event-Driven Programming........................................................................................27
Disadvantages of Event-Driven Programming...................................................................................27
Example of Event-Driven Paradigm.......................................................................................................27
Procedural VS Object-Oriented VS Event-Driven Paradigms.................................................................29
Conclusion.............................................................................................................................................29

3
List of Figures
Figure 1 Algorithm.......................................................................................................................................5
Figure 2 Flowchart to add two numbers......................................................................................................6
Figure 3 add two numbers entered by the users and print their sum.......................................................10
Figure 4 output of the algorithm...............................................................................................................10
Figure 5 Time and Space example.............................................................................................................12
Figure 6 Merge sort...................................................................................................................................15
Figure 7 Code for Fibonacci sequence using Dynamic Programming........................................................17
Figure 8 Output of Fibonacci sequence......................................................................................................17
Figure 9 Selection Sort...............................................................................................................................19
Figure 10 Selection Sort Output................................................................................................................19
Figure 11 Procedural Programming Paradigm...........................................................................................21
Figure 12 Output for Procedural Programming.........................................................................................22
Figure 13 OOP Code..................................................................................................................................24
Figure 14 OOP output................................................................................................................................24
Figure 15 Event-driven programming Code...............................................................................................28
Figure 16 Event-Driven Programming output............................................................................................28

List of Tables
Table 1 Algorithm VS Program.....................................................................................................................8
Table 2 Advantages and Disadvantages of Imperative Programming........................................................21
Table 3 Pros and Cons of OOP...................................................................................................................23

4
Section 1: Algorithms

What is Algorithm?
We have all seen food recipes, they usually contain a list of ingredients, along with set
of instructions to be followed to make the described meal similarly in computer science
an algorithm usually refers to a small procedure that solves a recurrent problem
(Gillis, 2022). In computer language, the recipe is referred as the procedure and
ingredients are known as inputs. Your computer looks at the procedure, follows it step
by step and you get to see desired outcomes, which are known as outputs.
Every time we use our phones, computers, laptops, or even calculators, we are using
algorithms. Programs work in a similar way. Their codes are made up of algorithm
telling them what and how to do something and give the desired outcome to the users
every single time.
Another example can be, let’s say you want to use a navigation app to get directions to
your favorite restaurant. So first you open the app and type in the address of the
restaurant, then the app uses different methods to look at various available directions to
reach that restaurant. Next, it uses different methods to check current traffic, then using
all this information it finds you the best available route to your favorite restaurant.
Both of the examples mentioned above, shows how a computer uses algorithm to
obtain result, which are better and faster in comparison to humans.
In simpler terms an algorithm is a step-by-step procedure or a set of rules followed by
your computer to solve a problem.

Figure 1 Algorithm

5
Algorithms can be written in pseudocode (a simplified programming language), in
English or any other language spoken by the programmer, programming language,
flowcharts and even control tables and yet the output will be the same. Natural
languages are rarely used as they are more ambiguous, whereas programming
languages, pseudocode, flowcharts and control tables are structured ways to express
algorithm that avoid ambiguities common in the statements based on natural language.
Programming languages are used for expressing algorithms executed by a computer.
Below, you can see a flowchart of an algorithm for adding two numbers entered by the
user (Anon., n.d.).

Figure 2 Flowchart to add two numbers

Instead of being all over the place algorithm has a start, a middle and an end, making it
look organized and easy for the users to understand. It also includes only what is
needed for the task to be completed, and excludes everything that is unclear to avoid
confusion.

6
Characteristics of Algorithm
Not all written instructions for programming is an algorithm. In order for the written
instructions to be algorithm, its needs to have or follow certain characteristic like:
Unambiguous and Clear: an algorithm must have specific, outlined steps. The steps
should be clear enough for the user to understand what to do at each step.
Well Ordered: the exact number of operations performed should be defined.
Feasibility: all steps should be possible.
Input and Output: should be defined precisely, along with being able to accept a well-
defined set of inputs and produce some result as an output.
Finiteness: should be able to terminate after a finite number of instructions.
Generality: being able to handle many inputs.
Language Independence: It should be language independent, meaning it can be
implemented in any language and still produce the same result.
Determinism: giving the same output for the same input case .
Also an algorithm can be analyzed by power consumption, CPU registers, network
data transfer rate, time and space.
Why do you need an Algorithm?
It is required for the following reason:
Scalability
It aids in your understanding, when you have a real-world problem, you can break it
down into smaller steps and analyze it, to solve the problem quickly.
Performance
It is challenging to break down real-world problem into smaller steps, so if the problem
can easily be divided into smaller steps it means it’s feasible.
Properties of Algorithm
 It should terminate after a finite time.
 It should produce at least one output.
 It should be deterministic.
 Every step must be effective.
 It should take zero or more input.

7
How does algorithm work?
An initial input along with a set of instructions are used by algorithm, input can be in the
form of numbers or words, and these are needed to make decisions. The output is the
last step in an algorithm and is normally expressed as data.
For example, a search algorithm takes a search query as input and runs it through a set
of instructions for searching through a database for relevant items to the query.
Automation software acts as another example of algorithms, as automation follows a set
of rules to complete tasks. Many algorithms make up automation software, and they all
work to automate a given process (Gillis, 2022).
Algorithm vs. Program
Algorithm Program
An algorithm is a step-by-step procedure Set of various instructions that a computer
for computers to solve any given follows.
program.
Design Implementation
Domain Knowledge Programmer
Natural language, flowchart and many Program Language
more
Hardware and Software independent Hardware and software dependent
Analysis Testing
Table 1 Algorithm VS Program

Factors of an algorithm
The following are the factors to consider while designing an algorithm:
Functionality: it takes into account various logical steps to solve real-world problem.
Simplicity: It has to be simple, so it can easily be understood.
Extensibility: it should be extensible, so other designer or programmers can use if they
want to.
Robustness: ability of an algorithm to define your problem clearly.
Maintainability: it should be designed in a straightforward, structured way so you can
make changes in the future if needed.
Correctness: the algorithm should produce desired outcome when given an input,
meaning it is designed correctly.
Modularity: perfectly designed for the algorithm if you are given a problem and break it
down into small steps which is a basic definition of an algorithm.

8
How to design an algorithm?
In order to write an algorithm, there are some requirements:
1. Problem, that is to be solved by this algorithm.
2. Limitations, that are to be consider while solving the problem.
3. Input that is to be taken.
4. Output that are expected when the problem is solved.
5. Solution to this problem, in the given constraints.
With the help of the parameters mentioned above an algorithm is established that
solves the problem.
Example: Consider the example to add two numbers entered by the users and print
their sum.
STEP 1: Fulfilling the pre-requisites
As discussed above, in order to write algorithm, its requirements should be fulfilled.
a. Problem: add two numbers entered by the user and print their sum.
b. Limitations: the user can only enter integers any other data type is not
applicable.
c. Input: Two numbers to be added by the user.
d. Output: The sum of two numbers taken as input, in integer.
e. Solution to this problem, in the given constraints: The solution consist of adding
two numbers, it can be done by using’+’ operator or any method.
STEP 2: Designing the algorithm
With the help of the pre-requisites mentioned above, design the algorithm
Algorithm to add two numbers entered by the user and print the result:
1. START
2. Declare 2 integer variables, num1, num2
3. Enter 2 numbers to be added as input in variables num1 and num2.
4. Declare another integer variable where the resultant is to be stored, called sum.
5. Add num1 and num2 and assign the result to sum.
sumnum1+num2
6. Display and print the sum.
7. END

9
STEP 3: Testing and implementing the algorithm
Program:

Figure 3 add two numbers entered by the users and print their sum

Output:

Figure 4 output of the algorithm

How to analyze an Algorithm?


For a standard algorithm to be good, it must be efficient and this can be checked and
maintained in 2 stages:
1. Priori Analysis
Priori means “before”, hence this analysis means checking the algorithm before
implementing it. This is done when the algorithm is still in theoretical form. Before
implementing various factors such as processor speed, which does not affect the
implementation, can be considered. This analysis is independent on the
language of the complier and type of hardware used. Also it gives approximate
answer for the complexity of the program.
2. Posterior Analysis
Posterior means “after”, hence this analysis means checking the algorithm after
implementing it. This is checked by implementing it in any programming
language and executing it. This analysis determines how much running time and

10
space is required. It is dependent on the language of the complier and type of
hardware used.

What is algorithm complexity and how to find it?


Performance of the algorithm can be measured by the Time it will need to execute and
get expected output, and the Space it will need to store all the data, hence these 2
factors define the efficiency of an algorithm.
Time is measured by counting the number of key operations such as comparisons in
the sorting algorithm.
Space which can be measured by counting the maximum memory space required by an
algorithm to run.
Therefore the complexity of algorithm can be divided into 2 types:
1. Time Complexity
Refers to the time required to complete an algorithm execution, we use big O notation to
represent time complexity.
Time complexity is calculated by primarily counting the number of steps required to
complete the execution.
2. Space Complexity
This refers to the amount of memory required by the algorithm to store variables and get
the result. This can be for inputs, output or even temporary operations. Just like time
complexity, space complexity is expressed in big O notation.
Following reasons are required why algorithm require space:
i. To store program instructions.
ii. To store track of constant and variables values
iii. To store track of function calls, jumping statement and so on.
Space Complexity= Auxiliary Space + Input Size (Upadhyay, 2022)
Before looking at the example, there are some time and space complexity rules:
a. Every simple statement will take 1 unit of time.
b. Every loop will take n+1 unit of time.
c. Every statement within the loop takes n unit of time.
d. Every simple variable takes 1 word memory
e. Metrics will take N word space for memory.

11
Example:

Figure 5 Time and Space example

The worst-time complexity is generally considered because it is the maximum time


required for any given input size.
Advantages
 Easy to understand, since it’s a step-wise representation of a solution it is easy to
understand.
 Language independent it doesn’t depend on programming language thus can be
understood by anyone,
 Debug, as every step in independent problems or mistakes can easily be
identified.
Disadvantages
 Creating efficient algorithm is time consuming and requires good logical skills.
 Nasty to show branching and looping in algorithm.
Types of Algorithm
There are several types of algorithm available, below are a few of them:

12
Brute Force Algorithm
This the most common and simplest form of algorithm, its follows a straightforward
approach towards the problem, trying every possibility rather than advanced techniques
to improve efficiency.

Example of brute force algorithm


If there is a lock of 4-digit PIN. The digits to be chosen from 0-9 then the brute force will
be trying all possible combinations one by one like 0000, 1002, 1003, 1004, and so on
until we get the right PIN. In the worst case, it will take 10,000 tries to find the right
combination.
Every problem can be solved using this algorithm although generally not with
appreciable with space and time complexity.
Divide and Conquer Algorithm
This is one of the most commonly used algorithm in programming. As the name
suggests, this algorithm first divides the problems into two sub-problems, and then
combines the solutions together to get a final result. In order to do that it should consist
of 3 steps:
 Divide: divide the problem into sub-problem.
 Solve: solve sub-problems recursively.
 Combine: combine the sub-problem to get the final solution for the whole
problem.
Although using this algorithm to solve all the problems is not possible, but it can be
extensively used in various problems as it is quite stable and optimal.
Applications that works on Divide and Conquer Algorithm:
Binary Search: this is a fast search algorithm which works on the principle of Divide
and Conquer Algorithm. This algorithm looks for a particular item by comparing the
middle most item of collection. If a match occurs, then the index of item is returned. If
the middle item is greater than the item, then the item is searched in the sub-array to
the left of the middle item. Otherwise, the item is searched for in the sub-array to the
right of the middle item. This process continues on the sub-array as well until the size of
the sub array reduces to zero.
Merge Sort: This algorithm first divides the problem into equal halves and then
combines them in a sorted manner, to find a solution to the whole problem.

13
Quick Sort: This algorithm picks a pivot element, rearranges the array elements in such
a way that all elements smaller than the picked pivot element move to left side of pivot,
and all greater elements move to right side. Finally, the algorithm recursively sorts the
sub arrays on left and right of pivot element.
Strassen’s Matrix multiplication: This technique is an efficient algorithm to multiply
two matrices.
The complexity of Divide and Conquer algorithm is calculated using the master
theorem which is as follow:

T(n) = aT(n/b) + f(n)


Where
n= size of input
a= number of sub-problems
n/b = size of each sub-problem. (We assume the size is same of all)
f(n) = cost of the work done outside the recursive call (cost of dividing the problem +
cost of merging the solution)

Example to find the time complexity of a recursive problem, for a merge sort the
equation can be written as:
T(n) = aT(n/b) + f(n)
= 2T(n/2) + 0(n)
a = 2 (each time, a problem is divided into 2 sub-problems)
n/b = n/2 (size of each sub problem is half of the input)
f(n) = time taken to divide the problem and merging the sub-problems
T(n/2) = O(n log n)

Therefore:
T(n) = 2T(n log n) + O(n) = O(n log n)

Example of Divide and Conquer


Merge Sort, divides the problem into two halves, solves the two halves recursively and
then merges the two result to give a final output.
14
Figure 6 Merge sort

Advantages of using Divide and Conquer Algorithm


1. This algorithm makes problem solving easier as it divides the given problem into
2 halves, solves them individually and then combines all the solution into one to
solve the original problem.
2. This also increases the efficiency of other algorithm such as quick sort, merge
sort and etc.
3. This algorithm is supported by most processors, especially where data
communication between processors does not need to be pre-programmed.
4. Also it uses memory caches in an efficient manner.
Disadvantages
1. Recursion is slow that increases the time complexity.
2. This algorithm can sometimes become complicated while solving the problem
through this approach and the basic iterative approach seems easier.
3. Sometimes there can be same sub-problems, so it’s best to save the solution to
the repeated sub-problem.

15
4. Stack overflow may cause the processing to fail, so make sure enough memory
is allocated to return stack.
Dynamic Algorithm
This the most sought out algorithm as it provides the most efficient way of solving the
problem. This type is also known as memorization technique, because instead of
calculating the result again and again it stores the previously calculated result. It goes
through 5 steps to find the best solution to the problem:
 It divides the problem into sub-problems to find the best solution
 After breaking down the problem, it finds the best solution from these sub-
problems.
 Then it goes through the process of memorization, meaning sorting the result of
sub-problems.
 Then reuse the result to prevent it from being recomputed for the same sub-
problem.
 Finally, it computes the complex program’s output.
Dynamic algorithm has 2 properties:
Optimal Substructure: An optimal solution to the problem contain an optimal solution
to its sub-problems (Waraich, 2022).
Overlapping sub-problems: sub-problems are smaller variations of an original, larger
problem (Team, 2022). 
This algorithm has 2 versions:
Bottom-up Approach: where the algorithm is solved from the bottom, meaning the last
possible sub-problem is solved first and using the result of this, the above sub-problems
are solved.
Top-Down Approach: where the algorithm is solved from the very beginning to arrive
at the required sub-problem and solve it using the previously solved sub-problems.
Problems that can be solved using this algorithm are:
Knapsack Problem, Longest Common Subsequence, Subnet Sum, Chain Matrix
Multiplication and etc.
The most common and simple example of this algorithm is Fibonacci sequence.

16
Code:

Figure 7 Code for Fibonacci sequence using Dynamic Programming

Output:

Figure 8 Output of Fibonacci sequence

Time Complexity: O (n) for given a


Auxiliary space: O (n)
Dynamic programming approach gives faster result although takes up extra space.
Greedy Algorithm
In the greedy Algorithm, the solution is built part by part. The decision to choose the
next part is done by the basis that it gives an immediate benefit, never considering the
choices that previously have been made. There are 2 properties in this algorithm:
 The best option is chosen greedily
 Optimal substructure property, if an optimal solution can be found by dividing
into sub-problems.

17
This algorithm is simple and easy on device. Common problems that can be solved by
this method are Huffman Coding, Prim’s Algorithm, Path Algorithm, Job
Scheduling, Selection sort and etc.
Example of Greedy Algorithm
Selection Sort, the best example, this sort works as follow:
 Find the largest element exchange it with element in the last position.
 Find the second largest element and exchange it with the second last position.
 Continue in this way until the entire array is sorted.

Sorted Swap Comparison


5 23 33 20 2 10

5 23 10 20 2 33
5 23 10 2 20 33
5 2 10 20 23 33
5 2 10 20 23 33
2 5 10 20 23 33
2 5 10 20 23 33

Code:

18
Figure 9 Selection Sort

Output:

Figure 10 Selection Sort Output

Complexity
Time complexity: O(n2)
Space Complexity: O(1)

Section 2: Programming Paradigms

19
What is Programming Paradigm?
Paradigm means pattern or a set of rules that define a standard. In context, of
programming the term paradigm means different ways or styles in which a program or
programming language can be organized, to create softwares.
One thing to remember is that programming paradigm does not specify the
programming language syntax, rather they simple define the program structure and the
set of principles that programming language complier should enforce.
Therefore, a programming paradigms is a framework that defines how a programmer
can model a complex problem to be solved, easily.
Each programming paradigm consist of different structure, features and opinions about
how a common problem can be solved. That is why different programming languages,
while being implemented follow different or combination of different strategies, and
these strategies are called paradigms. Like for example, C++ is a multi-paradigm
programming language that uses single or combination of approaches like Procedural
or Object-oriented programming, mixing in Generic or Functional programming
concepts.
There are many different types of programming paradigms that are used to fulfill each
and every demand. The paradigm can be broken down into 2 categories:
Declarative Programming Paradigms
This is a style of building program that expresses logic of computer without talking about
the control flow. It focuses more on what needs to be done instead of how it should be
done, and just declares the result.
The only difference between imperative and declarative programming is that imperative
is how to do and declarative is what to do.
Examples are logical, functional and database.
Imperative Programming Paradigms
Imperative is one of the oldest programming paradigm. It works by changing the
program state through assignment statements. It performs step by step task by
changing the state. It uses command to inspect and update variables, and store state in
program, then a procedure is created by the combinations of these command. Its main
focus is on how to achieve the goal.
Programming languages that use this include C, C++, Java, Python and etc.

Advantages Disadvantages
Very simple and easy to learn Optimization and extension is more

20
difficult
Efficient Higher risk of error when editing
Solution path is very easy for beginners to Codes can become very extensive and
understand thus confusing
Table 2 Advantages and Disadvantages of Imperative Programming

Imperative programming is broken down into 3 broad categories: Parallel, Procedural


and Object-Oriented Programming (OOP). These paradigm are as follow:
Procedural programming
This paradigm is a derivation of imperative programming, based on the concept of
procedure call, these procedures are also known as functions, subroutines or method.
These procedures contain simply series of computational steps to be followed. And
these procedures can and might be called at any point by the procedure itself or other
procedures, this ability to be reused was one of the reasons it proved to be beneficial.
As procedural programming language is old and easy to grasp in concept many new
developers begin their training with it.
Another useful aspect of this programming is the way it works with your devices. When
using this programming you give direct and specific instructions to your computer and
tell it how to accomplish its goals through logical and step-by-step processes.
Procedural programming languages are also known as top-down languages. Examples
of this paradigm include C, C++, Java and Pascal.
Example of Procedural Programming
Code:

Figure 11 Procedural Programming Paradigm

Output:

21
Figure 12 Output for Procedural Programming

Characteristic
Some key features of procedural programming language:
Predefined Functions
A set of predefined functions are available, these functions allow a programmer to
complete common task without having to create code themselves, and this is very time-
efficient.
Local Variables
Variable declared within the main structure of the method and is limited to local scope.
The local variables can only be used in the method it is defined in, and if used outside
the code will not work.
Global Variables
Unlike local variable, global variables increase functionality, they make themself
available to all method or functions in the code, providing the developer benefit of
accessing key data throughout the program.
Parameter Passing
This is a mechanism used to pass parameter to functions, subroutines or procedures to
complete computing actions on the data. The programmer places input parameters into
the module and receive output in return.
Modularity
This is a structure in which a developer divided the functionality of its code into a series
of small blocks. The programmer can then call these blocks, in their code to access
them at multiple points.
Top-down approach
This programming relies on top-down approach, under this approach a developer first
defines the primary goals then assess the component required to complete it.

Pros and Cons of Procedural Programming


Pros

22
 Coding is easy and simple
 Codes can be reused in several parts of the program
 Consumes less memory on the computer
 Program flow can be tracked easily
Cons
 Focus is on function rather than data
 Security is at risks, as the data is exposed to the world
 Difficult to relate with real-world objects
 Program codes are difficult to maintain and update because of global data

Object-Oriented Programming
The program is written as a collections of classes and object, which is based on
sending of messages to objects. These objects respond to the messages by performing
operations called methods.
Programming languages that uses this paradigm often handle complex application
better, and 3 most popular programming languages that use this are Java, C++ and
Python.
The paradigms core features are objects, classes, data encapsulation, inheritance
and polymorphism (use of a single symbol to represent multiple different types).
Classes are the foundational component of OOP, some classes inherit properties and
operations from other classes and this is often called a parent-child relationship by
developers.
Classes contain tools to hide and protect sensitive data through encapsulation, this is a
great advantage of this paradigm. Once the class is defined, we can use it to create
object.
Pros and Cons of OOP
Pros Cons
Codes can be used multiple time using Larger than other programs
class
Easy to maintain and modify Not suitable for some problems
Data Security Slower than other programs
Low-cost development Takes time to get used to
Data Redundancy Requires a lot of effort
Table 3 Pros and Cons of OOP

23
Example of OOP
Code:

Figure 13 OOP Code

Output:

Figure 14 OOP output

Parallel Paradigm
Dividing the program instructions among multiple processors. The processing system of
this paradigm possess many different number of processor thus the programming time
is less. This approach is similar to divide and conquer, example of this paradigm are
NSL, C and C++.

24
Event –Driven Paradigm
For building complex system, event driven programming is a great approach. It includes
divide and conquer principle while allowing you to use other approaches like
synchronous calls.
What is event-driven programming?
In this paradigm, entities (objects, services and so on) communicate indirectly by
sending messages to one another through an intermediary. The messages are typically
stored in queue before being handled by the consumers.
Unlike direct calls, event-driven programming completely separate the producer from
the consumer, leading to some significant benefits. For example, multiple producer and
consumers can all collaborate to process incoming requests. Retrying failed operations
and maintain an event history are also simplified.
The flow of the program is determined by events, these are pieces of data that are sent
by producers and consumed by consumers. Events are user’s actions like mouse click
or key presses, sensor outputs, or message passing from other programs or by small
sequence of programmed instructions known as threads.
Key to event-driven paradigm is, producer and consumers are unaware of each other
and interact through message queues only.
Message queues are central location where messages are stored, either in memory or
durable storage. These are typically categorized based on topics, and consumers only
receive the queues which they are subscribed to, and this is the responsibility of the
message broker. Some popular ones are Redis, RabbitMQ and Apache Kafka.
This type of programming are dominant in graphical user interfaces and other
applications like JavaScript, where actions are based off on user inputs. Another system
that is event-driven is Node.j.
Generally, there is main loop in the event-driven applications that listens to events and
when an event is detected, a callback function is triggered. This program can be written
in any programming language, although languages that provide high-level abstractions
such as awaits and closures are preferred. Also, this programming makes it easier to
scale large systems, adding capacity simply by adding consumers.
Characteristics of Event-Driven Programming
Events
Events are actions that can be reacted to, like mouse click, keyboard and etc.
Event Handler
A type of function that run specific action when a specific event is triggered like for
example when clicking on a compose button, a tab to enter message is displayed or
when a send button is clicked, a “message sent” message is displayed.

25
Trigger Function
These are functions that decide on what code to run when a specific event occurs.
Service Oriented Processing
This is a key-feature in event-driven programming that are used to write programs, that
are made for services and they usually run in the background of OS. Also SOP doesn’t
slow down the computer but consumes little processing power of computer.
Time Driven
A code that runs on a time trigger, time driven can be a specific code that runs on
specific time, meaning it’s a pre-set to do task. Example window updates which a user
can set when to update.
What makes an event-driven programming special?
Tightly Coupled VS Loosely Coupled Workflow
Traditional call-based workflows are tightly coupled. Let’s look at an example, if person
A has some information Y, that is relevant to person B, then person A needs to follow
some criteria to deliver this information successfully to B, like:
a. It needs to know if B is interested in Y.
b. In case if something goes wrong, then it needs to know what to do
c. It also needs reference, needs to call to B and also know about its interface, and
a lot more.
But what if person C and D are also interested in information Y, but they have different
interfaces for receiving Y. For this A needs to get reference to C and D along with their
respective interfaces, it also needs to consider what to do in case of half failure, like if C
and D receive the information but B didn’t.
Whereas for loosely coupling, the only requirement that A needs to follow is send an
event Y to event queue. Now B, C and D can subscribe to event if they are interested.
Synchronous VS Asynchronous Calls
Garden variety function calls are known as Synchronous calls. A function with
arguments can be called and get the result in the same thread, blocking the threads
until function call returns.
Whereas Asynchronous calls return immediately without waiting for computation to be
complete. This is used by event-based system to receive events.
Request- response VS Publish- subscribe
Approach to communicate is typical of traditional web development is Request
response, for example searching something on search engine and receiving information
in respond by server.

26
Fire and forget are cases are tackled by publish subscribe approach, short for PubSub.
This simply publishes an event to queue without needing any responds.
Advantages of Event-Driven Programming
1. Flexibility: Event-driven programs can be altered easily if the programmer wants
to edit something, allowing them to produce a form for their requirement.
2. Suitability for Graphical Interface: This allows users to select different tools
like buttons, radio button and etc. also allowing them to create, put and edit the
object to their desired requirements
3. Simplicity of the Programming: You can directly edit the object you want to
code for, making it easier for user. Also the programming languages such as
visual basic usually have predictive coding, so it can predict codes from what the
user is typing and save time.
4. Good for models systems that need to be both asynchronous and reactive.
5. Simple and understandable
Disadvantages of Event-Driven Programming
1. Event-driven programs are often more complex than simple programs
2. Programs with complex GUI may be slower to load and run if RAM is insufficient.
3. Possible tight coupling between event and consumer
4. Difficult to find error
5. The flow of the program is less obvious and logical
Example of Event-Driven Paradigm
Code and Output:

27
Figure 15 Event-driven programming Code

Figure 16 Event-Driven Programming output

28
Procedural VS Object-Oriented VS Event-Driven Paradigms
Procedural Object-Oriented Event-Driven
Character user interface is Commands written in To create the program GUI
provided to write the modules are provided. is provided.
command.
Commands are written and Specific task are Actions are defined on
executed in linear fashion. performed by interacting events, like mouse click,
with object and class. keyboard.

Sequential execution of Data Security Focuses on selecting user


steps. interface.
Examples, COBOL, Basic Examples, Java, C++ Examples, C# and Visual
Fortran Basic

Conclusion
Due to the increase need for large and complicated programs, programming paradigms
have been evolving continuously. In today’s world OOP and EDP are the most preferred
paradigm in comparison to others. These paradigm not only provide advantages like
reusability of codes, but also isolate the user from the internal working of the program
along with accepting many different and complex instructions, leading to decline of
procedural programming.

29
References
Anon., n.d. Flowchart In Programming. [Online]
Available at: https://www.programiz.com/article/flowchart-programming

Gillis, A. S., 2022. algorithm. [Online]


Available at: https://www.techtarget.com/whatis/definition/algorithm

Team, I. E., 2022. Dynamic Programming: Characteristics, Methods and Examples. [Online]
Available at: https://www.indeed.com/career-advice/career-development/dynamic-programming

Upadhyay, S., 2022. What Is An Algorithm? Characteristics, Types and How to write it. [Online]
Available at: https://www.simplilearn.com/tutorials/data-structure-tutorial/what-is-an-
algorithm#why_do_you_need_an_algorithm

Waraich, K., 2022. Types of Algorithms and their Uses. [Online]


Available at: https://www.codingninjas.com/codestudio/library/types-of-algorithms-and-their-uses

WordPress Theme by MH Themes, 2022. Event Driven Programming – Features. [Online]


Available at: http://bteccomputing.co.uk/event-driven-programming-features

30

You might also like