Microbit Computer Science
Beginner
40 mins
Teacher/Student led
What you need:
Chromebook/Laptop/PC

Introduction to Data Structures

In this lesson, you'll explore the basics of data structures using MakeCode for micro:bit. Learn what data structures are, understand arrays and lists, and practise creating, modifying, adding, and removing elements in arrays through hands-on steps.
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction

    In this lesson, you will explore the fundamentals of data structures, which are essential for organising and managing data in programming. Using MakeCode for micro:bit, you'll learn how to work with arrays to store and manipulate collections of data.

    By the end of this lesson, you will:

    1. Understand what data structures are and their importance in programming.
    2. Learn the differences between arrays and lists, and how arrays function in MakeCode.
    3. Create a simple array and initialise it with values.
    4. Add elements to an array.
    5. Remove elements from an array and observe changes.
    6. Find the position of a specific element in the array.
    7. Modify existing elements in the array.

    2 - What are Data Structures?

    In computer science, data structures are ways of organising and storing data so that it can be accessed and modified efficiently. They are fundamental building blocks for writing programs, allowing you to manage collections of data in structured formats.

    Think of data structures like containers in your kitchen: a cupboard organises plates, a drawer holds cutlery, and a fridge stores food items in shelves. Similarly, data structures help you store, retrieve, and manipulate data effectively.

    Common data structures include arrays, lists, stacks, queues, trees, and graphs. Each has its own strengths and is suited to different tasks. For example, an array is great for storing a fixed number of similar items that you want to access quickly by their position.

    In this lesson, we'll focus on basic data structures like arrays and lists, and you'll learn how to use them in code.

    Understanding data structures is key to solving complex problems, as they help optimise how your programs handle information.

    3 - Understanding Arrays and Lists

    Arrays and lists are two fundamental data structures used to store collections of items in programming.

    Arrays

    An array is an ordered collection of elements, where each element can be accessed directly by its position or index (starting from 0). Arrays are efficient for quick access to items if you know their position, but in some programming languages, their size is fixed when created, meaning you can't easily add or remove elements without creating a new array.

    Lists

    A list, on the other hand, is also an ordered collection, but it is typically more flexible. Lists can grow or shrink dynamically as you add or remove items. There are different types of lists, such as linked lists, which connect elements like a chain, making insertions and deletions easier without shifting other elements.

    The key differences: Arrays offer fast access by index but may have fixed sizes, while lists provide easier modifications but might be slower for random access depending on the implementation.

    In MakeCode for micro:bit, arrays are used and treated as dynamic lists. You can create an array, and then easily add items, remove, and access by index. This means in MakeCode, arrays function like flexible lists, combining the benefits of both. You don't need to worry about fixed sizes here, as they can resize automatically.

    4 - Creating a Simple Array

    In this step, you will create a simple array in MakeCode for micro:bit. This array will store a list of numbers, which we'll use as game scores. We'll initialize it with a few values and display the number of scores to verify it's working.

    Go to makecode.microbit.org and start a new project.

    The purpose of this code is to declare an array called 'scores', add initial values, and show how many items are in the array using the micro:bit display.

    Add the following code:

    let scores: number[] = [10, 20, 30]
    basic.showNumber(scores.length)
    This code creates the array with three scores and displays '3' on the micro:bit. In the next steps, you'll build on this array by adding more functionality.


    5 - Adding to an Array

    Now lets build on the array you created in the previous step by adding functionality to add new scores dynamically. We'll use the micro:bit's button A to add a new score to the array and update the display to show the new length.

    The purpose of this code is to demonstrate how to add elements to an array using the push method, which appends a new item to the end of the array.

    Add the following new code:

    let scores: number[] = [10, 20, 30]
    basic.showNumber(scores.length)
    input.onButtonPressed(Button.A, function () {
        scores.push(40)
        basic.showNumber(scores.length)
    })
    This code starts with the array from the previous step, displaying its initial length (3). When you press button A, it adds 40 to the array using (array) add value (40) to end, increasing the length, and then displays the new length (4). 

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more