Home > Enterprise >  When computing the sum of the series [1-X^2/2! X^4/4!- ...] why is the output showing "-1.00&qu
When computing the sum of the series [1-X^2/2! X^4/4!- ...] why is the output showing "-1.00&qu

Time:10-23

#include <stdio.h>
#include<math.h>
int main() {

    int n;
    int x;
    printf("enter base");
    scanf("%d",&x);
    printf("number of tearms");
    scanf("%d",&n);

    float sum=1.00;
    int fact=1;
    int s=1;
    int p=1;
    for(int i=1;i<=n;i  ) {
        s =s*(-1);
        int z=2*i;
        p =pow(x,z);
        fact =fact*2*i*(2*i-1);
        int k=s*p/fact;
        sum =sum k;
    }
    printf("the sum is : %0.2f",sum);
}

The logic and everything is correct, but I don't know why the output is getting an error. after getting some feedbacks i rectified some errors. now the out put is showing -1.00 for every input.

CodePudding user response:

If you specify 17 or more terms your factorial (fact) eventually overflows to 0:

#include <stdio.h>

int main() {;
    int fact=1;
    for(int i=1;i<=17;i  ) {
        fact =fact*2*i*(2*i-1);
        printf("%d: %d\n",i,fact);
    }
    return 0;
}

Typical output on platform using 32-bit integers:

1: 2
2: 24
3: 720
4: 40320
5: 3628800
6: 479001600
7: 1278945280
8: 2004189184
9: -898433024
10: -2102132736
11: -522715136
12: -775946240
13: -1853882368
14: -1375731712
15: 1409286144
16: -2147483648
17: 0

Change all your integer variables to double. Don't bother with float unless you have a specific reason (most likely space). It's a bit of an artefact and you should default to double in modern code.

Here's such a version. 20 terms for x=10.0 is 0.84 which is a fair approximation for COS(10.0) [what you wanted I assume!!]

I've output the terms to show convergence.

#include <stdio.h>
#include <math.h>

int main() {

    int n;
    int x;
    printf("enter base");
    scanf("%d",&x);
    printf("number of terms");
    scanf("%d",&n);

    double sum=1.00;
    double fact=1.0;
    double s=1.0;
    double p=1.0;
    for(int i=1;i<=n;i  ) {
        s=s*(-1.0);
        p=pow(x,2*i);
        fact =fact*2.0*i*(2.0*i-1.0);
        double term=s*p/fact;
        printf("%d: %f\n",i,term);
        sum =sum term;
    }
    printf("the sum is : %0.2f",sum);
    return 0;
}

CodePudding user response:

@ARPIT welcome to stackoverflow! This is too long for a comment so I'm putting it as an answer.

I assume your pow function is from the standard <cmath> header? (the #include isn't listed in your snippet). Also - it would be helpful to list your full build command line and example user inputs for which you see the error.

The problem is most probably in pow(x,2i), but in C 20 it should have manifested as a compile error, not runtime one. In clang it is:

error: implicit conversion from '_Complex int' to 'double' is not permitted in C  
        p =pow(x,2i);

and in gcc:

error: unable to find numeric literal operator 'operator""i'
   21 |         p =pow(x,2i);

If you'd provide more details we might be able to provide better help.

  •  Tags:  
  • c
  • Related