Skip Top Navigation Bar

Write Code Segments that Require Two-Dimensional Arrays and iteration.

Your Turn

  • Write program code to complete the provided tasks.
  • Run the code in the Java Playground to see if your code generates the desired result.
  • Make necessary changes until you have satisfied the tasks.

Practice 1: Classroom Scores

  • Given a double[][] scores where each sub-array represents the test scores for a student, write code to print all scores for the third student.
  • Try the code in the Java Playground to verify your prediction.

Practice 2: Gray-Scale Inverse

  • You have a 2D int array representing a grayscale image (values 0-255). Write code to invert the image (replace every value with 255 - value).
  • Try the code in the Java Playground to verify your prediction.

Practice 3: Transpose a Matrix

  • Given an int[][] matrix of size 2 x 3, write a code segment to create and print its transpose (a 3 x 2 array).
  • Try the code in the Java Playground to verify your prediction.

Practice 4: Monthly Sales

  • Given a double[][] sales where columns represent months, write code to compute total sales for the month at index 3.
  • Try the code in the Java Playground to verify your prediction.
  • Modify your code to report the monthly sales for all twelve months. Run your code to verify it is correct.

Practice 5: Main Diagonal

  • Given a square int[][] matrix, write a loop to set all elements on the main diagonal to zero. The main diagonal is from the upper left to lower right corner of the matrix.
  • Try the code in the Java Playground to verify your prediction.
  • Modify the code to set all elements on the secondary diagonal to 10. The secondary diagonal is from the lower left to the upper right corer of the matrix. Run your code to verify it is correct.