- #1
James889
- 192
- 1
Hi,
I am trying to write a simple recursive function for calculating money in the bank after a certain amount of years.
However i run into trouble since i need the new_base variable to be static(i need it to "remember" the base from the previous run) But trying to compile the program i get the error:
"Initializer element is not constant"
How would you solve this?
I am trying to write a simple recursive function for calculating money in the bank after a certain amount of years.
However i run into trouble since i need the new_base variable to be static(i need it to "remember" the base from the previous run) But trying to compile the program i get the error:
"Initializer element is not constant"
How would you solve this?
Code:
double recursive(double base, int runs) {
static double m_rate = 1 + (10/100); /* Ten percent interest*/
double new_base = (base*m_rate);
if (runs != 0)
return recursive(new_base,runs-1);
else
return new_base;
}
int main() {
printf("%.2f",recursive(100,10));
return 0;
}