Advanced programming: modularisation and data structures (Grade 11 focus) – Week 5 focus
Download the Lessonotes Mobile South Africa app for faster lesson access on Android and iPhone.
Subject: Information Technology
Class: Grade 11
Term: 3rd Term
Week: 5
Theme: General lesson support
This page supports the lesson note with a companion video and a short classroom-ready summary.
For class groups and homework, share this lesson page so learners also get the summary, objectives, and full lesson context.
This week, we delve into advanced programming concepts: modularisation and data structures. Think of building a house. You wouldn't build the entire house at once, would you? You'd break it down into smaller parts like the foundation, walls, roof, electrical wiring, and plumbing. Each part is like a module, performing a specific task. Similarly, we need ways to efficiently store and organize information – this is where data structures come in. Imagine trying to find a specific textbook in a library without any organization! These concepts are essential for writing efficient, maintainable, and scalable programs.
2.1 Modularisation Modularisation is the process of dividing a program into smaller, independent, and manageable modules (also called functions, procedures, or subroutines). Each module performs a specific task. Why Modularise?
Code Reusability: Modules can be reused in different parts of the program or even in different programs. Imagine writing a module to calculate the average rainfall in a region. You can reuse this module for different regions or for different years.
Maintainability: If a problem occurs, it's easier to locate and fix it within a specific module instead of searching through the entire program. Think of a fault in the electrical wiring of a specific room in your house – you don't have to rewire the whole house!
Readability: Modularised code is easier to understand and follow because it's broken down into logical units. A well-structured program is like a well-organized essay – easier to read and understand.
Collaboration: Multiple programmers can work on different modules simultaneously, speeding up the development process. Think of a team building a house – different teams work on different parts like the foundation, walls, and roof.
Reduces Complexity: Breaking down a complex problem into smaller, manageable parts makes it easier to solve.
Example (Python): ```python Module to calculate the area of a rectangle def calculate_rectangle_area(length, width): """Calculates the area of a rectangle.""" area = length * width return area Module to calculate the perimeter of a rectangle def calculate_rectangle_perimeter(length, width): """Calculates the perimeter of a rectangle.""" perimeter = 2 * (length + width) return perimeter Main program length = 10 width = 5 area = calculate_rectangle_area(length, width) perimeter = calculate_rectangle_perimeter(length, width) print("Area:", area) print("Perimeter:", perimeter) ``` In this example, `calculate_rectangle_area` and `calculate_rectangle_perimeter` are modules (functions). They perform specific tasks and can be reused. 2.2 Data Structures: Arrays (Lists) An array (or list in Python) is a fundamental data structure that allows you to store a collection of elements of the same data type under a single variable name. Each element in the array can be accessed using its index (position). Why Use Arrays?
Organised Storage: Arrays provide an organised way to store and access related data. Imagine storing the marks of all students in a class.
Efficient Access: You can quickly access any element in the array using its index.
Iteration: Arrays are easily iterated over using loops, allowing you to perform operations on all elements.
Example (Python): ```python Creating an array of student names student_names = ["Sipho", "Aisha", "Thando", "David"] Accessing the first element (Sipho) print(student_names[0]) Accessing the third element (Thando) print(student_names[2]) Looping through the array for name in student_names: print(name) ``` Array Operations: Adding elements: `append(element)`: Adds an element to the end of the array. `insert(index, element)`: Inserts an element at a specific index.
Deleting elements: `remove(element)`: Removes the first occurrence of a specific element. `pop(index)`: Removes the element at a specific index.
Searching: Linear Search: Iterates through the array to find a specific element. (Time complexity: O(n))
Sorting: Bubble Sort: Repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. (Time complexity: O(n^2))
Example (Array Operations in Python): ```python Creating an array of numbers numbers = [10, 20, 30, 40] Adding an element to the end numbers.append(50) print(numbers) # Output: [10, 20, 30, 40, 50] Inserting an element at index 1 numbers.insert(1, 15) print(numbers) # Output: [10, 15, 20, 30, 40, 50] Removing the element 20 numbers.remove(20) print(numbers) # Output: [10, 15, 30, 40, 50] Removing the element at index 2 numbers.pop(2) print(numbers) # Output: [10, 15, 40, 50] Linear Search def linear_search(arr, target): for i in range(len(arr)): if arr[i] == target: return i # Return the index if found return -1 # Return -1 if not found index = linear_search(numbers, 40) print(f"Index of 40: {index}") Bubble Sort def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] # Swap elements bubble_sort(numbers) print(f"Sorted array: {numbers}") # Output: Sorted array: [10, 15, 40, 50] ``` Guided Practice (With Solutions)
Question 1: Write a Python function called `calculate_average` that takes an array of numbers as input and returns the average of those numbers.