Hey guys I just need help with this somewhat troubling question!
Write a recursive function that finds and returns the sum of elements of an int array. Also write a program to test your function.
Thanks in advance!
Three answers:
James Bond
2012-03-26 09:40:28 UTC
int sum(int a[], int n)
{
if(n==1) return a[0];
else
return( a[n-1] + sum(a, n-1) );
}
main()
{
int x[]={10,20,90,30,30,40,80,60}, S;
S=sum(x,8);
printf("%d\n", S);
}
?
2016-10-22 11:38:46 UTC
we can assume that y is constrained to nonnegative values the bottom case will be the position y = 0, in which case our function will go back a million. otherwise, it really is going to go back x cases a recursive call to the function with y - a million. (For nonnegative integer values of y, the function returns x^y as predicted; if y features a fractional element, the function returns x^floor(y).) // pre: x is a valid double; y is interior the set of organic numbers // post: returns fee x^y double recursivePow (double x, double y) { if(y <= 0) { go back a million; } go back x * recursivePow(x, y-a million); }
2012-03-26 09:23:05 UTC
a[10]={1,2,3,4,5,6,7,8,9,10}; //initialiae array
int sumArray(int i)
{
if(i==0) return a[0];
else
return a[i]+sumArray(i-1);
}
main()
{
sumArray(9);
}
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.