A
A
Alexey Smirnov2017-01-30 16:33:40
.NET
Alexey Smirnov, 2017-01-30 16:33:40

How to find the minimum element in the first 5 elements of a one-dimensional array?

Hello.
There is some one-dimensional array with type data floatand consisting of 20 elements.

float[] firstArray = { 4, 2, 0, 1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

How to find the minimum element (its value or array element number) in the first 5 elements of this one-dimensional array?
PS I guess it can be done like this:
float[] firstArray = { 4, 2, 0, 1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

        float[] secondArray = new float[5];

        for (int i = 0; i < 5; i++)
            secondArray[i] = firstArray[i];

        float min = secondArray.Min();

But it may be possible to do this without creating a second array. To make the program code more compact.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zhulikof, 2017-01-30
@ERAFY

firstArray.Take(5).Min()
if you want LINQ
in the general case like this

float[] firstArray = { 4, 2, 0, 1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

int index = 0

for (int i = 0; i < 5; i++)
{
  if (firstArray[i] < firstArray[index])
  {
    index = i
  }
}
min = firstArray[index]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question