GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 15: More About 2-D Arrays


Objectives

By the end of this module, you will:




Before Starting

If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:




Catching Up

Before we move forward, let's catch up (complete any remaining work from the previous module)
In this case, make sure we've got:

  1. Two-Dimensional Arrays... Declaring, Initializing, and Accessing!




2D arrays: Array (table) vs. Cartesian (grid) view

There are two common ways of interpreting and visualizing a 2D array:

  1. As an array with the first row on top, followed by the second row below it and so on.

  2. As a grid that's aligned with the Cartesian x and y axes.
Let's expound with an example: Activity 1: Confirm this by writing up the above in MyArrayPrint.java.

Activity 2: Try the Cartesian printing approach in MyArrayPrint2.java.




Designing Solutions that include 2-D Arrays

 

  public class GradingSpreadsheet {
      public static void main(String[] argv) {
          double[][] studentGrades = {{4, 3, 5, 2, 10},
                                      {8, 9, 9.5, 8, 0},
                                      {10, 10, 9, 9.5, 0}};
          String[] studentNames    = {"Larry", "Curly", "Moe"};

          // your code here!
      }
  }
A simple spreadsheet app -- you have an array of student grades (each row contains the grades for each homework), and the student names. You can see that Larry did something mischievous on the last assignment. Lets do some simple processing:

Activity 3: Add code to print each student's name, and their average grade over all homeworks.

Activity 4: Add code to print the average grade for each homework.

Activity 5: Create an array

double[] weights = {0.2, 0.1, 0.2, 0.3, 0.2};
with the same length as the number of homeworks. Compute a weighted grade, where the final grade for the student is determined by these weights on the homeworks. The formula for each final grade should be:
final = grade_0 * weight_0 + grade_1 * weight_1 + \dots + grade_{n-1} * weight__{n-1}
.



Activity 6: Add code to print, for each of the homeworks, the name of the student that got the highest grade.

Activity 7: Add code to print the standard deviation of the grades for each student. Remember:
\sigma_{student} = \sqrt{ \frac{\Sigma(x_i - \mu)^2}{N}}
. Activity 8: Add code to compute and print the grades "on a curve". How would you do this? Be creative; there are multiple ways

It's all better with methods! If we have the time, let's re-implement these as separate methods that each return the value, or list, as appropriate. Let''s use the file GradingSpreadsheetMethods.java for that purpose.




A Cool Game-Like application of 2-D Grids

Conways Game of Life is a simple cellular automaton, defined in Mathworld as: "A cellular automaton is a collection of "colored" cells on a grid of specified shape that evolves through a number of discrete time steps according to a set of rules based on the states of neighboring cells. The rules are then applied iteratively for as many time steps as desired"

Look at a visual repesentation of the rules

Look at this video: Epic GOL

Play with this GOL simulator

Check out XKCD's post in honor of John Conway