Get Help
Arrays
A Bunch of Variables All Linked Together
Arrays are groups of variables, one after another. Here’s what we’ll be covering on this page: You can use any variable type for your array. If we think about our example of variables being like containers then arrays of our three different variable types might look like this-Declaring an Array
When you declare an array, you must declare both its variable type and size:
1 |
char charArray[6]; // an array named charArray with six spaces to hold info |
1 2 3 |
char charArray[6] = {'h','e','l','l','o','!'}; //or int intArray[6] = {1, 2, 3, 4, 5, 6, 7); |
Accessing Information Inside the Array
Once you declare an array, you can access any variable inside of it by using that variable’s index. An index is which place the variable is in the array. Remember: counting with programming starts with 0, so when you access the first item, use the index number 0. The second item would be 1, the third would be 2, and so on. This means that running this code:
1 |
sparki.println( charArray[0] ); |
1 |
sparki.println(charArray); |
1 2 |
charArray[6] = ' '; sparki.println( charArray ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <Sparki.h> // include the sparki library void setup() // code inside these brackets runs first, and only once { } void loop() // code inside these brackets runs over and over forever { sparki.clearLCD(); // clear the LCD char charArray[6] = {'h','e','l','l','o','!'}; sparki.println( charArray[0] ); // print the first item in array //sparki.println( charArray ); // try to print the array charArray[6] = ' '; // put an end in the array sparki.println( charArray ); // print the array with an end sparki.updateLCD(); // draw new info on LCD } |
Searching Through Arrays
Another common way you will see programmers poking around inside arrays is with “for” loops. Often programmers will use this type of code to search through an array looking for a certain value. We won’t cover “for” loops in this lesson but it’s important that you see this kind of code and get a little more comfortable with it. If you’re feeling overwhelmed by arrays already feel free to skip this portion of the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <Sparki.h> // include the sparki library char foundLetter; // variable to hold the letter we are looking for void setup() // code inside these brackets runs first, and only once { } void loop() // code inside these brackets runs over and over forever { sparki.clearLCD(); // clear the LCD char charArray[6] = {'h','e','l','l','o','!'}; charArray[6] = ' '; // put an end in the array (in a new variable) sparki.println( charArray ); // print the array with an end //Start of code that looks through the array for(int i = 0; i <= 7; i++) // this line looks through all seven values of the array { if(charArray[i] == 'l' || charArray[i] == 'L') // asks the question- is this value one of the letters we are looking for? { foundLetter = charArray[i]; // store the letter sparki.println(i); // print the index number of the letter we found i = 8; //skip to the end of for loop } } //End of code that looks through the array sparki.println(foundLetter); sparki.updateLCD(); // draw new info on LCD } |
1 2 3 4 5 |
if(charArray[i] == 'l' || charArray[i] == 'L') // asks the question- is this value one of the letters we are looking for? { foundLetter = charArray[i]; // store the letter sparki.println(i); // print the index number of the letter we found |
1 |
if(charArray[i] == 'l' || charArray[i] == 'L') // change l and L to whatever you want |