CPSC 120 Principles of Computer Science Fall 2025

Lab 9
Arrays

Due: Fri 11/14 at the start of class


Introduction

Your browser does not support the canvas tag.

Loops expand our ability to easily draw lots of similar things when there is some kind of pattern in the positioning, color, size, and orientation of those things — stripes on a road, a flock of birds, a patch of wildflowers, shapes in a quilt block, etc.

Arrays expand our ability to easily animate lots of similar things in similar ways by making it possible to write loops where what is changing from one repetition to the next is (or includes) the animation variables themselves — arrays are used when each thing would need its own set of animation variables because the values may be different for different things (without a pattern).

This week's lab focuses on working with arrays using a development strategy intended to simplify the task of animating lots of things: first figure out the drawing and animating for a single thing, then transform the single-thing solution to a many-thing solution by adding arrays.

Successfully completing this lab means that you are able to:



Handin and Presentation Meeting

Handin

Hand in a hardcopy (paper) of your worksheet in class.

To hand in your sketches:

It is OK if you copy your files to the handin directory at the very beginning of class.

Presentation Meeting

Presentation meetings for this lab will be the week of Nov 17.

Exercise #4 is the presentation problem. Come to the presentation meeting prepared to discuss your sketch. You may be asked to point out and explain how your code meets the requirements of the problem, explain how portions of your code work, and/or apply skills from the problem to a new situation.


Policies

The policies on late work and extensions, academic integrity, and the use of AI for this lab are the same as for lab 2. Review them there.


Preliminaries

Reference

Review the slides, in-class exercises handouts, and in-class exercise solutions from the last week (arrays and array-ifying sketches).


Exercises

  1. In this exercise you'll create a sketch which looks like raindrops landing in a puddle — there should be 50 expanding-and-fading circles and clicking the mouse should toggle the fill color for all of the circles.

    The starting point is a sketch which draws a single expanding circle — the circle starts at a random position with a random size and (grayscale) color, then expands (increasing the size) and fades (making the color more white). When the color is white, the circle is given a new random position and the size is reset to 0 and the color is reset to black. Clicking the mouse toggles the circle's fill color between white and the circle's current color.

  2. To do this:

  3. Your browser does not support the canvas tag.

    In this exercise you'll create an animation similar to the one shown — there should 50 circles, each of which starts in a random position and then, one by one, starts to move after a delay. Stagger the delays so that the first circle starts to move after 40 frames, the next after 80 frames, the next after 120 frames, etc. Click the reset the demo, but you do not need to be able to click to reset your sketch.

    The starting point is a sketch which draws a single circle that starts moving after 200 frames. A countdown timer is used to achieve the delayed start — the pattern for a countdown timer is an int variable initialized to some value greater than 0 and updated by subtracting 1. The action occurs when the countdown timer reaches or drops below 0.

    The countdown timer in the provided code is named timer. Observe that it is initialized to 200 in setup(), so that the ellipse will start moving after 200 frames. Also observe the update and use steps in draw() — the value of timer is decreased by 1 (update) and the ellipse's x coordinate is updated (to move the ellipse) when the countdown has reached 0 (use).


    To do this:

    • Create a new sketch, add your name and a description of the sketch in comments at the beginning, paste in the contents of the provided starter_delayed sketch, and save it as lab9b.

    • Complete the Exercise 2 section of the the lab 9 worksheet.

    • Based on your answers from the worksheet, array-ify the sketch so that there are 50 circles starting 40 frames apart as described above.

  4. In this exercise you'll create an animation of a series of exploding fireworks.

    The starting point is a sketch which animates a single spark in the fireworks display. This is an example of a two-stage animation — there's a launch stage where the spark moves upwards and an explosion stage where the spark fall back towards the ground. The transition between stages is based on the passage of time, so a countdown timer is used — when the timer reaches 0, the current stage changes from launch to explosion or vice versa.

  5. To do this:

    • Create a new sketch, add your name and a description of the sketch in comments at the beginning, paste in the contents of the provided starter_fireworks sketch, and save it as lab9c.

    • Complete the Exercise 3 section of the the lab 9 worksheet.

    • Based on your answers from the worksheet, array-ify the sketch so that there are 1000 sparks.

  6. In this exercise you'll create a sketch of your own design. What the sketch depicts is up to you (here's a chance to be creative!) but for full credit it must include the following elements:

    To do this:

    • Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab9d_starter.

    • Implement the starter version of your sketch — include everything except the bunch of things that will be animated using arrays. For those, animate just a single copy of the thing.

    • Save a copy of your starter version as lab9d.

    • Identify which animation variables should become array varaibles and the initialization, drawing, and upate chunks of code that will be wrapped with loops, then implement the array-ifying process to complete your sketch.

Extra Credit

Challenge yourself and earn extra credit by going substantially beyond the required elements. (See the assignments and evaluation policy for more details on extra credit.)

Some possibilities for extra credit: