Two Dimensional Array Worksheet

1.  Given: int[][] values = new int[4][5]
    Write a nested loop to set values as follows:

         [0] [1] [2] [3] [4]
      [0] 1   2   3   4   5
      [1] 1   2   3   4   5
      [2] 1   2   3   4   5
      [3] 1   2   3   4   5


2.  Given: int[][] values = new int[4][5]
    Write a nested loop to set values as follows:

         [0] [1] [2] [3] [4]
      [0] 0   1   2   3   4
      [1] 1   2   3   4   5
      [2] 2   3   4   5   6
      [3] 3   4   5   6   7


3.  Given: int[][] matrix = new int[5][5]
    Write a nested loop to set matrix as follows:

         [0] [1] [2] [3] [4]
      [0] 1   0   0   0   0
      [1] 0   2   0   0   0
      [2] 0   0   3   0   0
      [3] 0   0   0   4   0
      [4] 0   0   0   0   5


4.  A square matrix has the same number of rows and columns. The diagonal
    of a square matrix is made up of the entries which run from the upper
    left corner to the lower right corner. In the previous problem, the
    diagonal is made up of those entries which are equal to 1. A square
    matrix is upper triangular if all entries below the diagonal
    are 0. The diagonal entries and the entries above the diagonal can
    have any value, including 0, but all entries below the diagonal must
    have the value 0. Write a function called isUpper which has parm:
    an array with N rows and N columns. The function should return true
    if the array represents an upper triangular matrix, and return false
    otherwise.


   Given the following array of 5 rows and 5 columns, which contains the
   distances between cities:
    [0] [1] [2] [3] [4]
    Albany Boston Hartford NY Phila
[0] Albany 0 171 115 141 240
[1] Boston 171 0 103 194 333
[2] Hartford 115 103 0 120 235
[3] NY 141 194 120 0 104
[4] Phila 240 333 235 104 0
    Note that the city names are not in the array; the array contains
    the numeric entries only, which give the distance between the two
    cities represented by the row and column.


5.  Write the statements to initialize an array distance with the
    mileage data given above.


6.  Write the statements to print the following menu, read in two
    city numbers, and print the distance between the two cities:

        To determine the mileage between cities, enter the numbers of
        two cities from the following list:
             1:  Albany           4:  NY
             2:  Boston           5:  Phila
             3:  Hartford

        Enter your city numbers:


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2013