COP 2500 Spring 2012

Lab #8 2D Arrays

Home Labs Lecture Notes
 

Deliverables: To complete this assignment you must --
1.) Write the HTML and JavaScript page described below.

Introduction: The goal of this assignment is to encourage you get comfortable with 2D arrays and indexing schemes.
A 3x3 2D array:
Arrays can contain anything, even other arrays. In JavaScript, 2D arrays are allocated with a single array, where each element contains an array.
Declare a 2D array:
var matrix = new Array(numrows);
for(row = 0; row < numrows; row++)
matrix[row] = new Array(numcols);

Elements can be accessed with [] just like with 1D arrays, but since each row in the 2D array contains an array, accessing is with row, then column:
matrix[row][col]

Initialize a 2D array by iterating over each row and each column:
for(row = 0; row < numrows; row++)
{
for(col = 0; col < numrows; col++)
matrix[row][col] = #
}

Procedures:

  1. Using your text editor create a new document named "cop2500lab8.html".

  2. PART 1:
  3. You are to convert a 2d array specification, namely copying writing a Z shape, into a program.
    Note that the Z can be broken into segments where rows are related to columns in some defined way.
    Row 0 of Z has all set, just like the last row of the Z.
    The antidiagonal of the Z is another segment.
    Discover the relationship between the row and column by examining pairs:
    row: 0 col: 4
    row: 1 col: 3
    row: 2 col: 2
    ...
  4. Section Specification Comments
    INPUT: M A 5x5 2D array
    INITIALIZATION: Set all elements of M to 0
    COMPUTATION:
    11111
    00010
    00100
    01000
    11111
    Set Z shape to 1, all else leave as 0.
    OUTPUT: Iterate over rows of M, outputting each row. Display M with document.write

PART 2:
  1. You are to convert a 2d array specification, namely copying writing an N shape, into a program.
    Note that the N can be broken into segments where rows are related to columns in some defined way.
    Column 0 of N has all set, just like the last column of the N.
    The diagonal of the N is another segment.
    Discover the relationship between the row and column by examining pairs.
  2. Section Specification Comments
    INPUT: M A 5x5 2D array
    INITIALIZATION: Set all elements of M to 0
    COMPUTATION:
    10001
    11001
    10101
    10011
    10001
    Set N shape to 1, all else leave as 0.
    OUTPUT: Iterate over rows of M, outputting each row. Display M with document.write

  3. Open a browser and verify that the file displays properly. In order to obtain full credit for the lab you need to email your source code to your grader.