Uploaded by jeewasan53

Acknowledgement

advertisement
Acknowledgement
The success and final outcome of this assignment required a lot of guidance
and assistance from many people and I am extremely fortunate to having
been able to work with great minds specially during intensive practical
sessions. My gratitude goes to specially Mr. Tharaka Karunarathne for
providing us with all his insights in regards to the module and much more. It
was because of his guidance and efforts collectively that we were able to
complete the assignment on time and submit our best work. I would also like
to further extend my gratitude to the Esoft academic staff for facilitating us in
this journey.
Introduction
Computer programming is the process of designing and writing computer
programs. As a skill set, it includes a wide variety of different tasks and
techniques. Programming and computer science are vast fields that
encompass so very much. There are billions of dollars to be earned in
different ways. From selling software, to creating the next Facebook, or even
building a decentralized application with blockchain technology.This
coursework will cover the basic forefront to mid-level programming concepts
in a descriptive way to enhance understanding.
Activity 1 - Define basic algorithms to carry out an operation and outline the
process of programming an application.
Algorithm
According to (Bullinaria, 2019) an algorithm designed for a specific task can
be described as a finite series of instructions, each of which has a distinct
meaning and can be completed in a finite period of time with a finiteamount
of effort. As a result, an algorithm must be accurate enough for humans to
understand. Further (Gillis, 2022)suggests that algorithms can be classified
into various categories such as natural languages, programming
languages, pseudocode, flowcharts and control tables. Natural language
expressions are rare, as they are more complex. Programming languages
are normally used for expressing algorithms executed by a computer.
Characteristics of an Algorithm
According to (Sondhi, 2021) a successful algorithm musty be Unambiguous
which means an algorithm is like a blueprint of an application therefore it
should be clear and simple to understand the input & output criteria must
well outlined. The phases listed in an algorithm should be countable, and it
should end after a defined number of steps. Effectiveness should
be maintained in an algorithm to produce efficient results this can be
achieved through Time parameter and space parameter; Time parameter is
none other than the duration for an algorithm to process and the Space
parameter refers to the storage space required by the algorithm with
reference to input criteria and lastly algorithms must be language
independent which means the deigned algorithm must work for
allprogramming languages.
Pseudocode for Fibonacci number sequence
Start
i: integer
j: integer
k: int
eger
s: integer
n: integer
i=1, j=0, k=1, s=0, n=0
Input n
While i<=n
Print j
S=j + k
j=k
k=s
i=i+1
End while
Stop
Dry run for Fibonacci numbers
Input Sample number ‘N’ as 5
- While “i” is less than or equal to N
- J will be printed as 0
- S will be J plus K which is 1
- Then J will be equal to K new J value will be 1
- K will be equal to S value of K will be 1
- The counter will increment i is equal to i plus one
- Back to the beginning of the loop While “i” is less than or equal to N (i=2)
- New J value will be 1 and it will be printed
- S will be J plus K which is 2
- Then J will be equal to K new J value will be 1
- K will be equal to S value of K will be 2
- The counter will increment i is equal to i plus one
- Back to the beginning of the loop While “i” is less than or equal to N (i=3)
- New J value will be 1 and it will be printed
- S will be J plus K which is 3
- Then J will be equal to K new J value will be 2
- K will be equal to S value of K will be 3
- The counter will increment ; i is equal to i plus one
- Back to the beginning of the loop While “i” is less than or equal to N (i=4)
- New J value will be 2 and it will be printed
- S will be J plus K which is 5
- Then J will be equal to K new J value will be 3
- K will be equal to S value of K will be 5
- The counter will increment; i is equal to i plus one
- Back to the beginning of the loop While “i” is less than or equal to N (i=5)
- New J value will be 3 and it will be printed
- S will be J plus K which is 8
- Then J will be equal to K new J value will be 5
- K will be equal to S value of K will be 8
- The counter will increment; i is equal to i plus one
- Back to the beginning of the loop While “i” is less than or equal to N (i=6)
- Condition is False will exit the loop and end the program
Hence the output will be:
Output
→
0,1,1,2,3
Pseudocode for Factorial value
Start
n: int
fact: int
n=0, fact=1
Input n
While n>0
Fact = fact*n
n=n
-1
print fact
End while
Stop
Dry run for Factorial numbers
Input Sample number ‘N’ as 3
- While “n” is greaterthan 0
- fact is equal to fact multiplied by n current fact value is 1multiplied by the
current n value 3 givesthe new fact equals 3
- new n value equals previous n value minus 1 hence new n value is equal
to 2
- While “n” is greater than 0
- fact is equal to fact multiplied by n; current fact value is 3 multiplied by the
current n value 2 gives the new fact equals 6
- new n value equals previous n value minus 1 hence new n 2 minus 1
which is 1
- While “n” is greater than 0
- fact is equal to fact multiplied by n; current fact value is 6 multiplied by the
current n value 1 gives the new fact equals 6Hence the output will be:
Output
→
6
As 3x2x1
Steps to create a successful program
Developing a software entails the same processes as any other problem solving endeavor. In the programming process, there are five main
components:
1.Defining the problem
2.Planning the solution
3.Coding the program
4.Testing the program
5.Documenting the program
As the steps outlined above and to discuss those steps a bit in-depth is as
follows.
According to (Valenzuela, 2018) the first step to in creating a successful
program would be to Identify the problem, Defining the problem is the most
critical part as it is will need to develop a concrete plan for what the program
is intended to do.A crucial step in Identifyingthe known inputs (or provided
data) and the desired outputis required for this operation. Programming a
working applicationis not an easy task, with time and constant effort.In Order
to provide the solution to the problem identified in the next step would be to
Find a solution, the help of flowchart and pseudocodes are useful.
Experienced programmers use either of these methods to convey program
development to clients. The solution process
enables programmer to focus on the logical flow of the program without
having to adhere to the actual syntax used by the programming language for
the project. Once the solution is identified and a blue print is made the next
step would be to Code it. This is where the algorithmic design
skills from computational thinking come into play.Testingis yet another
critical aspect in the workflow it will determine the quality of the program, will
help to locate any bugs the testing phase is achieved in different levels, once
the testing is done the software should be ready to be handed over to the
user along with a fully documented user guide on how to use the for the first
time.
Big -O notation and its role in programing
The effectiveness of an algorithm as interpreted by (Jamieson, 2021)
can be evaluated using Big O Notation. As the input increases, it calculates
how long it takes to run a function.Efficiency is measured in terms of Time
complexity and Space complexity. The length of the function's execution in
terms of its computationalprocess is measured by its time complexity. Space
complexity is the required memory for a function to be executed
. Big O is referred to as the algorithm's upper bound, which denotes that it
handles the worst-case scenario. The algorithm will never performworse
than we anticipate thanks to our utilization of the worst-case
scenario.Furthermore (Mulonda, 2019)suggests that Big O notation is used
by programmers as a kind of measurement unit to assess or gauge the
effectiveness of a piece of code(s), a script, or an algorithm. The precise
runtime of a script or algorithm is difficult to ascertain. It also depends on
other elements like the processor's speed and other technical details of the
computer running the script or algorithm. Big O notation is therefore used to
assess how fast the runtime grows in relation to the input data that will be
processed by that algorithm rather than analyzing the runtime itself directly.
Python Code for Fibonacci numbers
nterms = int( input("How many terms? "))
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
photo
Fig1.1 Python code for Fibonacci numbers in python IDLE
Python Code for factorial numbers
num = int(input("Enter a number: "))
fact = 1
if num < 0:
print(" Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
fact = fact*i
print("The factorial of",num,"is",fact)
photo
Fig1.2 Python code for factorial numbers in python IDLE
Evaluating the program code with Big O notation for efficiency
According to (Spacey, 2016)The word "algorithm" has been used to describe
highly optimized codes, such as a widely complex problem in regards to
arithmetic functions. The word implied as acode has a strict design and
resolves a complex problem. Codesfrequently has little
complexity, is repetitious, or is not crucial. ACode that, for instance, shows a
user interface, verifies input, executes a transaction, or computes a value is
typically simple to implement. Since they are more sophisticated than other
types of structures, algorithms can start out as extensive research projects.
Any code written by a developer on the spot that doesn't resolve a significant
issue is often not regarded as an algorithm. It is important to realize that
businesses frequently use the word algorithm.Asa result, the phrase is
starting to lose its original meaning and is increasingly being used to refer to
code. Comparing the pseudocode for Fibonacci numbers it
and considering the Big O notation feature the pseudocode does not store
and display a lot of unnecessary variable/data this is an efficient use of space
complexity and when considering the timing of execution for the python code
for Fibonacci numbers is that comparatively its fast and could run computers
with less hardware features.
Activity 2 – Define the characteristics of procedural object oriented and
event driven programming.
Programming paradigm
According to (Bartoníček, 2014)Paradigm is a set of patterns and
procedures utilized to accomplish a specific task. For a certainidea to
become a paradigm, it should be picked up globally in many independent
organizationsand societies.There are mainly 3 paradigms that will be
discussed here it is as follows
1.Procedural Paradigm
2.Object oriented paradigm
3.Event driven paradigm
Procedural paradigm
The method of programming that has been utilized since the invention of
computers is known as the procedural paradigm. In this paradigm, unless
otherwise specified, the program consists of a series of instructions that the
computer is to carry out in the sequence that they were written. It is a
straightforward method that is usually simple to follow when the program is
brief. It may be exceedingly challenging to comprehend, manage, and
debug larger programs created with a procedural approach. The majority of
procedural languages have IF, FOR & WHILE statements control
mechanisms. Below is an example of a piece of procedural programing:
Characteristics of Procedural programming
• Predefined functions: Included in higher-level programming languages
and these functions are recognized by name.
• Local Variable: Defined in the method's primary structureandused inside
the method's local scope.
• Global variable:Is one that is declared outside of every other function. As
a result, unlike a local variable, global variables can be utilized in all
functions.
• Modularity: is the process through which two distinct systems that are
working on two different tasks are combined to complete a bigger job first.
• Parameter Passing: A mechanism used to pass parameters to functions,
subroutines or procedures.(Bhatia, 2022)
Object oriented paradigm
Due to the modularity of objects, the object-oriented paradigm was created
to simplify complex projects and distribute the workload among team
members. An object is the centerpiece of the object-oriented paradigm. Data
structures and operations that may be performed on the data, known as
methods, are grouped together as objects. The programmer can achieve a
high level of encapsulation by properly typing the data and procedures.
Characteristics of Object-oriented programming•
Encapsulation - When each object inside a class retains a private state.
Other objects cannot directly access this state; instead, they must call a set
of public functions. These methods let the object to control its own state, and
no other class may change it unless specifically authorized.
Abstraction - Abstraction is an extension of encapsulation. It is the process
of selecting data from a larger pool to show only the relevant details to the
object. Abstraction is the method of obtaining, deleting, or choosing user
data itenables to utilize the same data you used forapps with little to no
modification which is one of the benefits of Abstraction.
Inheritance – Ability to acquire some/all properties of another object. For
example, a child inherits the genes/characteristics of his/her parents.
Reusability is a major advantage whereas the fields and methods of the
existing class can be re-used. For example, Apple is a fruit assuming that a
class called Fruit is there and a subclass of it is an Apple. Hence an Apple
acquires the properties of the class Fruit.
Polymorphism – Polymorphism is the use of classes exactly like its parent
class this was there is less or no confusion with mixing types. This being
said, each child sub - class keeps its own functions/methods as they are.
(Banda , 2020)
Event - driven paradigm
The main focus of event - driven programming is the classification of events,
therefore the Program flow ultimatelyon external events. The model
containing the idea of event - driven programming is known as an
asynchronous model.Events are necessary for event - driven programming
to function. Events select what to do and in what order to perform it after they
have looped. The flowchart that follows will help you comprehend how this
operates.
(tutorialspoint, n.d.)
Characteristics of Event - driven programming
• Service oriented –Often appears in the background and does not have any
real interaction with the user but at the same time these applications could
be stopped from running with users will.
• Time driven – These included such as antivirus scans, task scheduler and
operating system updates etc. These events follow real time settings and
variables.
• Event Handlers –These are sections of code that are set out to denote the
actions of various events, such that when right or left clicking
the mouse can have different results.
•Trigger functions –These specific pieces of code designed to take actions
for an evente.g.,clicking abutton to trigger the calculation on a calculator.
(BURROWS, 2013)
Relationships between programming paradigms
Procedural programming means that as a series of steps, you describe a
program and its subprograms. On the other hand, declarative programs
attempt to define the result irrespective of the steps taken to calculate it but
rather some description or denotation of the desired result. Objectoriented programming is a way to organize code around the concepts of
encapsulation, inheritance, replacement, programming to interfaces, etc.
Event based programming is about writing event handling procedures and
having the core event loop provided by the underlying system. In this way
you can save the trouble of writing your own event loop and benefit from
various libraries that already work with the system provided event loop. E
vent based programs are very often writing using object-oriented style, but
not always. These three categories are thus not related strictly hierarchically,
but in common usage they are mostly nested within one
another.
Sample code for Procedural programing:
def Sum(sample_list):
total = 0
for x in sample_list:
total += x
return total
list1 = [25, 50, 90]
list2 = [9, 45, 77, 20]
print(Sum(list1))
print(Sum(list2))
Output:
165
151
The function “Sum()” have been used to calculate the “list1” and “list2” total.
The procedural style depends on procedure calls to create
modularizedcode. Which means the useof functions to group the tasks
parallelly makes the overall code simplerand easier to digest for
programmers when it comes to locating errors as well. According to (John,
2022) procedural programming Just consists of a set of computational steps
that teach the computer how to do a job in a logical sequence. When
addressing large projects, it is preferable to utilize another programming
paradigm rather than procedural programming, which is excellent for basic
applications. Further (Khatri, 2022) suggests that procedural paradigm
focuses on enhancing the program's clarity, quality, and development time,
with its unique characteristics with higher level programming language these
functions recognized by name, the parameter passing mechanism used to
functions ex: list1 & list2.
Sample code for Object oriented programing:
class Parrot:
species = "bird"
def __init__(self, name, age):
self.name = name
self.age = age
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
Output:
Blu is a bird
Woo is also a bird
Blu is 10 years old
Woo is 15 years old
An object could represent an employee with attributes such as name, title,
experience, etc., with behaviors like working, on - leave, underperformed,
etc as mentioned in the above code [species = bird ] According to (Herrity,
2022) For example, an object could represent an employee with
attributes such as name, title, experience, etc., with behaviors like working,
on-leave, underperformed, etc., The four principles of object-oriented
comparing the unique characteristic methods ofoop considering the
encapsulation feature in the class parrot there is private data such
as the name and age these data may not be exposable. Abstraction is the
feature when considering the above code def init __(self, name, age).
Sample code for Event driven programing:
import asyncio
async def main():
print('Hello ...')
await asyncio.sleep(1)
print('... World!')
asyncio.run(main())
The asyncio module in Python has a timer that can affect an event when its
time is up first it will print Hello then after sometime it will print World and the
loop ends. When the event does happen, the handler is called, and shown
below is the event when executed, create and manage
event loops, which provide asynchronous APIs for networking, running
subprocesses, handling OS signals, etc.; implement efficient protocols using
transports; bridge callback-based libraries and code with async/await syntax.
Download