Iterating Over an Array
Suppose you are given an array representing a student's test grades for the year:
To print out the grades, you will recall that we write a for loop which iterates through each of the array's indices:
The resulting output looks like:
You can accomplish a lot by iterating over arrays. You are not limited to simply printing out the contents!
More Things to do Using Iteration!
What else can you do with a list of grades? To start, you might be interested to know what the student's average exam grade is. With iteration, calculating the average is easy!
In our calculateAverage
function, we start by declaring our variables. We set the initial values of total
and average
to 0. The arrayLength
variable represents the total number of grades in the array. In this particular example, the examGrades
array has 11 total grades, so arrayLength
is equal to 11.
After setting up our variables, we loop through the array and add all of the grades together. The resulting sum is stored into total
. To compute the average
, we just divide the total
by the number of grades in the array, arrayLength
. The program will print:
Flipping Coins
In this program, we use an array to store a large amount of coin flips.
In the start()
function, we first call our flipCoins()
function. The flipsCoins()
function populates a new array with coin flips. The resulting array returned by flipCoins()
is then printed to the screen.
In the flipsCoins()
function, we start by initializing a new array called flips
. We then create a for loop which loops from 0 up until NUM_FLIPS
. In this particular example, we set the number of flips to 100. For each iteration in the for loop, we randomly push a value of either "Heads" or "Tails" into the flips
array. Randomizer.nextBoolean()
has a 50% chance of returning either true or false. Thus, we have a 50% chance of either pushing "Heads" or "Tails" into the array. We finish the function off by returning the array of coin flips.
Here is an example of what might get printed. To save space, we have omitted the coin flips from indices 8 - 92.
This is just one example of many. Since each of the coin flips have a 50% chance of either being "Heads" or "Tails", these values will change each time you run the program.
Lots of Crazy Balls
In this program, we create an array of circles. We use array iteration to make the circles flash and dance around the screen.
Like all good programs, we start by declaring some useful constants at the top. We use RADIUS
to represent the circle's radius, NUM_CIRCLES
to represent the number of circles, and DELAY
to represent the animation delay. We also need to make use of a global circles
array, so we can access it across multiple functions.
In the start()
function, we call a createCircles()
function which populates the circles
array with circles. We then use a timer to call the goCrazy()
function every DELAY
milliseconds. The goCrazy()
function randomly moves each of the circles in the array and makes them change color.
The result looks like:
Last updated
Was this helpful?