Introduction
Six variables called note1 to note6 are six separate boxes with no relationship except in your head. You cannot ask for "the next one", and adding a seventh means editing every place that touched the other six.
An array is one name holding many values in a row:
int notes[6] = {262, 294, 330, 349, 392, 440};
Now there is a single thing called notes, and you reach any part of it by position: notes[0], notes[1], and so on.
That word position is the whole idea. A position can be held in a variable, and a variable can be counted up — which means a loop can walk the whole array. Six values or six hundred, the code is the same length. Every list you have written out by hand in this course so far — the notes in Happy Birthday, the six answers in the magic 8-ball, the coins in the till — was waiting for this.
The one thing to burn in: arrays start at 0. An array of six has positions 0 to 5. There is no notes[6], and asking for it does not produce an error — it produces whatever byte happens to sit there.
What you will be able to do
By the end of this lesson you can:
- Declare an array and fill it at the same time.
- Read and write any element by its index.
- Say why the last index is one less than the length.
- Work out an array's length with
sizeof. - Recognise the symptoms of reading past the end.
What you need
| Part | Type | Qty |
|---|---|---|
| Arduino UNO R3 | Microcontroller | 1 |
| USB A to B cable | Cable | 1 |
Basic
No wiring. Read everything in the Serial Monitor at 9600 baud.
Declaring one
int notes[6] = {262, 294, 330, 349, 392, 440};
int— every element is anint. An array holds one type only.notes— the name.[6]— how many. This never changes once the sketch is compiled.{...}— the starting values, comma separated.
You may leave the size out when you supply the values:
int notes[] = {262, 294, 330, 349, 392, 440}; // the compiler counts: 6
Or give a size with no values, and fill it later:
int readings[10]; // ten ints, contents unpredictable
Getting at the elements
Serial.println(notes[0]); // 262 — the FIRST one
Serial.println(notes[1]); // 294
Serial.println(notes[5]); // 440 — the LAST one
notes[2] = 999; // write to it
An array of 6 has indexes 0, 1, 2, 3, 4, 5. The last index is always one less than the length.
Think of the index as "how far from the start". The first element is zero steps from the start.
Reading past the end
Challenges
Challenge 1
Print every element the long way.
Declare int notes[6] = {262, 294, 330, 349, 392, 440};
Print all six, one per line, with six separate lines of code — you do not have loops yet.
Then print notes[6] as well. Write down what appears, and one sentence saying why nothing complained.
Finally, print the length using the sizeof trick and check it says 6.
Log in to ask for the answer.
Challenge 2
Change one.
Starting from the same array:
- Print
notes[2]. - Set
notes[2] = 1000; - Print
notes[2]again. - Print
notes[1]andnotes[3]too.
Confirm that changing one element left its neighbours alone.
Then set notes[0] = notes[5]; and print the whole array again. Which elements are now equal, and is the original 262 recoverable?
Log in to ask for the answer.
Challenge 3
Two arrays that belong together.
Make two arrays of four:
char grades[4] = {'A', 'B', 'C', 'D'};
int marks[4] = {90, 80, 70, 60};
Print them paired up:
A = 90
B = 80
C = 70
D = 60
The pairing is only in the index — nothing in the code ties grades[1] to marks[1] except that you used the same number.
Now delete one value from marks so it has only three, and print again. What happens to the fourth line, and what does that tell you about parallel arrays?
Extra challenge
The magic 8-ball, revisited.
Go back to your ARD-PG-04 magic 8-ball — six if statements, six printed answers.
You cannot store text in an int array, so use six char letters standing for the answers, plus a printed key:
char answers[6] = {'Y', 'N', 'M', 'L', 'D', 'W'};
Roll random(0, 6) — note the range — and print answers[roll].
Compare the two sketches side by side. Count the lines.
Think about it: the old version needed a new if for every answer. This one needs one more letter. Which would you rather hand to somebody who wants twenty answers? And what is the one thing the old version could do that this cannot — printing a whole sentence rather than a letter?
Log in to ask for the answer.