Processing+-+Arrays

Imagine if you had a game where you had 10 objects (some red, some blue) bouncing around the screen and the user had to click on the red ones. This wouldn't be too bad, would it? (Try programming it right now - you may want to start by getting it working with one object moving randomly and detecting when you clicked on that one.)

You'll quickly find that you have lots of repeated variables, even if you just kept track of where the red ones were. It would look something like: code format="java" int x1; int y1; int x2; int y2; int x3; int y3; int x4; int x4; int x5; int y5; code (and that's just for the variable declaration!)

This is an example of where a **data structure** would be useful.

A **data structure** is a collection of data. There are many, many different types of data structures used in software applications. Data structures store information and allow for accessing and changing any pieces of information that are held by the data structure.

Data structures can be **random**, when the program can access and change any piece of data, or **sequential**, when the program only has access to data in a specific order.

One of the most common and simple random data structures used in programming is the **array***. It is so simple, that it has its own special syntax (code grammar) in Processing (and Java).

An array is like a row of variables of one type all stored together (and in C, it actually was a set of variables, stored in a row). Each element in the array can be accessed by an **index**, which is the address for the element. **Index** numbers start at 0 and go up to the **length** (size) of the array.

To create a new array of int variables, we would write: int [] nums;

and to make it have 50 spots for storage, we would write: nums = new int [50]; (possibly in setup or maybe draw).

To add new numbers to the array we could do something like: code format="java" int [] nums; void setup { nums = new int[50]; nums[0] = 10; //the first element stores 10 nums[5] = -5; //the sixth element stores -5 nums[3] = nums[0] + nums[5]; //hey, we can treat them //like variables! nums[3] stores 5 println(nums[3]); } code If we want to set each element to hold something at the beginning (**initialize each element**), we could do something like this: code format="java" int [] nums; void setup { nums = new int[5]; //I'm not writing out 50 elements nums[0] = 10; //the first element stores 10 nums[1] = 5; //the second element stores 5 nums[2] = 0; //etc.... nums[3] = -5; nums[4] = -10;//note that this is the last element! } code but, you can see that we haven't saved much here from our original problem, and this would be a big mess if we had 50 or 100 or 1 000 000 elements. (Don't worry - there's a nice solution coming soon!)

Arrays are very handy, in that they allow us to access any element by just calling out its index number. So, let's say we wanted to pick a random element. Then, we could do something like: code format="java" int [] nums; void setup { nums = new int[5]; //I'm not writing out 50 elements nums[0] = 10; //the first element stores 10 nums[1] = 5; //the second element stores 5 nums[2] = 0; //etc.... nums[3] = -5; nums[4] = -10;//note that this is the last element! int rand = int(random(4)); //random from 0 to 4 println(nums[rand]); } code Then a random element would be printed out.

Stop: You try! Create an array that stores a list of 10 of your favourite friends. Then make a random friend picker to pick out one friend from your list and print out their name.

So that's kind of cool, but an even cooler way to use this idea of variable names for index numbers is in a **loop!** Here's how we could print out the example above: code format="java" int [] nums; void setup { nums = new int[5]; //I'm not writing out 50 elements nums[0] = 10; //the first element stores 10 nums[1] = 5; //the second element stores 5 nums[2] = 0; //etc.... nums[3] = -5; nums[4] = -10;//note that this is the last element! for(int i=0; i<5; i++) { print(nums[i] + ", "); } } code (You should really be trying this!)

Can you make your friend picker print out all of your friends names?

Ok, now we have enough to do some fun things! Exercises to practice: 1. Make a program to fill an array of 50 ints with numbers in a pattern (like we started doing above). Write code to print it out nicely. 2. Make an array of ints and fill it. Write a program that will add up all of the values and print out the total. 3. Modify your program from exercise 2 to find the average of the elements in the array. 4. Write code to find the maximum value in an array.


 * Note: Arrays show up often in C-like languages (like Java, C++, etc.) but are not as common in other languages, like Python, LISP, etc. However, these languages also have similar data structures, so learning about arrays is useful, even in other languages. Some languages have arrays with different rules (like index numbers that don't start at 0, etc.)