Professional Practice
Professional Practice
repeat n - 2 times:
next_fibonacci = a + b
print next_fibonacci
a=b
b = next_fibonacci
```
References:
- Dasgupta, S., Papadimitriou, C. H., & Vazirani, U. V. (2006). Algorithms. McGraw-Hill
Education.
- Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2009). Introduction to Algorithms.
MIT Press.
1. Correctness: The algorithm should produce the correct output for all valid inputs.
2. Efficiency: The algorithm should use appropriate resources (time and space) efficiently.
3. Input and output: The algorithm should clearly define the input and output requirements.
4. Finiteness: The algorithm should terminate after a finite number of steps.
5. Clarity: The algorithm should be easy to understand and interpret.
6. Modularity: The algorithm should be composed of smaller, manageable components.
7. Maintainability: The algorithm should be easy to modify and maintain.
8. Optimality: The algorithm should provide the best possible solution, if applicable.
B. Pseudocode for displaying the Fibonacci series and calculating the factorial of a given
number:
```
function fibonacci(n):
if n <= 0:
return []
else if n == 1:
return [0]
else if n == 2:
return [0, 1]
else:
sequence = [0, 1]
for i from 2 to n-1:
next_number = sequence[i-1] + sequence[i-2]
sequence.append(next_number)
return sequence
```
2. Factorial algorithm:
```
function factorial(n):
if n < 0:
return "Factorial is undefined for negative numbers."
else if n == 0:
return 1
else:
result = 1
for i from 1 to n:
result = result * i
return result
```
1. Syntax errors: Mistakes in programming syntax can lead to compilation or runtime errors.
2. Logical errors: Flaws in the algorithm or incorrect use of programming constructs may
produce incorrect results.
3. Boundary conditions: Handling edge cases, such as input validation or handling extreme
values, can be challenging.
4. Efficiency: Designing an efficient algorithm may require careful consideration of time and
space complexity.
5. Debugging: Identifying and fixing errors or bugs in the code can be time-consuming and
require careful analysis.
6. Readability and maintainability: Writing clean and organized code can contribute to its
readability and long-term maintainability.
References:
- Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein.
ACTIVITY 2
A programming paradigm refers to a specific approach or style of programming that
provides guidelines and rules for structuring and organizing code. It encompasses a set of
principles, concepts, and techniques used to design and implement programs. The three
main programming paradigms are procedural, object-oriented, and event-driven. Let's
discuss their main characteristics and relationships:
1. Procedural Paradigm:
The procedural paradigm focuses on a step-by-step execution of instructions to solve a
problem. It treats the program as a series of procedures or functions that manipulate data.
Key characteristics include:
```c
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
int result = add(x, y);
printf("The sum is: %d\n", result);
return 0;
}
```
Critique: The code follows a procedural style, with functions and global data. The control
flow is sequential, starting from the `main()` function. However, as the program grows
larger, maintaining data integrity and managing global data may become challenging.
2. Object-Oriented Paradigm:
The object-oriented paradigm organizes code around objects, which encapsulate data and
behaviors. It focuses on modeling real-world entities using classes and enables modular,
reusable, and extensible code. Key characteristics include:
- Encapsulation: Data and related behaviors are encapsulated within objects, promoting
data abstraction and information hiding.
- Inheritance: Classes can inherit properties and behaviors from other classes, establishing
an "is-a" relationship.
- Polymorphism: Objects of different classes can respond to the same message or method
call, allowing flexibility and code reuse.
- Modularity: Code is organized into classes and objects, facilitating maintainability and
reusability.
Here's a small code snippet in Python, which demonstrates the object-oriented paradigm:
```python
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
rectangle = Rectangle(5, 3)
print("The area is:", rectangle.area())
```
Critique: The code follows an object-oriented structure, with the `Rectangle` class
encapsulating data and behavior. It showcases encapsulation, as the `area()` method
accesses the object's internal state. The code also demonstrates modularity and reusability
through the class definition. However, if the program becomes more complex, managing
relationships between objects and handling dependencies may become challenging.
3. Event-Driven Paradigm:
The event-driven paradigm focuses on responding to events or user actions. It typically
involves an event loop that waits for events to occur and triggers appropriate callbacks or
event handlers. Key characteristics include:
- Event-driven architecture: The program is structured around events and their handlers,
allowing asynchronous and non-blocking execution.
- Event loop: A central event loop processes events and dispatches them to the
corresponding event handlers.
- Callbacks: Event handlers or callbacks are functions that get invoked when specific events
occur.
- Event hierarchy: Events can be organized in a hierarchical manner, allowing event
propagation and handling at different levels.
Here's a small code snippet in JavaScript, which demonstrates the event-driven paradigm in
a web environment:
```javascript
const button = document.getElementById('myButton');
function handleClick() {
console.log('Button clicked!');
}
button.addEventListener('click', handleClick);
```
Critique: The code follows an event-driven style, where the `handleClick` function is
registered as an event handler for the 'click' event on the `button` element. It showcases the
event-driven architecture and asynchronous execution. However, in complex systems,
managing event handlers and their dependencies might become challenging, leading to
potential issues like callback hell or event collision.
In summary, each programming paradigm offers its own approach to structuring and
organizing code. The choice of paradigm depends