I am trying to write a function that returns the outcome of multiplying all the negative numbers in an array. If the value is too large to be handled, I want it to return the int.MinValue.
I have coded this the following way, but it does not work correctly and I am not sure why.
public int MultiNeg(int[] array)
{
List<int> negativenumbers = new List<int>();
foreach (int numbers in array)
{
if (numbers < 0)
negativenumbers.Add(numbers);
}
int multiply = 1;
foreach (int i in negativenumbers)
{
multiply = multiply * i;
if (multiply > int.MaxValue)
{
return int.MinValue;
}
}
if (multiply > int.MaxValue)
{
return int.MinValue;
}
else
{
return multiply;
}