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.
Where am I going wrong?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])); }



LinkBack URL
About LinkBacks


