# segmentation fault (core dumped)

i wrote this code to print the sum of following series:

x+x^3/3!

since pow function was not getting recognized, i made a function power to achieve the same function.

#include<stdio.h>

int fact(int a);

int power(int x,int y);

int sum(int a,int b);

main(){

int x=0,n=0,s=0;

printf("**printing sum of x+x^3/3!

printf("enter x and n respectively\n");

scanf(

s=sum(x,n);

printf("the sum is :%d\n",s);

}

int fact(int a){

return a*fact(a-1);

}

int power(int x,int y){

int g=1;

while(y!=0){

g=g*x;

y--;

}

return g;

}

int sum(int x,int n){

int sum=0,i=0;

for(i=1;i<=n;i++){

sum+=

}

return sum;

}

i have two questions:

1. why isnt pow function displayed as undefined even after including math.h?

2. this code written above is getting compiled and even running upto

**printing sum of x+x^3/3!

enter x and n respectively

after entering two numbers it is diplaying "segmentation fault (core dumped)"

please help

## Question information

- Language:
- English Edit question

- Status:
- Solved

- Assignee:
- No assignee Edit question

- Solved by:
- nabanita

- Solved:
- 2013-03-04

- Last query:
- 2013-03-04

- Last reply:
- 2013-03-04

I suggest you post on a coding forum too

Savio (abhijeet) said : | #2 |

sum+=power(

all the problem start here please look into it

nabanita (nabanita-paul1) said : | #3 |

whats the problem here?

Manfred Hampl (m-hampl) said : | #4 |

I see several problems:

You are using int.

According to http://

If you call your function

int power(int x,int y){

int g=1;

while(y!=0){

g=g*x;

y--;

}

return g;

}

with a negative value for y, you run into an endless loop.

Your function

int fact(int a){

return a*fact(a-1);

}

does not have any termination condition and goes into an endless loop.

You are missing a line like

int fact(int a){

if(a <= 1) return 1;

return a*fact(a-1);

}

nabanita (nabanita-paul1) said : | #5 |

well i know the power function assumes that only positive values are entered . but this should work fine in the program as the values we are sending is actually never decreasing beyond 0. it starts from 2*i-1 where i has its first value as 1. so that shouldn't be a problem?

and the fact function , thanks for that, i really missed out on that. now i wrote

int fact(int a){

return a<=1?1:

}

and its working fine. thanks :)

Manfred Hampl (m-hampl) said : | #6 |

Two additional comments:

You have to use a floating point variable for sum and use a floating point casting on one of the factors before division in the expression

power(x,

If you use just integers, this expression will use interger division that cuts off all fractions less than 1, giving e.g the value 1 for the result for x=1 instead of 1,1752...

In my opinion the correct way would be modifications like

...

float sum(...)

...

sum += ((float) power(x,(2*i-1))) / fact(2*i-1);

(or use long or double instead of float).

+++++++

If you change the termination condition of your power function from

while(y!=0){

to

while(y>0){

it will terminate even if you start with a negative y value (although the result might be nonsense in such case).