Why
To build a robot with legos, you will need to understand how to create and use arrays.
What
In this lesson you will learn about arrays, a very important data structure in coding. You will learn how use them in a simple project for the microbit.
How
You will create a program in Microsoft Makecode that utilizes arrays and builds on the skills you learned about buttons and variables in the last several lessons.
What you will need for this activity:
1. A laptop, desktop, or chromebook
2. An internet connection
3. A Microbit v2
4. USB cable to connect microbit to
computer
The Lesson
1. Open https://www.microsoft.com/en-us/makecode in a web
browser
2. Sign in with your free account by clicking the Sign in in the
top right of the screen
3. After signing in, click on the picture of the micro:bit, this
should take you to https://makecode.microbit.org/
4. Click on the purple New Project rectangle image to create a
new project
5. Enter “arrays” for the name of your project, then
click the green Create button.
Have you ever written down a list of items on paper before? Perhaps it was a shopping list or a todo list? If you think about a list, it has certain things like a count of items, a first element, and a last element.
In programming, there is this same concept. Sometimes they call it a list, and sometimes they call it an array. It is known as a data structure, and it is very useful in all types of programs.
Let’s try to think of a list. Pick your five favorite foods, and order them so that your most favorite food is first, you second most favorite second etc.
In programming, we have this concept called an index. Some programming languages use an index of an array that starts with zero, but most humans use an index of a list that starts with one. What I mean by this is that when we think if the first item of a favorite food list, we call that 1. In most programming languages, they call that an index of zero. In MakeCode, they use an index of zero. When you see an index of zero, just think of it as referring to the first item on the array.
There are different things you can do to an array in MakeCode. You can get the length of an array, you can add items to an array, you can insert items, you can remove items, and you can find where items are in an array.
Challenge
Challenge and solution guide for this lesson is currently only available to subscribers.
Review
In this lesson you learned about arrays and how use them in your program.
Next lesson
In the next lesson you will learn how to make sound with the microbit.