3

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.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188

2 Answers2

2

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)));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

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();
keyboardP
  • 68,824
  • 13
  • 156
  • 205