0% found this document useful (0 votes)
42 views

Modular Programming and Algorithms I

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Modular Programming and Algorithms I

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Module 5. Modular Programming & Algorithms Pt.

1
The practice of modular programming and understanding of standard algorithms is
central to efficient coding and software development. This module introduces the
principles of modular programming and explores some of the standard algorithms
that are commonly used in the industry. From breaking down complex code to
understanding sorting techniques, this module is a step towards mastering the
building blocks of programming.

In this module, you will explore:

● Learn the modular programming approach, breaking down complex


programs into manageable and reusable modules or functions.
● Discover standard algorithms, understanding their structure, and
application in programming environments.
● Gain insights into pass-by-reference parameter passing, and how it affects
the calling variables.

To enhance your understanding, we have tailored specific learning activities:

● Break down a complex program into smaller modules or functions.


● Implement and compare standard sorting algorithms like bubble sort and
quick sort.
● Write a function that uses pass-by-reference and demonstrates its effect
on the caller's variables.

Subject Learning Outcomes

The material in this module relates to the following subject learning outcomes:

​ SLO 3: Learn to implement complex data structures like stacks, queues,


trees, and graphs, as well as algorithms for sorting and searching.

Student Expectations

During this module, you are expected to spend time working through the prescribed
material prior to attending class. This will enable a more valuable discussion to take
place in online tutorials or conferences, where you will be able to discuss concepts,
present ideas and explore information collaboratively in greater depth.
Accordingly, it is suggested that you allow:

4 hours for facilitated study. This includes guided facilitated study, watching or
attending course lectures (live or pre-recorded), responding to facilitator feedback.

8 hours for personal study. This includes reading through course material,
undertaking learning activities and exploring additional related resources.

Topic 1. Modular Programming Approach

Key takeaways from this topic...

1. One of the significant advantages of modular programming is the ease of


code reusability. Smaller, function-specific modules can be reused across
different parts of an application.
2. Breaking down a complex program into smaller modules makes it easier to
debug, as issues can be isolated and addressed individually.
3. In a team setting, modular programming allows multiple developers to work on
different modules simultaneously, increasing productivity.

Learner Guide

The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All

Panels

Collapse All

Panels

Section 1: Understanding Modular Programming

In the fast-paced
world of software development, efficiency and adaptability are key to meeting
ever-changing demands. Modular programming serves as a beacon in this regard,
promising streamlined processes and enhanced productivity.

This approach, which is becoming increasingly popular, divides a program into


separate, functionally independent modules. These modules, which can be
developed independently, function harmoniously when integrated, thus facilitating a
coherent and efficient system.

The characteristics of this approach are as follows:

● Separation of Concerns
This principle dictates that a program should be segmented into distinct
sections, with each handling a specific aspect of the functionality. This
separation ensures a cleaner, more organized code base which is easier
to maintain and upgrade.
● Reuse of Code
Modular programming encourages the development of reusable modules
that can find utility in various projects, promoting efficiency and reducing
the overall development time.
● Ease of Maintenance: One of the standout benefits of this approach is
the ease with which modules can be updated, added, or removed
individually, without affecting other parts of the program. This makes
maintenance less cumbersome and time-consuming.
● Increased Reliability
By isolating errors to individual modules, it becomes considerably easier to
identify and rectify issues, thereby enhancing the reliability of the software.

Benefits and Challenges of Modular Programming

Benefits

● Parallel Development
Different teams can work on separate modules simultaneously, which can
significantly speed up the development process.
● Improved Testing
Each module can be tested individually, thereby ensuring higher reliability
and stability of the system.

Challenges

● Integration Issues
Although modules are developed independently, integrating them can
sometimes pose challenges, especially if the modules are developed by
different teams.
● Complexity
As programs grow in size and complexity, managing numerous modules
requires careful planning and coordination to prevent potential issues and
maintain coherence.
Section 2: Functions

