urgent c help!!

Status
Not open for further replies.

geekgod

Journeyman
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.photobucket.com/albums/v461/technomodel/untitled.jpg

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.
 

alib_i

Cyborg Agent
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;
 
OP
G

geekgod

Journeyman
whew, thnx friend. i applied the coorections and it is working purrfectly :D :D :D :D .
thnx to both u and anupam.
 

demoninside

In the zone
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
 
Status
Not open for further replies.
Top Bottom