Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-10-2004, 08:37 AM   #1 (permalink)
Broken In
 
Join Date: Aug 2004
Posts: 101
Default urgent c help!!


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 )



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.
geekgod is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 11-10-2004, 03:15 PM   #2 (permalink)
Wise Old Owl
 
alib_i's Avatar
 
Join Date: Jun 2004
Location: omnipresent
Posts: 1,191
Default

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;
__________________
What I've felt, What I've known; Never shined through in what I've shown
Never free, Never me; So I dub thee unforgiven
-Metallica
alib_i is offline  
Old 11-10-2004, 06:41 PM   #3 (permalink)
Broken In
 
Join Date: Aug 2004
Posts: 101
Default

whew, thnx friend. i applied the coorections and it is working purrfectly .
thnx to both u and anupam.
geekgod is offline  
Old 11-10-2004, 11:15 PM   #4 (permalink)
In The Zone
 
Join Date: May 2004
Location: dehradun
Posts: 389
Default

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
__________________
Music is like Sex, The more you get, the more you want.
demoninside is offline  
Old 12-10-2004, 03:03 PM   #5 (permalink)
Wise Old Owl
 
alib_i's Avatar
 
Join Date: Jun 2004
Location: omnipresent
Posts: 1,191
Default

thnx guys ...
i'll pass it on to anu ( as we call him ) too
__________________
What I've felt, What I've known; Never shined through in what I've shown
Never free, Never me; So I dub thee unforgiven
-Metallica
alib_i is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by clmlbx
- by Sujeet
- by gforz

Advertisement




All times are GMT +5.5. The time now is 03:35 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2