A function is a group of statements with a name. We can only execute the group of
statements by using the name of the function. Every C++ program has at least one
function, which is main().

Why define functions?

● Prevent repetition (keep code DRY)


○ Makes code easier to understand and maintain
○ If you find yourself copying lines of code, you should probably
create a function
● Encapsulate an algorithm (a series of steps) with a single purpose and
give it a name
○ Makes code easier to understand and maintain
● Makes it easier to test code
○ Testing a function separately makes it easier to debug if there is
a flaw in logic
● Functions make your code easier to understand and maintain

Creating a function

The general form of a C++ function definition is:

return_type function_name (parameter_list) {


statement1;
statement2;
statement3;

return statement;
}
Parts of a function

● return_type
is the data type of the value of the function returns. Some functions
perform the desired operations without returning a value. In this case, the
return_type is the keyword void.
● function_name
is the actual name of the function. The function name and the parameter
list together constitute the function signature.
● parameter_list
A parameter is like a placeholder. When a function is called, we pass a
value to the parameter. This value is referred to as the actual parameter.
The parameter list refers to the type, order and number of parameters of a
function. Parameters are optional.
● body
The function body is between the curly braces {}, and contains the
statements executed by the function.
● The return statement terminates the current function and returns the value
of the function to its caller.

Function declaration

A minimal function declaration consists of the return type, function name and the
parameter list. For example:

int sum(int num1, int num2);

A function declaration must be done before calling the function.

The required parts of a function declaration are:

● The return type, which specifies the type of the value that the function
returns, or void if no value is returned.
● The function name, which must begin with a letter or underscore and
cannot contain spaces.
● The parameter list, a local name by which the values may be accessed
inside the function.

Function parameters

A function has a comma-separated parameters list of zero or more types.


Information can be passed into a function as a parameter.

Parameters act as local variables inside the function and are specified after the
function name, inside the parentheses.

We can add as many parameters as we want, separated by commas.

return_type function_name (parameter1, parameter2, parameter3) {


// code to be executed
}

Return values from a function

The void keyword, used in the examples before, indicates that the function should
not return a value. If we want the function to return a value, we can use a data type
(such as int, float, char, etc...) instead of void, and use the return keyword inside the
function.

Example 1

void printAge(int age) {


cout << "Your age is " << age<< endl;
}

void main() {
int myAge = 23;
printAge(23);
}

In the above example, the function prints the age.

Example 2

void add (int no1, int no2) {


int total = no1 + no2;
cout << "Total is " << total << endl;
}

void main() {
int x = 10;
int y = 5;
add (x, y);
}

In the above example, the function calculates the total.

Section 3: Case Study - Enhancing Efficiency in a Healthcare System

Background

ABC Healthcare, a healthcare provider, decided to enhance its existing software


system to improve efficiency and streamline operations. The goal was to employ a
modular programming approach to facilitate seamless integration of various
functionalities and services.

Problem Statement

The existing software system at ABC Healthcare was monolithic and rigid, making it
difficult to implement new features and upgrades. The system was also prone to
errors, with issues in one part often affecting the entire system. ABC Healthcare
sought a robust and flexible solution that would overcome these challenges.

Implementation

ABC Healthcare adopted a modular programming approach, segregating the


software system into several distinct modules, such as Patient Management,
Appointment Scheduling, and Billing. Each module was designed to handle specific
functionalities and could be developed and maintained independently. This approach
also facilitated:

● Separation of Concerns: Ensuring each module focused on a specific


function, enhancing efficiency and reducing errors.
● Code Reusability: Developing modules that could be reused in other
healthcare facilities within the ABC network, reducing development time
and costs.
● Parallel Development: Enabling different teams to work on separate
modules simultaneously, speeding up the development process.

Outcome

The modular approach transformed ABC Healthcare’s software system, making it


