Menu
Coddy logo textTech

Recap - Multi-dimensional

Part of the Logic & Flow section of Coddy's C# journey — lesson 7 of 66.

challenge icon

Challenge

Medium

Create a method called processMatrix that:

  1. Takes a jagged array of integers (int[][] matrix) as a parameter
  2. Returns a new jagged array where:
    • Each element is replaced with the sum of its adjacent elements (up, down, left, right)
    • Elements at the edges only count the existing adjacent elements
    • The original matrix should not be modified

For example, given the matrix:

[2, 3, 4]
[5, 6, 7]
[8, 9, 10]

The result should be:

[8, 12, 10]
[16, 24, 20]
[14, 24, 16]

For instance, the value 6 in the middle becomes 24 because its adjacent values are 3, 9, 5, and 7, and 3+9+5+7=24.

Try it yourself

public class ProcessMatrix
{
    public static int[][] processMatrix(int[][] matrix)
    {
        // Write your code here
        
    }
}

All lessons in Logic & Flow