Quantcast

Jump to content


Uhmm..... Finding Euler's number with C++?


  • Please log in to reply
2 replies to this topic

#1 Guest_jcrgirl_*

Guest_jcrgirl_*

Posted 24 March 2011 - 07:42 PM

Write a program that calculates Euler’s number e. To do this, first write a function that takes a parameter n, and returns the result (1+1/n)n. The limit of this function approaches e as n approaches infinity. In your main program, write a loop that calls this function with increasing values of n. On each iteration, multiply n by 2 (if you just add 1 to n each time, the algorithm won't work) and stop when the your new approximation and your previous approximation differ by less than 1e-8. Now you have a pretty good estimate. So in main(), print your best approximation and the number n that generated it.



OKAY
I thought I knew what was going on in class UNTIL I SAW THIS PROBLEM.
HELP r__r

He used a bunch of big words... what is a parameter and what is this approximation crap? I'm so confused... is there a thinger in #include <math.h> for this crap?

#2 Junsu

Junsu
  • 1566 posts

Posted 24 March 2011 - 08:14 PM

Im a loser highschooler so Im going to use this as my excuse to fail if I do fail at this.

Math.h lets you use these:
http://www.cplusplus...clibrary/cmath/
You should use it when you find the difference


You should have two methods.
Your first one, main, should have a while loop
Second one should have the (1+1/n)n part

Ok so in main...
create two double values.
One of them will be holding the value before you send it off, and the second should hold the value you send off (otherwise known as n in your post)
Then have a do-while loop. The while part should be while( abs(initialnum - secondvalue) < 1e-8) (use math.h here for the 1e-8)
In the do-while loop, you should...
make your intialvalue = secondvalue and then have your (secondvalue * 2) be sent to the other method and make the return value of the method equal to secondvalue
so now you've updated intialvalue with the new initialvalue and secondvalue should be your new value

outside of the do-while loop, print out second value.

in your second method, heading should be

double methodname (double somevariablename)
and in this method, it should be like
variable = (1 + (1/variable)) * variable ;
// same thing as (1+1/n)n

then return variable



I can do it for you, but if learning is better.

AND IF SOMEONE FUCKING Posted Image'ED ME, IM GOING TO SUICIDEE

EDIT: its the approximation cuz you can't get teh EXACT value of e
and parameter is this part (double &somevariablename)
so I could say that somevariablename is a parameter of the method methodname

Edited by Junsu, 24 March 2011 - 08:16 PM.


#3 1337hunt3r

1337hunt3r
  • 191 posts

Posted 30 March 2011 - 11:12 PM

Junsu covered most of it, but I'll try to clarify:
(You should already know what a class is, at least I hope so)
In your class, you should have two things: a main and a method

The main is what runs when you actually compile the program. So in the main, you should actually write the code to loop through and call your method on each iteration.
The method is like a function in algebra. You pass it a parameter, and it processes stuff using the variable (well technically, you can pass it nothing if you want).
In your case, your method should take in the variable n (which is an int), and return (1+1/n)^n.

Junsu covered the example code pretty well.
If you're having trouble, you should ask your teacher for help.


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users