more flexible, reliable, and efficient. The system could easily adapt to changing
requirements, and new features could be implemented quickly and seamlessly. The
approach also enhanced the system’s reliability, as issues could be isolated and
resolved without affecting other modules.

Topic 2. Introduction to Standard Algorithms

Key takeaways from this topic...

1. Understanding different sorting algorithms like bubble sort and quick sort
provides insights into computational efficiency and time complexity.
2. Standard algorithms solve common problems and are widely applicable in
data science, machine learning, and general software development.
3. Comparing the performance of different algorithms is crucial for choosing the
most efficient solution for a given problem.

Learner Guide

The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.

Expand All

Panels

Collapse All

Panels

Section 1: Introduction to Standard Algorithms


In the ever-evolving world of
computer science, algorithms operate as the centrepiece, offering a structured
pathway to solving complex problems. Specifically, standard algorithms emerge as
tried and tested procedures that address recurrent computational challenges with a
high degree of efficiency.

Their power lies in several features such as optimised resource utilisation, reducing
complexity, offering predictability and consistency, and showcasing adaptability and
scalability.

As we delve deeper into this module, we will illuminate two vital components of
standard algorithms: space complexity and sorting algorithms.

These foundational elements not only enhance your understanding but also equip
you to craft robust and proficient systems in the future.

Section 2: Delving Deeper into Space Complexity and Sorting Algorithms

Space Complexity

When we speak of space complexity, we refer to the quantitative analysis of the


amount of memory an algorithm needs during its execution. This facet is a critical
parameter to assess the efficiency of an algorithm, especially in settings where
memory availability is restricted. Having a firm grasp of space complexity assists
developers in formulating algorithms that solve problems while conserving valuable
memory resources. This section is dedicated to imparting a comprehensive
understanding of space complexity, including methods to calculate it and strategies
to optimise algorithms to minimise memory usage.

Sorting Algorithms

Next, we venture into the realm of sorting algorithms, a category of standard


algorithms specially designed to arrange a collection of items in a specific order,
such as numerical or alphabetical. These algorithms play a pivotal role in diverse
fields, facilitating the organisation of voluminous datasets, enhancing data retrieval
processes, and bolstering the efficient functioning of other algorithms.

Section 3: Case Study - Optimising an E-Commerce Warehouse

In the bustling hub of a thriving e-commerce


business, the warehouse stands as the nerve centre, managing a large range of
products ready to be dispatched to customers.

The company noticed a rising trend in delayed deliveries and identified that the
primary cause was the disorganised product retrieval process within the warehouse.
This led to the decision to optimise the warehouse operations using suitable
standard algorithms.

Objective

To streamline the warehouse operations by implementing an effective product sorting


system, which would enable quicker retrieval of products and consequently faster
deliveries.
Implementation

The team decided to implement a blend of sorting algorithms to organise the


products more efficiently within the warehouse. Here is a breakdown of the
strategies employed:

Bubble Sort Algorithm

● Application: Used for sorting less frequently bought items where the data
set is relatively small.
● Benefit: Simplicity and ease of implementation.

Merge Sort Algorithm

● Application: Applied to sort the products based on various categories such


as price or popularity, especially when dealing with larger datasets.
● Benefit: Offers a more efficient and faster solution for larger datasets
compared to bubble sort.

Quick Sort Algorithm

● Application: Utilised for dynamic sorting requirements, where products


needed to be sorted quickly based on changing criteria, such as seasonal
demands.
● Benefit: Fast and efficient, particularly with large datasets, adapting well to
varying sorting criteria.

Moreover, to further optimise the space utilisation in the warehouse, the team
analysed the space complexity of various processes and implemented strategies to
minimise memory usage in the warehouse management system.

Outcome
Post-implementation, the warehouse witnessed a substantial reduction in product
retrieval time, resulting in faster order processing and happier customers. The
meticulous sorting facilitated easy and quick access to products, optimising the
workflow and enhancing the overall efficiency of the warehouse.

Topic 3. Pass-by-Value Parameter Passing

