(2nd Problem) From Chapter 15 on page 1080, do problem #5. Recursive function that computes the same of values in an array. To test this function in the main, create the following array:
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
#15. Write a recursive function that finds and returns the sum of the elements of an int array. Also, write a program to test your function.
The java
class Arraysum{
static int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Return sum of elements in arr[0..n-1]
// using recursion.
static int Sum(int arr[], int n)
{
if (N <= 0)
return 0;
return (Sum(arr, n - 1) + arr[n - 1]);
}
// Driver method
public static void main(String[] args)
{
System.out.println(Sum(a, a.length));
}
}
program for finding sum of array recursively is given below:
Get Answers For Free
Most questions answered within 1 hours.