1. ## Recursion help

I need to write a program that recursively calculates the sum of UP TO 20 array elements provided by the user. Currently the program is resulting in an error when I attempt to run it. I'm also not sure if my recursive function does what I need it to in it's current state.

Code:
``` #include <stdio.h>
#include <string.h>

int sum(int i, int element[i]); /*Prototype for recursive addition */

int main(void)
{
int max_elements, element[(max_elements-1)], i, result;
i=0;
printf("Determine the number of elements to be entered from 1 to 20: ");
scanf("%d", &max_elements);

for(i=0; i<max_elements; i++)
{ printf("Enter an integer: ");
scanf("%d", element[i]);
printf("\n");
}
result=sum(i, &element[i]);
printf("The sum of the elements is %d", result);
return 0;
}

int sum(int i, int element[i])

{ int max_elements;
if (i==max_elements)
return 0;
else
return (sum(i+1, element[i+1]+&element[i]));
}```
Where am I going wrong?

2. > int max_elements, element[(max_elements-1)]
1. C doesn't have variable length arrays
2. You haven't even initialised max_elements, so assuming it even compiles by some compiler extension, the size could be anything
Since you state "max 20", just go with int element[20];

> scanf("%d", element[i]);
You forgot the &
scanf("%d", &element[i]);

Think about the recursive function in these terms
- if there is only one element in the array, the sum is that element
- else the sum is the last element, plus the result of the recursive call to sum n-1 elements

Code:
```int sum ( int n, int *arr ) {
if ( n == 0 ) {
return arr[n];
} else {
return arr[n] + sum(n-1,arr);
}
}```

Code:
``` #include <stdio.h>
#include <string.h>

int sum(int i, int element[i]); /*Prototype for recursive addition */

int main(void)
{
int max_elements, element[20], i, result;
i=0;
printf("Determine the number of elements to be entered from 1 to 20: ");
scanf("%d", &max_elements);

for(i=0; i<max_elements; i++)
{ printf("Enter an integer: ");
scanf("%d", &element[i]);
}
result=sum(i, &element[i]);
printf("The sum of the elements is %d", result);
return 0;
}

int sum(int i, int element[i])

{
if (i==0)
{
return element[i];
}
else
{
return element[i]+ sum(i-1, &element[i-1]);
}
}```
The code now runs, but the sum is being displayed as '32' regardless. I realize that you suggested trying the else statement of:
return arr[i]+sum(i-1, arr);
That resulted in a huge negative sum value.

4. Code:
```#include <stdio.h>
int sum ( int n, int *arr ) {
if ( n == 0 ) {
return arr[n];
} else {
return arr[n] + sum(n-1,arr);
}
}
int main ( ) {
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
printf("%d\n", sum(9,arr) );
}```

5. There is nothing wrong with Salems example.
It's the way you call the function.
Code:
`  result=sum(i, &element[i]);`
What is the value of i here ?.
Where points &element[i] ? is that the array ?
Another thing is that not many ( if any ) compilers accept
Code:
`  int sum(int i, int element[i]); /*Prototype for recursive addition */`
Kurt
EDIT: I'm a little slow.

6. If the sum is calculated to be greater than 20, the program has no problem displaying it. If the sum is less than 20, however, the program feels the need to add 20 to it? I implemented an if/ else statement to try and correct it, but it was unsuccessful.
Code:
```#include <stdio.h>
#include <string.h>

int sum(int i, int element[i]); /*Prototype for recursive addition */

int main(void)
{
int max_elements, element[20], i, result, total;
i=0;
printf("Determine the number of elements to be entered from 1 to 20: ");
scanf("%d", &max_elements);

for(i=0; i<max_elements; i++)
{ printf("Enter an integer: ");
scanf("%d", &element[i]);
}
result=sum(max_elements, &element[i]);
if (sum(max_elements, element)<20)
{
total=((sum(max_elements, element))-20);
printf("The sum is %d", total);
}
else
{
printf("The sum is %d", sum(max_elements,element));
}
return 0;
}

int sum(int i, int element[i])

{
if (i==0)
{
return element[i];
}
else
{
return element[i]+ sum(i-1, element);
}
}```

7. Code:
`result=sum(max_elements, &element[i]);`
This should just be:
Code:
`result = sum( max_elements, element );`
What exactly are you trying to do here?
Code:
` if (sum(max_elements, element)<20)`
Quzah.

8. Originally Posted by quzah
What exactly are you trying to do here?
Code:
` if (sum(max_elements, element)<20)`
Quzah.
That was an attempt to fix the sum problem I am getting. A calculated sum that was less than 20 was having 20 added to it. A sum greater than 20 was outputted normally. So I tried to write a line that would fix any sum smaller than 20.

9. But it is supposed to retun the sum of the entered values, so why on earth ... anyway, it's better to try and find out what's wrong, rather than try to cobble scraps together to get the desired output.

Quzah.

10. Originally Posted by quzah
But it is supposed to retun the sum of the entered values, so why on earth ... anyway, it's better to try and find out what's wrong, rather than try to cobble scraps together to get the desired output.

Quzah.
I realize that, but for whatever reason that isn't working, and neither is this attempt to patch it up.

11. I don't know what you are talking about, your code from above works fine for me. Kill the if-else, and it is working, displayin correct results for values under 20 too.

12. element[max_elements] is past the end of the array.
You have to call sum this way
Code:
`total=sum(max_elements-1, element);`
Kurt