How to sum the corners of a matrix in C#

1 Answer

0 votes
using System;

public class MatrixCornerSum
{
	public static int sumMatrixCorners(int[][] matrix) {
		return matrix[0][0] + 
		       matrix[0][matrix[0].Length - 1] + 
		       matrix[matrix.Length - 1][0] + 
		       matrix[matrix.Length - 1][matrix[0].Length - 1];
	}

	public static void Main(string[] args)
	{
		int[][] matrix = new int[][]
		{
			new int[] {1, 2, 3, 4},
			new int[] {5, 6, 7, 8},
			new int[] {9, 3, 2, 0}
		};

		Console.WriteLine(sumMatrixCorners(matrix));
	}
}



/*
run:
     
14
     
*/

 



answered May 20, 2024 by avibootz

Related questions

1 answer 94 views
94 views asked May 20, 2024 by avibootz
1 answer 147 views
1 answer 150 views
1 answer 119 views
119 views asked May 20, 2024 by avibootz
1 answer 93 views
1 answer 106 views
1 answer 111 views
111 views asked May 19, 2024 by avibootz
...