whomustnotbenamed
whomustnotbenamed

Reputation: 51

how to get the absolute values in each element of array

I have some problem in converting the elements in the array to absolute values.

Console.WriteLine("\nQ = {0}, difference = |{1} - {2}| = {3} ",
                  a + 1, store[a], store2[a], Math.Abs(store3[a]));

the store3 is a array. I already inputted it some elements in my program. and I will get the right answer and the problem is after getting the absolute value I have to find the minimum value of the array but it returns the negative integer. and i want only to return the smallest or minimum of the elements in their absolute value. How will I do this. I hope you understand my question.

Upvotes: 3

Views: 5814

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

If you want "smallest or minimum of the elements in their absolute value", try direct Min with required lambda:

store3.Min(x => Math.Abs(x));

Implementation

Console.WriteLine("\nQ = {0}, difference = |{1} - {2}| = {3} ",
                  a + 1, 
                  store[a], 
                  store2[a], 
                  store3.Min(x => Math.Abs(x)));

Upvotes: 2

keyboardP
keyboardP

Reputation: 69372

You can use LINQ to get the Abs value

store3Abs = store3.Select(x => Math.Abs(x)).ToArray();

If you don't need the intermediate abs array, you can just get the minimum directly

var min = store3.Select(x => Math.Abs(x)).Min();

Upvotes: 0

Related Questions