I am making a program to calculate operations over polynomials and I already finished other operation (+ - *) but stucked with the division, I am getting this error always as it shown in the code
static int[] DividePolnomials(int[] pol1, int[] pol2)
{
int tmp = 0;
int power_tmp = 0;
int[] result_tmp;
int[] result;
if (pol1.Length > pol2.Length)
{
result = new int [pol1.Length];
result_tmp = new int[pol1.Length];
for (int i = 0; pol2.Length<=pol1.Length; i++)
{
if (pol2[pol2.Length-i-1] == 0) // here is the problem it gives that the index is outside the bounds
{
continue;
}
else
{
tmp = pol1[pol1.Length - i - 1] / pol2[pol2.Length - i - 1];
power_tmp = (pol1.Length - i - 1) - (pol2.Length - i - 1);
result[power_tmp] = tmp;
result_tmp = Multiply(result, pol2);
pol1 = SubtractPolnomial(pol1, result_tmp);
}
}
}
else
{
result = new int [pol1.Length];
}
return result;
}
I haven't completed all other scenarios in the code yet due to this error, but I would like to get any help in the case the two polynomials are equals in length
Your i becomes greater than pol2.Length-1, so at this moment you have pol2[-1], pol2[-2] etc. This does not allowed in C#. You can check the next statement:
if ((pol2.Length-i-1 < 0) || (pol2[pol2.Length-i-1] == 0))
{
continue;
}
EDIT: If the first condition is true, the second one will not be evaluated https://msdn.microsoft.com/en-us/library/6373h346.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With