Reputation: 1600
I have 2Arrays
of doubles on Which I need to perform arithmetic operations(+,-,/,*)
Array1 = d1 , d2, d3, d4, d5, d6, , , d9, d10
Array2 = d3, d4, , d6, , d8, d9, d10
Operation = "+";
Edit Above Empty elements are shown just for representation of the data they are all stored without the empty spaces
Array1 = d1, d2, d3, d4, d5, d6, d9, d10
Array2 = d3, d4, d6, d8, d9, d10
Req Output
ArrayOpt =
[0] = d3+d3
[1] = d4+d4;
[2] = d5+d4;
[3] = d6+d6;
[4] = d6+d8;
[5] = d9+d9;
[6] = d10+d10;
What I have tried
int i = 0, PreviousIndex = -1;
int SecondaryIndex = 0;
// check if there is a gap
if (SecondaryIndex < DATES2.Length && Convert.ToInt32(d) != Convert.ToInt32(DATES2[SecondaryIndex]))
{
// first value of index contain the index of second symbol date that matches with first symbol's first date.
// eg: first data => d1 d2 d3 d4 d5 d6
// 2nd data => d3 d4 d6
// in the above case, the index would be the index of d3.
index = DATES2.Select((v, j) => new { Index = j, Value = (int)v }).Where(p => p.Value == (int)d).Select(p => p.Index).ToList();
if (index.Count > 0)
SecondaryIndex = index[0];
else
SecondaryIndex = -1;
}
if(secondaryIndex != -1)
{
CalculateData(operation, DATES1[i],DATES2[secondaryIndex]);
PreviousIndex = secondaryIndex;
}
else
{
CalculateData(operation, DATES1[i],DATES2[PreviousIndex]);
}
i++;
secondaryIndex++;
but the output is this
d1, d2, d3, d4, d5, d6, d9, d10
+
d3, d4, d6, d8, d9, d10
Can anybody suggest what is the issue or any other better solution?
Upvotes: 0
Views: 629
Reputation: 1308
You can use Math.Net library to perform basic linear algebra operations and all kinda matrix operation stuff you may wanna use. As you can see in the link basic linear algebra operation examples shown (+,*, -, /).
Also, since you matrixes are single dimentional, you can sum Array1 and Array2 indexes as shown below with a single for loop.
var a1 = new int[5] {1,2,3,4,5};
var a2 = new int[7] {1,2,3,4,5,6,7};
var maxLength = a1.Length > a2.Length ? a1.Length : a2.Length;
var outputArray = new int[maxLength];
for(var i = 0; i < maxLength; i++)
{
if(a1.Length < i + 1)
{
outputArray[i] = a2[i];
continue;
}
if(a2.Length < i + 1)
{
outputArray[i] = a1[i];
continue;
}
outputArray[i] = a1[i] + a2[i];
}
Console.Write(outputArray.GetValue(6));
Upvotes: 1