# Recursion double function

• Comp Sci

## Homework Statement:

Calculate the Math Formula

## Relevant Equations:

-
C:
double foo(int arr[], double *ave, int index){
double *s;
*s=*ave;
// calculation//
return(foo (arr,ave,index));
// other calculation//
}
I want to keep the ave value during the recursion, because after ave is calculated, I will do another calculation is recursively in this function. But I write like this, I am getting segmentation fault.

Related Engineering and Comp Sci Homework Help News on Phys.org
anorlunda
Staff Emeritus
Hint: How many levels deep will the recursive calls go in your code?

Hint: How many levels deep will the recursive calls go in your code?
I did not understand what you say. the function will call itself about 560 times

Mark44
Mentor
Hint: How many levels deep will the recursive calls go in your code?
I did not understand what you say. the function will call itself about 560 times
Based on what you posted, the function will run until there is no more stack memory for it.
anonym said:
C:
double foo(int arr[], double *ave, int index){
double *s;
*s=*ave;
// calculation//
return(foo (arr,ave,index));
// other calculation//
}
I want to keep the ave value during the recursion, because after ave is calculated, I will do another calculation is recursively in this function. But I write like this, I am getting segmentation fault.
Right, and @anorlunda's hint might have something to do with why you're getting this fault.
Are there any conditions in which the function returns without calling itself? You don't show any in this code snippet. In particular, how does the part you have commented as "other calculation" actually get executed?

wle
Homework Statement:: Calculate the Math Formula
Relevant Equations:: -

C:
double foo(int arr[], double *ave, int index){
double *s;
*s=*ave;
// calculation//
return(foo (arr,ave,index));
// other calculation//
}
I want to keep the ave value during the recursion, because after ave is calculated, I will do another calculation is recursively in this function. But I write like this, I am getting segmentation fault.
You're assigning to the address of an uninitialised pointer:
C:
  double *s;
*s=*ave;
Why is s a pointer here?