1. Hey Guest Did you know you can win an Honor 10 phone worth ₹33,000 and an additional ₹70,000 in paytm vouchers, just by replying to some threads and taking part in the discussions happening in the Honor Hub?

    What are you waiting for? Start commenting and start winning! Remember to read the instructions posted here.

    Dismiss Notice

urgent c help!!

Discussion in 'QnA (read only)' started by geekgod, Oct 11, 2004.

Thread Status:
Not open for further replies.
  1. geekgod

    geekgod New Member

    Joined:
    Aug 18, 2004
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    In a class assignment, i have to write a cprogram to calculate the sum of the following series:
    1/1!+3^2/2!+5^3/3!+......
    for that i wrote the following program.
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <ctype.h>
    main()
    {
      float sum,term;
      FILE *pt;
      pt=fopen("series", "w+");
      int count, tag, denom=1, num;
      char answer='Y';
      while (toupper(answer)=='Y')
      {
      printf("Enter the number of terms of the series : ");
      fprintf(pt, "Enter the number of terms of the series : ");
      scanf("%d", &count);
      tag=count;
      printf("The sum of the given series is\n");
      fprintf(pt, "The sum of the given series is\n");
      for (count=1;count<tag;count++)
    	{
    	  sum=0;
    	  denom=denom*count;
    	  num=2*count-1;
    	  term=pow(num,count)/denom;
    	  sum=sum+term;
    	  if(count==1 || count%7==0)
    	  {
    	  printf("\n(%d^%d)/%d! + ", num, count, count);
    	  fprintf(pt, "\n(%d^%d)/%d! + ", num, count, count);
    	  }
    	  else {
    	  printf("(%d^%d)/%d! + ", num, count, count);
    	  fprintf(pt, "(%d^%d)/%d! + ", num, count, count);
    			}
    	}
    	count=tag;
    	denom=denom*count;
    	num=2*count-1;
    	term=pow(num,count)/denom;
    	printf("(%d^%d)/%d! = ", num, count, count);
    	fprintf(pt, "(%d^%d)/%d! = ", num, count, count);
    	sum=sum+term;
    	printf("%.2f", sum);
    	fprintf(pt, "%.2f", sum);
    	printf("\nDo you want to repeat the calculation with a different value of n?(Y/N) ");
    	fprintf(pt, "\nDo you want to repeat the calculation with a different value of n?(Y/N) ");
    	scanf("%s", &answer);
    	}
    }
    
    i added a few extra lines so that my series is also diplayed, but the problem is whenever i give the number of terms a s 18 or more, my compiler shows the following error,(at least similar to it :) )

    [​IMG]

    but i need the sum of 50 terms :( :(
    help plz guys, i'm in trouble, gotta submit it tomorrow. i use Turbo C++ 4.5 from Borland.
     
  2. alib_i

    alib_i New Member

    Joined:
    Jun 24, 2004
    Messages:
    1,191
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    omnipresent
    thnx to anupam ( my friend )

    1. your code is incorrect

    Put sum=0 outside for loop
    You are resetting sum in each count loop


    Put both sum=0 and denom=1 just after this line ..
    while (toupper(answer)=='Y')
    Or else if we try to calculate the series another time, it will give wrong answer.


    2. Floating Point error

    I tried lots of tricks on the code.. and found that actually denom = factorial (count) is going out of range beyond count=7
    Factorial(7) = 5040
    Factorial(8) = 40,320 ( more than 32,768 )
    So the program incorrectly shows Negative values of denom beyond 7. You start calculating incorrect sum beyond 7
    At around count=18, the value of denom is shown 0. So you get the error.

    Solution

    Put
    Code:
    double float sum, term, denom;
    int count, tag, num;
    
     
  3. OP
    OP
    geekgod

    geekgod New Member

    Joined:
    Aug 18, 2004
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    whew, thnx friend. i applied the coorections and it is working purrfectly :D :D :D :D .
    thnx to both u and anupam.
     
  4. demoninside

    demoninside New Member

    Joined:
    May 20, 2004
    Messages:
    389
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    dehradun
    Good work guys but this program can me get done in very easy way
    but not sure about file handling (i m not good in it)
    so better not to say that,
    any way good work alini & anupam
     
  5. alib_i

    alib_i New Member

    Joined:
    Jun 24, 2004
    Messages:
    1,191
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    omnipresent
    thnx guys ...
    i'll pass it on to anu ( as we call him ) too
     
Thread Status:
Not open for further replies.

Share This Page