9 Replies - 7879 Views - Last Post: 20 October 2010 - 07:45 PM Rate Topic: -----

#1 micaxxa   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Help with approximating the value of e using factorials

Posted 17 October 2010 - 02:29 PM

I have to make a program that finds the value of e such that the formula used is 1+1/1!+1/2!+1/2!+1/3!+1/n!..... these terms are added till the term does not become less than 0.001.

p.s this is not my homework, I am just practicising some programming for my test, would be glad to get some help!!



#include <stdio.h>
int main(void)
{double term,den,sum,n,x;

 for(sum=0.0,n=0.0;term>0.001;n++)
 {
  if(n==0.0||n==1.0)
   {x=1.0;}
   else
   {
    x*= n;
     }

   term=1/x;
   sum+=term;

   }

   printf("%lf",sum);
   return 0;
   }



Is This A Good Question/Topic? 0
  • +

Replies To: Help with approximating the value of e using factorials

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Help with approximating the value of e using factorials

Posted 17 October 2010 - 02:50 PM

This:
   term=1/x;

needs to be this:

   term=1/factorial(x);

Now you need to write factorial:
int factorial(int Num)
{
 // compute factorial
}

and get rid of your if statement.

This post has been edited by CTphpnwb: 17 October 2010 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 micaxxa   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: Help with approximating the value of e using factorials

Posted 17 October 2010 - 03:14 PM

I havent studied functions yet !! , but thanks anyways. Can you tell me why this will not work properly ?
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Help with approximating the value of e using factorials

Posted 17 October 2010 - 04:28 PM

Hi, term is not initialised. The value of term is probably zero so the for loop will not be executed.
Was This Post Helpful? 0
  • +
  • -

#5 micaxxa   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: Help with approximating the value of e using factorials

Posted 17 October 2010 - 08:44 PM

View Post#define, on 17 October 2010 - 03:28 PM, said:

Hi, term is not initialised. The value of term is probably zero so the for loop will not be executed.



I initialised the term it didnt help :S
Was This Post Helpful? 0
  • +
  • -

#6 lfaivor21   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 29
  • Joined: 17-October 10

Re: Help with approximating the value of e using factorials

Posted 17 October 2010 - 09:49 PM

Watch out using a for loop in this way. Usually you'd want to use a while loop in this case.

for(sum=0.0,n=0.0;term>0.001;n++){
}



So the code might read instead... while term is > 0.001 keep looping. Instead of saying given n loop until term > 0.001 and then keep incrementing n. Teacher might be upset about it even if it works.

term = 1.0; //Set term to a number greater than 0.001 so the conditional holds true in the while loop for the first run.
sum = 0.0;
n = 0.0;
while(term>0.001)
{
     x = 1.0; //Covers the case when n = 0 as well! and returns x = 1 just like you'd expect.
     while(n!=0.0)
     {
          //multiply n*n-1*n-2 etc until n goes to 0. and set each step = to x
     }
     term = 1/x;
     sum +=x
     n++;
}



So instead of a separate function an easy and short nested loop would suffice just as well. Generally you'd want to use for loops to loop a number of times given by your iteration variable like i or n and not for testing for a condition and looping until it's met. Sure it can be used that way but I got docked for it in class so I'm assuming you might too.
Hope this helped!!

This post has been edited by lfaivor21: 17 October 2010 - 09:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 lfaivor21   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 29
  • Joined: 17-October 10

Re: Help with approximating the value of e using factorials

Posted 17 October 2010 - 10:05 PM

I made a slight booboo in the code. Here's an update.
term = 1.0; //Set term to a number greater than 0.001 so the conditional holds true in the while loop for the first run.
sum = 0.0;
n = 0.0;
iteration = 0; //Initialize an iteration variable to keep track of what n needs to be.
while(term>0.001)
{
     x = 1.0; //Covers the case when n = 0 as well! and returns x = 1 just like you'd expect.
     while(n!=0.0)
     {
          //multiply n*n-1*n-2 etc until n goes to 0. and set each step = to x
     }
     term = 1/x;
     sum +=x
     iteration++;
     n = iteration;
}


This post has been edited by lfaivor21: 17 October 2010 - 10:06 PM

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Help with approximating the value of e using factorials

Posted 18 October 2010 - 07:07 AM

You were closer in your first post.
Here it is using a function:
#include <stdio.h>
#include <iostream>
using namespace std;
int fact( int number);

int main()
{
	double term,sum;
	int n, x;
	term = 1;
	sum = 0;
	n = 0;
	while(term>0.001)
	{
		x = fact(n);
		term=1.0/x;
		sum+=term;
		n++;
	}
	
	cout << "e: \t" << sum << endl;
	return 0;
}

int fact( int number)
{
	int myfact = 1;
	for (int i = number; i > 1; i--) {
		myfact *= i;
	}
	return myfact;
}

Put aside the function call and think about how this compares to yours. Do you see how this is similar to yours, and where the logic is different?
Was This Post Helpful? 0
  • +
  • -

#9 lfaivor21   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 29
  • Joined: 17-October 10

Re: Help with approximating the value of e using factorials

Posted 18 October 2010 - 10:33 AM

If you were talking about my posts.. yeah I see it :) In my first post I was iterating n but should have initialized another variable for inside the nested loop. The second post worked with my initial logic with using n inside the nested loop while keeping track of iterations... which is where my first post was lacking. Oh and I set the sum equal to x instead of term. Thanks for catching my mistakes.

This post has been edited by lfaivor21: 18 October 2010 - 10:37 AM

Was This Post Helpful? 0
  • +
  • -

#10 micaxxa   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: Help with approximating the value of e using factorials

Posted 20 October 2010 - 07:45 PM

Thankyou guys, I see for loop is usually good for making stars and all. I have just become too addicted to for loops for some odd reason that even though I know about while, I try to use for loop all the time =___=
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1