I have a function that takes an array a[] and its length n. I must calculate the sum of the numbers inside the array. I wrote this recursive function:
int somma(int a[], int n)
{
if (n == 0) { return 0; }
else {return a[n] + somma(a, n-1);}
}
And I call it in my main() in this way:
int main() {
int array[5], ris;
printf("Type the numbers of the array: \n");
for(int i=0; i<4; i++)
{
scanf("%d", &array[i]);
}
printf("\nThe sum is: %d.", somma(array,4));
getch();
return 0;
}
If the array contains array = [2; 4; 7; 5] the printf must show 18 (2+4+7+5). By the way the function returns me 88, can you help me?
I am using wxDevC++.
a[0]is also an element of the array, whichsommaskips.