segmentation fault (core dumped)

Asked by nabanita

i wrote this code to print the sum of following series:
x+x^3/3!+x^5/5!+....upto n terms.
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!+x^5/5!+....n terms**\n");
 printf("enter x and n respectively\n");
 scanf("%d%d",&x,&n);
 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+=power(x,(2*i-1))/fact(2*i-1);
 }
 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!+x^5/5!+....n terms**
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
For:
Ubuntu gcc-defaults Edit question
Assignee:
No assignee Edit question
Solved by:
nabanita
Solved:
Last query:
Last reply:
Revision history for this message
actionparsnip (andrew-woodhead666) said :
#1

I suggest you post on a coding forum too

Revision history for this message
Savio (abhijeet) said :
#2

 sum+=power(x,(2*i-1))/fact(2*i-1);
all the problem start here please look into it

Revision history for this message
nabanita (nabanita-paul1) said :
#3

whats the problem here?

Revision history for this message
Manfred Hampl (m-hampl) said :
#4

I see several problems:

You are using int.
According to http://www.codecogs.com/reference/computing/c/math.h/pow.php you have to use floating point values for the base.

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);
}

Revision history for this message
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:(a*fact(a-1);
}

and its working fine. thanks :)

Revision history for this message
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,(2*i-1))/fact(2*i-1);
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).