First off, I know my title can be formulated better, but my math classes are so far gone I can’t remember the correct words anymore..
I need to do something like this (pseudo c#)
int[] digits1 = new int[10]{0,1,2,3,4,5,6,7,8,9};
int[] digits2 = new int[10]{0,1,2,3,4,5,6,7,8,9};
int result = digits1*digits2
This would be the sum of the product of element[i] of each array.
This obviously doesn’t work.
Any suggestions towards either a better title or the solution?
EDIT
clarification: I know I could loop them both and do the math.
Basically I would think there is a better way to do this and I’m looking for it purely out of personal curiousity.
With LINQ:
Zipwill produce a streaming sequence containing the products of corresponding elements from both arrays, which is then summed into an integer withSum.Note that this will not fail like it should when the arrays of unequal length, so you probably need to validate the input:
EDIT:
As Jeff M points out,
Enumerable.Zipwas only added to the framework in .NET 4.0. In .NET 3.5, you can do this (the idea is only efficient for collections that expose fast indexers):