-1

I have this code block

double[] tabHeight = { 16, 10, 15, 20 };
double[] tabMidSectionWA = { 6, 2, 5, 6 };

double[] tabCurveWA = {
           (tabHeight[0] - tabMidSectionWA[0]) / 2 ,
           (tabHeight[1] - tabMidSectionWA[1]) / 2,
           (tabHeight[2] - tabMidSectionWA[2]) / 2 ,
           (tabHeight[3] - tabMidSectionWA[3]) / 2 
};

Is there an easier way to add these values using the formula of (tabheight - tabMid) / 2 using a for loop or foreach?

Joelad
  • 492
  • 3
  • 12

5 Answers5

6

You could use Linq with Enumerable.Zip

var result = tabHeight.Zip(tabMidSectionWA,(x,y)=>(x-y)/2).ToArray();

Enumerable.Zip applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.

Output

enter image description here

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • 2
    This is a good alternative. Definitely something I'll look into as I've never come across this – Joelad Jan 08 '20 at 12:25
3

You can use Linq :

double[] tabHeight = { 16, 10, 15, 20 };
double[] tabMidSectionWA = { 6, 2, 5, 6 };

double[] tabCurveWA = Enumerable.Range(0, Math.Min(tabHeight.Length, tabMidSectionWA.Length))
                                .Select(i => (tabHeight[i] - tabMidSectionWA[i]) / 2)
                                .ToArray();

Console.WriteLine(string.Join(", ", tabCurveWA)); // outputs 5, 4, 5, 7

Enumerable.Range(0, Math.Min(tabHeight.Length, tabMidSectionWA.Length)) will create a sequence from 0 and of a size of the lowest value between tabHeight.Length and tabMidSectionWA.Length (to avoid an IndexOutOfRangeException)

Then, Select(i => (tabHeight[i] - tabMidSectionWA[i]) / 2) sets every number of this sequence (0 to 3 in this example) in i and apply (tabHeight[i] - tabMidSectionWA[i]) / 2, creating another sequence.

Finally, ToArray() transforms the sequence in an array

Cid
  • 14,968
  • 4
  • 30
  • 45
2

You mean something like this?

double[] tabHeight = { 16, 10, 15, 20 };
double[] tabMidSectionWA = { 6, 2, 5, 6 };
var minLength = Math.Min(tabHeight.Length, tabMidSectionWA.Length);
double[] tabCurveWA = new double[minLength];

for (var i = 0; i < minLength; i++)
{
    tabCurveWA[i] = (tabHeight[i] - tabMidSectionWA[i]) / 2;
}
Ignasi93
  • 533
  • 2
  • 5
  • 14
  • 1
    This is a nice simple alternative to be honest – Joelad Jan 08 '20 at 12:40
  • It's fastest way too. https://stackoverflow.com/questions/40129813/performance-of-enumerable-range-vs-for-loop However, with such a small collection, performance should not be an issue for you. – Ignasi93 Jan 08 '20 at 13:02
2

one of linq way :

double[] tabHeight = { 16, 10, 15, 20 };
double[] tabMidSectionWA = { 6, 2, 5, 6 };

double[] tabCurveWA = tabHeight.Select ((x, index) => (x - tabMidSectionWA[index])/2).ToArray();
Bhushan Muttha
  • 420
  • 2
  • 11
1

I'd make a class like this:

class Item // please find a better name
{
   public double Height {get;}
   public double MidSection {get;}
   public double Curve {get;}

   public Item( double height, double mid )
   {
       // TODO add Sanity Checks
       Height = height;
       MidSection = mid;
       Curve = height - mid / 2;
   }
}

And instead of two arrays have an array or list of that class.

As @Longoon states in comment, this could also be done with a struct. The idea, however, I wanted to produce is introducing a layer of abstraction. And there are other benefits coming along with this:

  • for example, this inherently enforces the same number of height and midSection Elements.
  • it's readable
  • it's easy to understand
  • if you fix naming, it shouldn't surprise clients

TL;DR: I think this is more "clean code" than the (very good) Linq-Answers.

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • 1
    Could also be a struct – Longoon12000 Jan 08 '20 at 12:21
  • @Longoon12000 while that's true, there are some perks with structs. I'd try with a class first and then see if making it a struct holds enough benefits. But it's definitely something to think about, yes. This answer is also more about the idea to create a layer of abstraction more than the exact details of implementation. – Fildor Jan 08 '20 at 12:23