May 20, 2013 at 11:05am UTC
hi guyz!
i've been studying "algorithm" by myself by book.
and there's this part -
vulnerable point of "define" syntax on recursive programming.
plz see below code.
int max_arr2(int arr[], int arr_len)
{
int (arr_len == 1)
return arr[0];
else
return max(arr[arr_len-1], max_arr2(arr, arr_len-1);
}
.
.
this book says,
"
function 'max_arr2' is just recursive function, without any problem. but if max() is defined like this,
#define max(x,y) ((x)>(y) ? (x):(y))
and if array is just declined, it will take very long time.
"
yeah, i typed this code and run, and actually it really takes long time.
but i still can't find out why it is.
anyone who can explain me newbie? :(
help me~
May 20, 2013 at 11:52am UTC
Look at the y term of max. For each recurrsion of max_arr2, the y term of max can be evaluated twice. Once to test > and again if > is true. Each time y is evaluated, it must recusrse down the remaining length of the array.