4.13 Implementing 2D Array Algorithms

6 阅读2分钟

1. Exam Points

  • Implement 2D array algorithms using selection and loop statements:
    • determine a minimum or maximum value
    • compute a sum or average
    • determine if at least one element has a particular property
    • determine if all elements of the 2D array have a particular property.
    • determine the number of elements in the 2D array
    • access all consecutive pairs of elements
    • determine the presence or absence of duplicate elements
    • shift or rotate elements in a row left or right or in a column up or down
    • reverse the order of the elements in a row or column

2. Knowledge Points

(1) Implementing 2D Array Algorithms

  • There are standard algorithms that utilize 2D array traversals to:
    • determine a minimum or maximum value of all the elements or for a designated row, column, or other subsection.
    • compute a sum or average of all the elements or for a designated row, column, or other subsection.
    • determine if at least one element has a particular property in the entire 2D array or for a designated row, column, or other subsection.
    • determine if all elements of the 2D array or a designated row, column, or other subsection have a particular property.
    • determine the number of elements in the 2D array or in a designated row, column, or other subsection having a particular property.
    • access all consecutive pairs of elements.
    • determine the presence or absence of duplicate elements in the 2D array or in a designated row, column, or other subsection.
    • shift or rotate elements in a row left or right or in a column up or down
    • reverse the order of the elements in a row or column.

(2) Examples

  • Example 1: compute a sum or average
    int[][] nums = { 
            { 1, 2, 3 }, 
            { 4, 5, 6 } 
    };
    
    double sum = 0;
    
    // i for row index, j for column index
    for (int i = 0; i < nums.length; i++) {
    
        for (int j = 0; j < nums[0].length; j++) {
            sum += nums[i][j];
        }
    }
    
    System.out.println("sum: " + sum);
    	
    int rows = nums.length;
    int cols = nums[0].length;
    int n = rows * cols;
    	
    double avg=sum/n;
    System.out.println("average: "+ avg);
    
  • Example 2: find a maximum or minimum value
    int[][] nums = { 
        { 11, 2, 31 }, 
        { 42, 15, 26 } 
    };
    
    int max = nums[0][0]; // or Integer.MIN_VALUE
    
    for (int i = 0; i < nums.length; i++) { // i: row index
    
        for (int j = 0; j < nums[i].length; j++) { // j: column index
    
            if(nums[i][j] > max) { // if a greater value if found
                max = nums[i][j]; // update max
            }
        }
    }
    
    System.out.println("max: " + max);
    
  • Example 3: Check if all the elements are odd
    int[][] nums = { 
        { 11, 13, 31 }, { 41, 15, 21 } 
    };
    
    // suppose all the elements are odd
    boolean allOdd = true; 
    
    // i: row index, j: column index
    for (int i = 0; i < nums.length; i++) {
    
        for (int j = 0; j < nums[0].length; j++) {
    
            if (nums[i][j] % 2 == 0) { // if an even number is found
                allOdd = false; // update allOdd
            }
        }
    }
    
    System.out.println("all odd: " + allOdd);
    

3. Exercises