Extra Credit - Write FRQ on Arrays/2D Arrays

Consider the following code segment:

public static int[] removeDuplicates(int[] arr) {
    int[] result = new int[arr.length];
    int index = 0;
    for (int i = 0; i < arr.length; i++) {
      boolean isDuplicate = false;
      for (int j = 0; j < index; j++) {
        if (arr[i] == result[j]) {
          isDuplicate = true;
          break;
        }
      }
      if (!isDuplicate) {
        result[index] = arr[i];
        index++;
      }
    }
    int[] trimmedResult = new int[index];
    for (int i = 0; i < index; i++) {
      trimmedResult[i] = result[i];
    }
    return trimmedResult;
  }

Assume that the above code segment is executed with the input array arr containing the following values:

int[] arr = { 1, 2, 3, 2, 4, 1 };

Answer the following questions:

What is the output of the removeDuplicates method when called with the input array arr?

What is the purpose of the variable index in the removeDuplicates method?

Suppose that the input array arr contains only non-negative integers. How could you modify the removeDuplicates method to make it more efficient?

int[][] arr = new int[3][4];
for (int i = 0; i < arr.length; i++) {
  for (int j = 0; j < arr[i].length; j++) {
    arr[i][j] = i * j;
  }
}

Assume that the above code segment is executed. Answer the following questions:

What is the length of the array arr?

What is the length of the array arr[1]?

What is the value of arr[2][3]?

What is the value of arr[0][2]?

Write a code segment that prints all the elements of the 2D array arr.

int[][] arr = new int[3][];
arr[0] = new int[] {1, 2, 3};
arr[1] = new int[] {4, 5};
arr[2] = new int[] {6, 7, 8, 9};

Assume that the above code segment is executed. Answer the following questions:

What is the length of the array arr?

What is the length of the array arr[1]?

What is the value of arr[2][3]?

What is the value of arr[1][2]?

Write a code segment that prints all the elements of the 2D array arr.

Hacks for MacroMonkey Lesson

void setArray(int[] arr, int n) {
    for (int i = 0; i < arr.length; i++) {
        arr[i] = n;
        //sets each of the 0 to n, which is 10 in this case
    }
}

int[] array = new int[10];
setArray(array, 10);

for (int i = 0; i<array.length; i++) {
    System.out.println(array[i]);
}
10
10
10
10
10
10
10
10
10
10
public static int average(int[] array) {
    int sum = 0;
    for (int i = 0; i < array.length; i++) {
        sum += array[i];
    } //adds each array element to sum and divides by length of array
    return sum / array.length;
}

//tester array
int[] test = {3, 5, 7, 2, 10};

//returns 10
System.out.println(average(test));
5
public static void main(String[] args){
    int[][] arrayOne = new int[5][7];
    for (int row = 0; row < arrayOne.length; row++){
        for (int col = 0; col < arrayOne[row].length; col++){
            arrayOne[row][col] = row+col - 1;
        }
    }

    System.out.println(arrayOne[4][6]);
}
public static double averageDiagonal (int[][] array2D) {
    // your code here
    int sum = 0;
    int count = 0;
    int row = 0;
    int col = 0;
    
    // Iterate over the diagonal elements of the array
    while (row < array2D.length && col < array2D[0].length) {
        sum += array2D[row][col];
        count++;
        row++;
        col++;
    }
    
    // Calculate and return the average
    return (double) sum / count;
}

int[][] arr = {
    {1,2,3,4,5,6},
    {7,8,9,10,11,12},
    {0,1,2,3,4,5},
    {10,11,12,13,14,15},
    {15,16,17,18,19,20}
    //populating an arr
};

System.out.println(averageDiagonal(arr));
8.6