Programming for problem solving and projects – Week 9 focus
Download the Lessonotes Mobile South Africa app for faster lesson access on Android and iPhone.
Subject: Information Technology
Class: Grade 12
Term: 1st Term
Week: 9
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 deeper into programming for problem-solving and project development, focusing on efficient algorithm design and debugging techniques, particularly within the context of data structures (arrays and potentially linked lists if covered earlier in the year). In South Africa, proficiency in programming is crucial for contributing to the growing tech industry, developing solutions for local challenges (like load shedding management or agricultural optimization), and participating in the global digital economy.
Arrays: A Refresher An array is a fundamental data structure that stores a fixed-size sequential collection of elements of the same data type. Think of it like a row of numbered lockers at school. Each locker (element) holds one item (data), and you can access it by its number (index). Arrays are essential for organizing and manipulating data efficiently. In many programming languages (Python, Java, C++, etc.), array indices usually start at
0. Algorithm Design for Array Manipulation When working with arrays, efficient algorithm design is critical.
We need to consider: Searching: Finding a specific element in the array.
Sorting: Arranging the elements in a particular order (e.g., ascending or descending).
Insertion: Adding a new element to the array.
Deletion: Removing an element from the array.
Traversal: Visiting each element in the array. Searching Algorithms (Linear Search) The simplest search algorithm is linear search. It involves checking each element of the array sequentially until the target element is found or the end of the array is reached.
Example: Searching for the number 25 in the array `[10, 15, 20, 25, 30, 35]`. We start by comparing 25 with 10, then 15, then 20, and finally find it at index
3. Python Code Example (Linear Search): ```python def linear_search(arr, target): """ Performs a linear search in an array.
Args: arr: The array to search. target: The element to search for.
Returns: The index of the target element if found, -1 otherwise. """ for i in range(len(arr)): if arr[i] == target: return i # Target found at index i return -1 # Target not found my_array = [10, 15, 20, 25, 30, 35] search_element = 25 index = linear_search(my_array, search_element) if index != -1: print(f"Element {search_element} found at index {index}") else: print(f"Element {search_element} not found in the array") ``` Sorting Algorithms (Bubble Sort) Bubble Sort is a simple sorting algorithm that repeatedly steps through the array, compares adjacent elements and swaps them if they are in the wrong order. The largest element "bubbles" to the end of the array in each pass.
Example: Sorting the array `[5, 1, 4, 2, 8]` in ascending order.
Pass 1: ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Swap since 5 > 1 ( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2 ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), No swap since 5 ( 1 4 2 5 8 ), No swap since 1 ( 1 2 4 5 8 ), Swap since 4 > 2 ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ), No swap since 4 ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Pass 4: ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Python Code Example (Bubble Sort): ```python def bubble_sort(arr): """ Sorts an array using the bubble sort algorithm.
Args: arr: The array to sort. """ 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 my_array = [5, 1, 4, 2, 8] bubble_sort(my_array) print("Sorted array:", my_array) ``` Debugging Techniques Debugging is a crucial skill for any programmer.
Here are some common techniques: Print statements: Inserting `print()` statements at strategic locations in your code to display the values of variables and track the flow of execution.
Breakpoints: Setting breakpoints in your IDE (Integrated Development Environment) to pause the execution of your code at a specific line. This allows you to inspect the values of variables and step through the code line by line.
Variable inspection: Using your IDE's debugger to examine the values of variables during execution.
Trace tables: Manually tracing the execution of your code on paper to identify errors in logic. Example of Debugging with Print Statements: Let's say our Bubble Sort isn't working correctly, and we want to debug it. We can add print statements to see the array's state after each swap. ```python def bubble_sort(arr): """ Sorts an array using the bubble sort algorithm with debug print statements.
Args: arr: The array to sort. """ n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1] : print(f"Before swap: arr[{j}]={arr[j]}, arr[{j+1}]={arr[j+1]}") # Debug Print arr[j], arr[j+1] = arr[j+1], arr[j] # Swap elements print(f"After swap: arr[{j}]={arr[j]}, arr[{j+1}]={arr[j+1]}") # Debug Print print(f"End of pass {i+1}: {arr}") # Debug Print my_array = [5, 1, 4, 2, 8] bubble_sort(my_array) print("Sorted array:", my_array) ``` By looking at the output of these print statements, you can see exactly what's happening during each step and identify any logical errors in your algorithm. Guided Practice (With Solutions)
Question 1: Write a Python function that takes an array of integers as input and returns the sum of all the even numbers in the array.
Solution: ```python def sum_even_numbers(arr): """ Calculates the sum of even numbers in an array.
Args: arr: The array of integers.
Returns: The sum of the even numbers in the array.