1

I've seen a lot of topics about finding position of specified element in array, but I can't apply their solutions to my problem or find appropriate.

I have a jagged array:

double[][][] _distance = new double[_mapSize][_mapSize][1]; 
//incorrect, but for clarifying array's structure 

And I need to get indexes of minimum element.

If

_distance[2][5][0]

is minimum, I need to get "2" and "5"

Thanks in advance!

1 Answer 1

2

Try the following

Tuple<int, int, int> minimumIndex = null;
double minimumValue = Double.Max;

for (var i = 0; i < _mapSize; i++) {
  for (var j = 0; j < _mapSize; j++) {
    for (var k = 0; k < _lastDimension; k++) {
      var current = _distance[i][j][k];
      if (current <= minimumValue) {
        minimumValue = current;
        minimumIndex = Tuple.Create(i, j, k);
      }
    }
  }
}

Console.WriteLine("{0} {1} {2}", minimumIndex.Item1, minimumIndex.Item2, minimumIndex.Item3);
Sign up to request clarification or add additional context in comments.

2 Comments

+1. Not efficient at all, but if that's what he's looking for, there's no better way of accomplishing this.
I thought that there will be some better solution, but this one is acceptable. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.