Robert Sedgewick and Kevin Wayne
Princeton University
wayne@princeton.edu
In one version of the assignment, students use parallel arrays to maintain the position,
velocity, and mass of each body. Calculating the net forces involves nothing more than a
double nested loop; updating the velocities and positions are simple loops, as is drawing
the bodies.
There are limitless opportunities for creativity and enrichment,
including modifying the laws of gravity, handling collisions, adding a third dimension,
or creating a new universe.
The assignment is nifty because n-body simulation is an authentic and archetypical example of scientific computing. A small amount of code produces striking visualizations.
Summary | Students compose a program to simulate the motion of n particles, subject to Newton's laws of motion. |
Topics | arrays, loops, animation, scientific computing |
Audience | early CS1 or early CS 2 |
Difficulty | easy |
Strengths |
|
Weaknesses |
|
Dependencies | Requires a drawing library to draw and animate images, such as StdDraw.java for Java or stddraw.py for Python. |
Variants |
Many variations of this assignments are suitable for CS1 and CS2.
|