Week 7 - Arrays

Note: We will take breaks at least once an hour

Today’s Schedule

  • check-in
  • midterm review
  • help! Classes and Objects
  • arrays!

Midterm Exam

In-person exam on Oct 19. Relax! We will have a study sheet. Your exam and project are together worth 15%.

Subjects to review:

  • Comments
  • Variables
  • Types
  • randomness
  • basic functions
  • println debugging
  • conditionals
  • loops
  • functions
  • arguments
  • return
  • translation, rotation, pushMatrix()/popMatrix()
  • timer with millis()
  • classes and objects
  • arrays

Midterm project

Midterm project is due on Oct 26 (2 weeks).

Midterm Tamagotchi

Tamagotchi

We are a little over a third of the way through the semester and have come far in our programming knowledge. We have covered variables and types, conditionals, loops, functions, translations, interaction through keypresses and the mouse, classes and objects. Next week we will add in arrays to complete the foundations. Taking the totality of what we’ve learned so far our midterm project is to code a Tamagotchi, type of digital pet such as a Furby or artificial human companions.

Furby

Part A due next week:

  1. Sketch out what your Tamagotchi creature looks like in its resting state. Note that your digital pet could be an animal, robot, plant, etc.
  2. Write down the various states your Tamagotchi can take. For example: resting (neutral), sleeping, eating, crying, playing a game, talking. Include at least 3 states and no more than 5.
  3. Draw a flow chart consisting of a finite state machine diagram showing how it progresses between states. For example, bored (fed) -> happy. bored (not fed) -> crying. Indicate what causes it to change from one state to another. This could be a timer or a trigger (“petting” a dog by clicking on it, etc).

dog finite state machine

animal finite state machine

Pacman finite state machine

Weeks ahead: Part B will be to create your creature’s Class in code. Part C will be to implement timers and interactivity to build the state machine.

Arrays

What are they? Arrays are lists. An array is a list of data stored under the same name. You can create an array of any type of data. When you create an array, you can access any of the data stored in that array. We call the location of a piece of data its index.

The first location in an array is always 0. We call that position [0]. The second location is [1]. Etc.

IMPORTANT: Each array has a variable length that returns the total number of elements in the array. Keep in mind that arrays start counting at 0 so an array with 10 elements in (a length of 10) will go from location [0] up to location [9]!

//from Processing tutorials - Arrays

int[] x = { 
  50, 61, 83, 69, 71, 50, 29, 31, 17, 39
};

fill(0);
// Read one array element each time through the FOR loop
for (int i = 0; i < x.length; i++) {
  rect(0, i*10, x[i], 8);
}

array example

Define an Array

You define an array similar to other data types with the addition of brackets [ and ]. When you declare an array you specify what kind of data it will hold. An array can only hold a single data type.

Three ways to define an array

int[] ages; //declaring an array for BTS members' ages

void setup(){
  size(100,100);
  ages = new int[7]; //create space in memory for 7 values
  ages[0] = 27;  //assign
  ages[1] = 26;
  ages[2] = 25;
  ages[3] = 25;
  ages[4] = 24;
  ages[5] = 24;
  ages[6] = 22;
}

OR

int[] ages = new int[7]; //declare and create 

void setup(){
  size(100,100);
  ages[0] = 27;  //assign
  ages[1] = 26;
  ages[2] = 25;
  ages[3] = 25;
  ages[4] = 24;
  ages[5] = 24;
  ages[6] = 22;
}

OR

int[] ages_of_BTS_members = {27, 26, 25, 25, 24, 24, 22};
//declare, create and assign all in one

void setup(){
  size(100,100);
}

Name array example

String[] names = {"Owen","Zev","Anthony","Frankie","Alexander","Brock","Jake","Sofia","Sergio","Tiffany","Maya","Markus","Allan","Khalid"};

void setup(){
  size(100,100);
  println("Total students: "+names.length);
  println("The first student: "+names[0]);
  println("The last student: "+names[names.length-1]);
  //question: why do we specify name.length - 1?
}

in-class challenge

How do pick a random student name from the list?

Resources

  • More about arrays in the Processing reference.
  • An additional chapter about arrays from the book Processing: A Programming Handbook for Visual Designers and Artists.

Using loops to initialize an array

Let’s say we wanted to roll 5 dice.

int[] die = new int[5];
die[0] = random(1,7);
die[1] = random(1,7);
die[2] = random(1,7);
die[3] = random(1,7);
die[4] = random(1,7);
die[5] = random(1,7);

Instead, let’s use a loop:

int [] die = new int[5];

for (int i = 0; i < die.length; i++){
    die[i] = int(random(1,7)); 
    //must convert from float to int. random produces floats!
}

Remember .length tells us how long the array is

In-class

  • Create an array of 1000 floats. Initialize every element with random number between 0 and 10.

  • Create an array of 10 integers. Add to each number the number that follows in the array. Skip the last number.

  • Create and initialize an array of 100 integers. Calculate the sum of all elements in the array.

More on arrays

  • Arrays are declared similarly to other data types but use a bracket to indicate a list of elements.
  • After declaring an array, use new to create the array. data = new int[5];
  • Array’s individual elements may have their value changed, but you cannot change the length of the array.

Watch

Watch The Coding Train videos on Arrays.