By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

approximating a series

P: 6
The value ex can be approximated by the sum
1 + x + x2/2! + x3/3! + + xn/n!
Write a program that takes a value x as input and outputs this sum for n taken
to be each of the values 1 to 100. The program should also output ex
calculated using the predefined function exp. The function exp is a predefined
function such that exp(x) returns an approximation to the value ex. The
function exp is in the library with the header file cmath. Your program should
repeat the calculation for new values of x until the user says she or he is
through.
Use variable of type double to store the factorials or you are likely to produce
integer overflow (or arrange your calculation to avoid any direct calculation of
factorials). 100 lines of output might not fit comfortably on your screen. Output
the 100 output values in a format that will fit all 100 values on the screen. For
example, you might output 10 lines with 10 values on each line.
Mar 13 '07 #1
Share this Question
Share on Google+
8 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
The value ex can be approximated by the sum
1 + x + x2/2! + x3/3! + + xn/n!
Write a program that takes a value x as input and outputs this sum for n taken
to be each of the values 1 to 100. The program should also output ex
calculated using the predefined function exp. The function exp is a predefined
function such that exp(x) returns an approximation to the value ex. The
function exp is in the library with the header file cmath. Your program should
repeat the calculation for new values of x until the user says she or he is
through.
Use variable of type double to store the factorials or you are likely to produce
integer overflow (or arrange your calculation to avoid any direct calculation of
factorials). 100 lines of output might not fit comfortably on your screen. Output
the 100 output values in a format that will fit all 100 values on the screen. For
example, you might output 10 lines with 10 values on each line.
Ok, so what is your question about that?
Mar 13 '07 #2

DeMan
100+
P: 1,806
First work through the problem in the most obvious way, creating a method that works out x^n/n!, and create a loop to iterate through adding these values from 1 to 100.

You'll find your output may not be so accurate (factorials get REALLY big, even at 10! we're already up to 3628800.....) This is why the problem suggests that you try to avoid using a direct fatorial calculation). Likewise x^100 could be a reasonably large value, so you may like to find a shortcut for that also....
HINT: you can simplify an individual (x^n)/n! to something like (x/1) * (x/2) * (x/3) * .. .. * (x/n). While this may talke longer to calculate, you'll find the answer is more likely to be accurate, as you are always dealing with manageable quantities.
Mar 13 '07 #3

P: 6
Ok, so what is your question about that?
i need to write a programe for this equation .. i have no idea how to start it
Mar 14 '07 #4

DeMan
100+
P: 1,806
How much do you know about programming?
how well do you understand the equation?


The experts here are more than happy to help, but they won't give you a complete answer (after all that defeats the purpose of doing an assignment).

When you say you don't know how to start, you obviously have a lot of Mathematical information in the question, so I assume you need help with the actual programming. I doubt the question would have been posed if you had had no exposure to programming, so I'm not really sure where you are at.

Basically if you plan how to do it first, and then try to implement some basic things. You might first like to start by making a function that adds x consecutive numbers.....
Having done this, you might like to refine that method into something more useful.

If you get stuck, please post again, but please understand that we like to see that you have made an effort. You will never learn without doing, and while I appreciate that sometimes it may be difficult, the best way to get help is to show how much you know....
Mar 14 '07 #5

P: 6
can sm one plz tell me how to declare the variables ///
Mar 14 '07 #6

P: 6
How much do you know about programming?
how well do you understand the equation?


The experts here are more than happy to help, but they won't give you a complete answer (after all that defeats the purpose of doing an assignment).

When you say you don't know how to start, you obviously have a lot of Mathematical information in the question, so I assume you need help with the actual programming. I doubt the question would have been posed if you had had no exposure to programming, so I'm not really sure where you are at.

Basically if you plan how to do it first, and then try to implement some basic things. You might first like to start by making a function that adds x consecutive numbers.....
Having done this, you might like to refine that method into something more useful.

If you get stuck, please post again, but please understand that we like to see that you have made an effort. You will never learn without doing, and while I appreciate that sometimes it may be difficult, the best way to get help is to show how much you know....

am a biggner not very sure of c++ coding i want to learn but i am unable to do this equation . if i have the answer i can try it and learn how it works
Mar 14 '07 #7

Expert 100+
P: 1,510
am a biggner not very sure of c++ coding i want to learn but i am unable to do this equation . if i have the answer i can try it and learn how it works
you need to start by implementing some sime program defining variables and executing loops, have a look at the tutorial
http://www.cplusplus.com/doc/tutorial/
Mar 14 '07 #8

DeMan
100+
P: 1,806
can sm one plz tell me how to declare the variables ///
some examples.....(before you have a look at the tutorial listed above)

Expand|Select|Wrap|Line Numbers
  1. int i;  /* i is now of type int */
  2. float f; /* f has been declared as a floating-point type */
  3. char c; /* c has been declared as a character (8bit integer) */
  4.  
  5. int k = 0; /* k has been declared an int, and initialised to 0 */
  6.  
  7. /* There are other base types also, but i leave it for you to find them out */
  8.  
  9.  
Mar 14 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.