Reputation: 908
I want to create a method that I can send two arrays (that will be containing ints). These arrays wont necessarily be equally long. for example first array could have an index of 15 while the second array has an index of 12. in that case I want to add array1 and array2 for the first 12 then just get the value of array1 for the last 3.
I thought something like this:
int[] ArrTotal(int[] array1, int[] array2)
{
int[] total = new int[15];
for (int i = 0; i < 15; i++)
{
if (array1[i] != null && array2[i] != null)
{
total[i] = array1[i] + array2[i];
}
else if(array1[i] != null)
{
total[i] = array1[i];
}
else if (array2[i] != null)
{
total[i] = array2[i];
}
else
{
total[i] = 0;
}
}
return total;
}
Problem is that I can't check and see if an int array is null. I read something about doing an: If(i < array1.Length)
but that does not seem to work either, it says it will always be true in my case. Am I on the right track at all or is there some major flaw I'm missing? :)
Upvotes: 0
Views: 1545
Reputation: 27085
How about:
int[] ArrTotal(int[] a, int[] b)
{
if (a == null || b == null)
{
return (int[])(a ?? b).Clone();
}
int length = Math.Max(a.Length, b.Length);
int[] result = new int[length];
for (int i = 0; i < length; i++)
{
int sum = 0;
if (a.Length > i) sum += a[i];
if (b.Length > i) sum += b[i];
result[i] = sum;
}
return result;
}
Upvotes: 4
Reputation: 109557
Using Linq you can do this (which will handle null arrays). You will need a using System.Linq
at the top of the source code file:
int[] ArrTotal(int[] array1, int[] array2)
{
if ((array1 == null) && (array2 == null))
return new int[0]; // Zero length array - put some other number here if you need!
else if (array1 == null)
return (int[])array2.Clone(); // Result will just be a copy of the non-null array.
else if (array2 == null)
return (int[]) array1.Clone(); // Result will just be a copy of the non-null array.
else
{
int skip = Math.Min(array1.Length, array2.Length);
return Enumerable
.Zip(array1, array2, (i1, i2) => i1 + i2)
.Concat(array1.Skip(skip))
.Concat(array2.Skip(skip))
.ToArray();
}
}
Upvotes: 0
Reputation: 32541
You may use Linq for it:
int[] ArrTotal(int[] array1, int[] array2)
{
return Enumerable.Repeat(0, Math.Max(array1.Length,array2.Length))
.Select((a, index) => a +
((array1.Length > index) ? array1[index] : 0)+
((array2.Length > index) ? array2[index] : 0))
.ToArray();
}
Upvotes: 0
Reputation: 965
Try to check lengths of both arrays before:
int length = (array1.Length < array2.Length ? array1.Length : array2.Length);
Then iterate and assign only to array indeces from 0 to length of the shorter array - 1:
for (int i = 0; i < 15; i++)
if (i < length)
newArray[i] = array1[i] + array2[i];
else
newArray[i] = 0;
Upvotes: 1
Reputation: 325
int[] a1 = new int[] { 1, 2, 3, 2, 3, 1 };
int[] a2 = new int[] { 1, 2, 3, 2, 3, 1, 3, 2, 3 };
List<int> r = new List<int>();
bool a1_longer = (a1.Length > a2.Length);
int length_diff = Math.Abs(a1.Length - a2.Length);
int length = (a1_longer ? a2.Length : a1.Length);
for (int i = 0; i < length; i++) r.Add(a1[i] + a2[i]);
for (int i = 0; i < length_diff; i++) {
r.Add(a1_longer ? a1[length + i] : a2[length+i]);
}
r.ToArray();
Upvotes: 0