C# Finding 3 largest values in a 2D Array -
currently, have 2d array assigned random values. know how find largest values this:
public static int largest( int[,] c) { int largest = 0; (int row = 0; row < c.getlength(0); row++) { (int col = 0; col < c.getlength(1); col++) { if( largest < c[ row, col]) { largest = c[ row, col]; } } } return largest; } but want find 3 largest values , return sum. don't have restrictions in how find it, going method similar largest method wrote?
in c# have linq can in breeze:
var sum = c.cast<int>().orderbydescending(i => i).take(3).sum(); and that's all, converts array one-dimensional enumerable, orders biggest smalles, takes first 3 elements , sums them.
Comments
Post a Comment