1

I have to find the value of 1+x/1!+x^2/2!+.....

Here is my code:

void main()
{
  long int a, i = 0;
  double p, e, x, sum = 1, t = 1;

  printf("Enter desired number of decimal places\n");
  scanf("%d", &a);

  printf("Enter x\n");
  scanf("%d", &x);

  e = pow(10, -a);

  do
  {
    t = x * t / (double) (i + 1);
    sum = sum + t;
    p = fabs(t);
    i++;
    printf("Sum is %ld\n", sum);
  } while ((p > e) && (i < 10000));
}

My logic for the program is like this:

I'm letting the user input x and accuracy(a). My error is 10^(-a). i is my counter. sum is my sum of the series. t is denoting my terms.

I'm starting with t=1, and using the recursion t=(x*t)/(double)(i+1). [Note: since i is int and t is double, I'm doing a type conversion here.]

For example, if I choose x=5, then t=1 is initialized. Next I get t=5/1=5. Sum is initialized as 1. So sum now becomes sum=1+5=6. Then i becomes 1. So t becomes 5*5/2=12.5. So my sum now should be 6+12.5.... In this way it should go now in my do {} part.

Now, for the while part, p is the absolute value of t. The difference between the sum of (n+1) terms and n terms is this p. So when my p will be less than e, I should get my desired sum, right? In case the series is divergent, to avoid an infinite loop, I'm letting the do {} part run only as long as my i<10000.

The error I'm getting every time I run this program is I get the sum as 0. How is that even possible when I initialized sum as 1?

Please help me out with where I'm wrong. I'm new to programming.

alk
  • 69,737
  • 10
  • 105
  • 255
Diya
  • 133
  • 6
  • 2
    This `scanf ("%d",&x);` looks ugly (for `x` being a `double`). – alk Jan 16 '15 at 19:10
  • Also you might like to compile with symbols (`-g` for gcc) and run your code in a debugger (gdb for gcc) and step through it line by line inspecting the values of any relevant variables and you might get enlighted. – alk Jan 16 '15 at 19:12
  • Also^2: No need to cast here: `t=x*t/(double)(i+1);` – alk Jan 16 '15 at 19:13
  • For what does the `e` in your question's title stand? – alk Jan 16 '15 at 19:54
  • 1
    @alk Certainly "e" stands for [e (mathematical constant)](http://en.wikipedia.org/wiki/E_(mathematical_constant)) – chux - Reinstate Monica Jan 16 '15 at 20:58
  • What has happened to the maths education that people do not know what 'e' is - http://en.wikipedia.org/wiki/MDMA – Ed Heal Jan 17 '15 at 08:15

2 Answers2

4

Change this

scanf("%d", &x);

to become this:

scanf("%lf", &x);

to at least use the x you intend to and not provoke undefined behaviour.

For prinf()ing sumyou also shall not use d, but f for a double. There is no length modifier l necessary here as for printf() f stands for double. Weird, but that's the way it is.

printf("Sum is %f\n", sum);

Also you are suing the wrong conversion specifier to scan in a, which is along` so it shall be:

scanf("%ld", &a);

Also^2 it's (at least) int main(void).

From the C11 Standard:

5.1.2.2.1/1 Program startup

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }


Also^3 your code snippet misses to include the relevant header files.


As a final note: Compile with all warnings on and take them serious.

alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    You'll probably want to do the same to the `printf` statement at the end. – alarge Jan 16 '15 at 19:18
  • Yeah, I added the stdio, conio, math header files. I made the changes but I'm still getting the error. Now it's a "POW: Overflow. sum=6.00000" for x=5. Also, can you please explain why I should use int main () instead of void main ()? – Diya Jan 17 '15 at 01:46
  • "*why [...] use `int main ()` instead of `void main ()`?*" please seen my updated answer. @Diya – alk Jan 17 '15 at 08:00
1

In this answer I have used the decimal places both to format the terms and to test if the series has converged, by formatting the sum as a string - which is needed anyway to format it for output.

#include <stdio.h>
#include <string.h>

int main ()
{
    double sum=1, term=1;
    int n=1, x=1, dp=0;
    char current[50]="", previous[50]="";

    printf ("Enter number of decimal places: ");
    scanf ("%d", &dp);
    printf ("Enter x: ");
    scanf ("%d", &x);
    printf ("Series for x=%d to %d decimal places\n", x, dp);

    do {
        strcpy (previous, current);
        sprintf (current, "%-10.*f", dp, sum);
        printf ("Term = %-10.*f Sum = %s\n", dp, term, current);
        term = term * (double)x / (double)n;
        sum += term;
        n++;
    } while (strcmp(previous, current));
    return 0;
}

I should have tested the result of scanf(). Program output

Enter number of decimal places: 2
Enter x: 2
Series for x=2 to 2 decimal places
Term = 1.00       Sum = 1.00
Term = 2.00       Sum = 3.00
Term = 2.00       Sum = 5.00
Term = 1.33       Sum = 6.33
Term = 0.67       Sum = 7.00
Term = 0.27       Sum = 7.27
Term = 0.09       Sum = 7.36
Term = 0.03       Sum = 7.38
Term = 0.01       Sum = 7.39
Term = 0.00       Sum = 7.39
Weather Vane
  • 33,872
  • 7
  • 36
  • 56