Answer the question
In order to leave comments, you need to log in
What is wrong with this recursion?
there is an array. find recursively the products of its factors. by bisecting the array. but recursive calls must stop if the piece of the array we are working with consists of one or two elements.
int recursion(int* X, int N, int i, FILE* res)
{
if (N - i == 2)
return X[i] * X[i+1];
if (N - i == 1)
return X[i];
int N1 = N/2;
int i2 = N - N1 + 1;
return (recursion(X, N1, i, res)*recursion(X, N, i2, res));
}
Answer the question
In order to leave comments, you need to log in
Correct function:
int mul(int * arr, int N, int shift = 0)
{
switch (N)
{
case 1:
return arr[shift];
break;
case 2:
return (arr[shift] * arr[shift + 1]);
break;
default:
int M = N / 2;
return (mul(arr, M, shift) * mul(arr, N - M, shift + M));
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question