Other 2013 FR Questions FR other years Be Prepared Home
A-4
Part (a)
  public SkyView(int numRows, int numCols, double[] scanned) 1
  {
    view = new double[numRows][numCols];
    int row = 0, col = 0, step = 1;
    for (double amtLight : scanned)
    {
      view[row][col] = amtLight;
      int nextCol = col + step;
      if (nextCol >= 0 && nextCol < numCols)
        col = nextCol;
      else
      {
        row++;
        step = -step;
      }
    }
  } 2
Notes:
  1. There is a typo in the statement of this question (Page 17):
    SkyView(4, 3, values) and SkyView(3, 2, values)
    probably was meant to be SkyView(4, 3, scanned) and SkyView(3, 2, scanned).  Thanks to Doug Vermes for mentioning this to us.

  2. There are many different acceptable solutions to this part of the question — too many to list here.  For example, you can get rid of the step variable and use 1 - 2*(row % 2) instead:
        view = new double[numRows][numCols];
        int row = 0, col = 0;
        for (double amtLight : scanned)
        {
          view[row][col] = amtLight;
          int nextCol = col + 1 - 2*(row % 2);
          if (nextCol >= 0 && nextCol < numCols)
            col = nextCol;
          else
            row++;
        }
    Or use an if statement:
        view = new double[numRows][numCols];
        int row = 0, col = 0;
        for (double amtLight : scanned)
        {
          view[row][col] = amtLight;
          int nextCol = col;
          if (row % 2 == 0)
            nextCol++;
          else
            nextCol--;
          if (nextCol >= 0 && nextCol < numCols)
            col = nextCol;
          else
            row++;
        }
    Or you can fill the even rows and the odd rows in separate loops.  Or for each element
    view[row][col] calculate the location of the corresponding element scanned[i]:
        view = new double[numRows][numCols];
        for (int row = 0; row < numRows; row++)
        {
          for (int col = 0; col < numCols; col++)
          {
            int i = row * numCols;
            if (row % 2 == 0)
              i += col;
            else
              i += numCols - col - 1;
            view[row][col] = scanned[i];
          }
        }


Part (b)
  public double getAverage(int startRow, int endRow,
                           int startCol, int endCol)
  {
    double sum = 0.0;
    for (int r = startRow; r <= endRow; r++)
      for (int c = startCol; c <= endCol; c++)
        sum += view[r][c];
    return sum / ((endRow - startRow + 1) * (endCol - startCol + 1));
  } 1 
Notes:
  1. Or count the number of elements in the region:
        double sum = 0.0;
        int count = 0;
        for (int r = startRow; r <= endRow; r++)
        {
          for (int c = startCol; c <= endCol; c++)
          {
            sum += view[r][c];
            count++;
          }
        }
        return sum / count;

Other 2013 FR Questions | Back to Contents

Copyright © 2013 by Skylight Publishing
support@skylit.com