CS 1713 Introduction to Programming II Spring 2012 Daily Answers


Class 27:

  1. Assuming that positive means greater than or equal to 0:
    public static largestPositive(int[][] list) {
       int max = -1;
       for (int i=0;i<list.length;i++)
          for (int j=0;j<list[i].length;j++)
             if (list[i][j] > max)
                max = list[i][j];
       return max;
    }
    
  2. public static char getTurn(char[][] board) {
       int count = 0;
       for (int i=0;i<board.length;i++)
          for (int j=0;j<board[i].length;j++)
             if (board[i][j] != EMPTY_CHAR)
                count++;
        if (count == board.length*board.length)
           return DRAW_CHAR;
        if (count % 2 == 0)
           return X_CHAR;
        return O_CHAR;
    }