Complete the function RecursiveSumArray below that recursive

Complete the function RecursiveSumArray below that recursively adds the numbers of an array.

The iterative version of the program appears as:

int SumArray(int A[], int number)
{   
   int i, sum=0;
   for(i=0; i < number; i++) {   
   sum = sum + A[i];   
   }   
   return sum;
}

int RecursiveSumArray(int A[], int number)
{

Code your code here for C language.

Please explain the definition of your code.

Thanks a lot

}

Solution

int RecursiveSumArray( int A[], int number )
{ // must be recursive

int sum1 = 0;
//base case:
if (number < 0)
{
return sum1;
}
else
{
sum1 = sum1 + A[number];
}
//make problem smaller
RecursiveSumArray(A,number-1); //Calling function within function=Recursion
}

Complete the function RecursiveSumArray below that recursively adds the numbers of an array. The iterative version of the program appears as: int SumArray(int A

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site