*EDIT* INTERPRET! lol....sorry!

Write a recursive function array_sum(int *array, int size, int index) that computes the sum of all the elements of the array starting with position "index".

Okay thats not too bad..but then he said this

for example, if an array A holds the values 5,4,3,2,1 then the call array_sum(A,5,2) will return the value 6=3+2+1 for it will sum up the values starting at position 2, that is 3 and 2 and 1.

This is what most confused me...

any tips on what the function will look like?

he said that the array needs to be filled with random numbers..i know that one at least...

void fillArray(int a[], int size)
{	
	for (int i=0; i<size; i++)
		a[i]=rand()%10;
	// heres my lil random number generator woot woot! 
}

Recursive usually means that the function calls itself (some say recursive when they mean iterative - which is quite different).

So for your problem, the following (incomplete) code shows recursion.

int recurseAdd(int*A, int N, int i){
   // ...
      return A[i] + recurseAdd(A, N, i+1);   // add current element to the next element
  //...

 }

Note that you're not allowed to try to access A where i >= N (i.e. outside the defined array), so when i >= N, you'll wan't to stop recursing. Do a google on recursion, there should be heaps of info available.

I know what recursion is sir...though thank you for your help!!! i just dont understand what output he wants...or what type of function he wants

I know what recursion is sir...though thank you for your help!!! i just dont understand what output he wants...or what type of function he wants

*EDIT* INTERPRET! lol....sorry!

Write a recursive function array_sum(int *array, int size, int index) that computes the sum of all the elements of the array starting with position "index".

Okay thats not too bad..but then he said this

for example, if an array A holds the values 5,4,3,2,1 then the call array_sum(A,5,2) will return the value 6=3+2+1 for it will sum up the values starting at position 2, that is 3 and 2 and 1.

This is what most confused me...

any tips on what the function will look like?

he said that the array needs to be filled with random numbers..i know that one at least...

array_sum(A, 5, 2) needs to add the elements of A from index 2 through index 5 - 1, or 4. So you're adding A[2] through A[4]. The 4 in A[4] is the array size minus 1. The 2 is the index you are starting the cumulative sum from. So you are adding this:

A[2] + A[3] + A[4] = 3 + 2 + 1 = 6

Putting parentheses around it, you are doing this:

A[2] + (A[3] + (A[4]))

You're actually going to call this function 4 times:

array_sum (A, 5, 2)
array_sum (A, 5, 3)
array_sum (A, 5, 4)
array_sum (A, 5, 5)

array_sum (A, 5, 5) needs to return 0 since you're going past the array bounds here. It'll be your "base case", where index >= size.

So use dougy's example and add a base case situation to it. The "base case" needs to occur BEFORE the return line he mentions because, as he says, you're not allowed to access A if i >= size of the array.

int recurseAdd(int*A, int N, int i){
   // ...
      return A[i] + recurseAdd(A, N, i+1);   // add current element to the next element
  //...

 }

Note that you're not allowed to try to access A where i >= N (i.e. outside the defined array), so when i >= N, you'll wan't to stop recursing.

This article has been dead for over six months. Start a new discussion instead.