top of page
Search

Arrays

Updated: Jul 16, 2020

As always, I started with watching the tutorial videos. We already had a little intro to this topic this week, and I have a feeling is not going to be that hard. Let's see...



This are my notes on the tutorial videos:

A very important thing about arrays is the lenght, that we did not have with objects or functions. This seems very logical to me.


The default value of an int is 0.

The elements of the array are referred to by index number. You always want to count from 0.

Using an array is the same to using a single variable. x = 0. Line x, y. The thing now is that we have to refer to which element of that array.


All in all the videos were very good and understandable. I really want to get to try some things with my corona. But first, I will also read the chapter of the book on that. I don't thinkg this will be that difficult, though.


These are important notes from the book:

You can think of an array as a list of variables. A list, it should be noted, is useful for two important reasons. Number one, the list keeps track of the elements in the list themselves. Number two, the list keeps track of the order of those elements.




Exercise 9-2: If you have an array with 1,000 elements, what is the range of index values

for that array?

Answer: 0 through 999


One fundamental property of arrays, however, is that they are of fixed size. Once we define the size for an array, it can never change. A list of 10 integers can never go to 11.


Exercise 9-3: Write the declaration statements for the following arrays:

30 integers: int[] arrayOfInts = new int [30];

100 floating point numbers: float[] pointNumbers = new float[100];

56 Zoog objects: myZoog[] = new Zoog[56];


Initializing an Array


One way to fill an array is to hard-code the values stored in each spot of the array.

int[] stuff = new int[3];

arrayName[INDEX]


A second option for initializing an array is to manually type out a list of values enclosed in curly braces and separated by commas.

int[] arrayOfInts = { 1, 5, 8, 9, 4, 5 };

float[] floatArray = { 1.2, 3.5, 2.0, 3.4123, 9.9 };



Now we added Loops to the coding. Till now, everything seems understandable and familiar. I am happy :).


Using a while loop to initialize all elements of an array:

int n = 0;

while (n < 1000) {

values[n] = random(0,10);

n = n + 1;

}


A for loop allows us to be even more concise:

for (int n = 0; n < 1000; n + + ) {

values[n] = random(0,10);

}


An array operation using dot length

for (int i = 0; i < values.length; i + + ) {

values[i] = 0;

}



Exercise 9-7: Rewrite the snake example in an object-oriented fashion with a Snake class.

Can you make snakes with slightly different looks (different shapes, colors, sizes)? (For an

advanced problem, create a Point class that stores an x and y coordinate as part of the sketch. Each snake object will have an array of Point objects, instead of two separate arrays of x andy values. This involves arrays of objects, covered in the next section.)


This one was actually fun to do. A little bit of try and error, but it was very understandable.


Exercise 9-8: Write a Button class (see Example 5-5 f or a non-object-oriented button). The button class should register when a mouse is pressed over the button and change color. Create button objects of different sizes and locations using an array. Before writing the main program, sketch out the Button class. Assume the button is off when it first appears. Here is a code framework:


Button[] buttons = new Button[6];


void setup() {

size(800, 400);

for (int i = 0; i < buttons.length; i++) {

buttons[i] = new Button(i*135+35, height/2-25, 50, 50);

}

}


void draw() {

background(255);

// Here are the buttons displayed

for (int i = 0; i < buttons.length; i++) {

buttons[i].display();

}

}


void mousePressed() {

// When the mouse is pressed

for (int i = 0; i < buttons.length; i++) {

buttons[i].click(mouseX, mouseY);

}

}



class Button {


// Button location and size

float x;

float y;

float w;

float h;

// Button on or off?

boolean on;


// Constructor initializes all variables

Button(float tempX, float tempY, float tempW, float tempH) {

x = tempX;

y = tempY;

w = tempW;

h = tempH;

on = false; // Button always starts as off

}


void click(int mx, int my) {

// Check to see if a point is inside the rectangle

if (mx > x && mx < x + w && my > y && my < y + h) {

on = !on;

}

}


// Draw the rectangle

void display() {

rectMode(CORNER);

stroke(0);

// The color changes based on the state of the button

if (on) {

fill(0,255,0);

} else {

fill(255,0,0);

}

rect(x,y,w,h);

}

}


All in all I found this chapter doable. I think when we have the basic information on how things work on the program, it is easier to understand how to take things a step forward. I think it could be fun to try things and make a lot of corona viruses with arrays.

 
 
 

Comments


© 2020 by Maria Guzman. Proudly created with Wix.com.

bottom of page