Hi, here is my simple linear recursive algorithm to sum elems of an array. Does anyone know how I can convert this to be optimized, to be tail recursive.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int r_linear_sumArray_v1(int* list, int size, int start)
{
if ( start == (size - 1) )
return list[start];
elsereturn r_linear_sumArray_v1(list, size, start + 1) + list[start];//when base case reached, that elem is added to the current index start just after the popped base case
}
int main()
{
int quickList[] = {100,20,3009,4};
int qSize = sizeof(quickList)/sizeof(int);
int sum = r_linear_sumArray_v1(&quickList[0], qSize, 0);
return 0;
}