Recursion double function

  • #1
anonim
40
2
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.
 

Answers and Replies

  • #3
anonim
40
2
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
 
  • #4
36,854
8,888
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?
 
  • #5
wle
336
158
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?
 

Suggested for: Recursion double function

Replies
4
Views
943
  • Last Post
Replies
11
Views
381
Replies
19
Views
2K
  • Last Post
Replies
10
Views
683
  • Last Post
Replies
12
Views
775
  • Last Post
Replies
11
Views
1K
  • Last Post
Replies
2
Views
763
  • Last Post
Replies
2
Views
730
  • Last Post
Replies
12
Views
579
Top