Divide and conquer sum algorithm

  array-algorithms, c++, sorting

i was looking for some D&C algorithms and came out founding this one

int sumArray(int anArray[], int size)
{
    //base case
    if (size == 0)
    {
        return 0;
    }
    else if (size == 1)
    {
        return anArray[0];
    }

    //divide and conquer
    int mid = size / 2;
    int rsize = size - mid;
    int lsum = sumArray(anArray, mid);
    int rsum = sumArray(anArray + mid, rsize);
    return lsum + rsum;
} 

but everytime i run the code without the base case it returns a segment fault error.
im trying to figure out why this base case is so important that even running with n>1 it stills returning that error, someone would lend a hand here?

Source: Windows Questions C++

LEAVE A COMMENT