Mihir Bose
Mihir Bose

Reputation: 11

Code::Blocks 13.12 error - program has stopped working

I am using codeblocks 13.12. I have made a program for the following question:-

Evaluate the function to 0.0001% accuracy. Sinx = x - (x^3/3!) + (x^5/5!) - .....

It gives error after I enter the value of x. It says "AccuracyOfSinx.exe has stopped working".

#include<stdio.h>
#include<conio.h>

#define ACCURACY 0.000001
int main()
{
int n, num, mulx, fac, rem;
float x,term, sum;

printf("Enter x = ");
scanf("%f", &x);
num=1;
sum=0;
mulx=1;
for(n=1, term=1; term>ACCURACY ; n++)
{
 fac=num;
 do
  {
  num=num-1;
  fac=fac*num;                                /*Finds the factorial*/
  mulx=mulx*x;                                /*Computes x raised to num*/
  }
 while(fac>0);
 term = 1/fac;
 num=num+2;
 rem=n%2;                                     
 if(rem==0)                                   /* -ve sign is given to even position*/
  sum = sum-(mulx/fac);
 if(rem!=0)                                   /* +ve sign is given to odd position*/
  sum = sum+(mulx/fac);
 mulx = x;
}
printf("Sin(x)=%f", sum);
}

Upvotes: 0

Views: 414

Answers (1)

Ayushi Jha
Ayushi Jha

Reputation: 4023

The main problem lies here:

 term = 1/fac;

It is a case of division by zero. I printed the value of fac before this statement, and it prints 0 before accuracy.exe stops working.

The main reason for fac going to 0 is

 num=num-1;
 fac=fac*num; 

To prevent this, add an if condition to check the value of num before using it for calculating fac.

Upvotes: 1

Related Questions