Key takeaways from this topic...

1. Pass-by-value doesn’t allow a function to modify the caller's variables directly,


facilitating the original data to be kept unchanged.
2. Using pass-by-value doesn’t save memory, as it creates copies of data
including large data structures.
3. Pass-by-value can introduce fewer errors, as changes to the variable within
the function don’t affect its original value.

Learner Guide

The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.

Expand All

Panels

Collapse All

Panels

Section 1: Pass-By-Value Parameter Passing


In programming, particularly in languages such as C++, parameter passing is a
technique where you provide inputs to functions. There are several methods to pass
parameters, and one of the most commonly used methods is "pass-by-value". This
method doesn’t facilitate the efficient use of memory but allows the data to be
protected as the function is unable modify the original data.

Understanding Pass-By-Reference

Pass-by-value is a parameter passing method where a function receives a copy of


the value of the arguments, rather than their original reference. This approach allows
the function to access but doesn’t allow to modify the original variables, which can
enhance the protection of data of programs.

Example

void calculate (int no1, int no2) {

no1 = no1 * 1.5;


no2 = no2 * 2;

int newValue = no1 + no2;

cout << "New value " << newValue << endl;


}

void main() {

int x = 10;
int y = 5;

cout << "x " << x<< endl;


cout << "y " << y<< endl;

calculate(x, y);

cout << "x " << x<< endl;


cout << "y" << y<< endl;
}
In the above example, the original values of x and y will not change.

Further Resources
The resources in this section are for further exploration, providing additional
information for deeper insights.

Expand All
Panels
Collapse All
Panels
Reading: Modular Programming
This resource explores modular programming, a method of designing software by
breaking down functions into independent modules. It emphasises grouping lines of
code into functions, which can be either for program control or specific tasks.

Modular Programming

Reference: Busbee K.L. & Braunschweig D 2024, Modular Programming, rebus Community (online
publication), accessed 18 January 2024,
<https://press.rebus.community/programmingfundamentals/chapter/modular-programming/>

Reading: Modular Programming - Organising Code with Functions


Breaking a complex problem into smaller and more manageable parts is a common
practice. We call each of these parts of a program a module and the process of
subdividing a problem into manageable parts top-down design. This article explores
the principles of top-down design and structured programming.

Organizing Code with Functions

Reference: n.d., 'Organizing Code with Functions', accessed 31 January 2024,


<https://sites.google.com/view/sirallan-programmingtutorials/modular-programming-organizing-code-
with-functions>

Reading: Modularization and C++ Program Layout


This article discusses the concept of modularization in programming, focusing on
C++. It emphasizes the importance of grouping lines of code into units called
functions, which can be included in a program. Functions are categorized into
Program Control (unique to the program being written) and Specific Task (designed
for use in multiple programs).

Modularization and C++ Program Layout

Reference: Kenneth Leroy Busbee n.d., 'Modularization and C++ Program Layout', LibreTexts,
accessed 31 January 2024,
<https://eng.libretexts.org/Bookshelves/Computer_Science/Programming_and_Computation_Fundam
entals/Programming_Fundamentals_-_A_Modular_Structured_Approach_using_C_(Busbee)/02%3A_
Introduction_to_Programming/2.03%3A_Modularization_and_C_Program_Layout>

Reading: Pass-by-value vs Pass-by-reference with C++ examples


This article explains how functions handle values when called in programming. When
calling a function, values can be passed either by value or by reference. The article
includes simple code examples for both pass-by-value and pass-by-reference,
clarifying the concepts with code and comments.

Pass-by-value vs Pass-by-reference — with C++ examples

Reference: Karimov I 2020, Pass-by-value vs Pass-by-reference — with C++ examples, Medium


(website), accessed 18 January 2024,
<https://medium.com/star-gazers/pass-by-value-vs-pass-by-reference-with-c-examples-911e7e81ae6
5>

You might also like