| jayygerman (11) | |
|
write a program to evaluate the value of e^x by using the formula: e^x= 1+ x/1! +x^2/2! + x^3/3!... evaluate the above expression for different values of x and number of terms in the expression. The user should be allowed enter x and number of terms for which the expression is calculated. | |
|
|
|
| L B (3817) | |
| What have you written so far? | |
|
|
|
| jayygerman (11) | |
|
#include <stdio.h> #include <math.h> int main() { int x,n,counter; printf("Enter value of x : "); scanf ("%d",&x); printf("Enter number of terms (n) : "); scanf ("%d",&n); counter=counter+1; this is all I have :/ I'm not sure how to start my loops on this one. in class all we've covered up to is for loops, while loops, and inner loops, if else loops as well. I know the value of e=2.7182. I just can't figure out what should go inside the loops. | |
|
|
|
| ne555 (4385) | |
|
Simplify the problem. ¿can you calculate the factorial of a number? | |
|
|
|
| jayygerman (11) | |
| yeah i can, 4!=4*3*2*1, 10!=10*9*8*7*6*5*4*3*2*1 and so forth... | |
|
|
|
| Peter7 (7) | |
| you will have to first write the function that computes the factorial, and then do a while loop with the sum from i=0 to n with pow(x,i)/factorial(i). | |
|
|
|
| jayygerman (11) | |
|
#include <stdio.h> #include <math.h> int main() { int x,n, factorial=1; printf("Enter value of x : "); scanf ("%d",&x); printf("Enter number of terms (n) : "); scanf ("%d",&n); for(x=1; x<=n, x++) { fact = fact * x printf("Factorial of %d = %d\n", n, fact); return 0; } okay, so I'm pretty sure the for loop is wrong..ahhhh! is it normal for programming to be this hard? this is my first time taking it. I mean i understand what each loop does, and whats going on, but writing it is a different story. | |
|
|
|
| L B (3817) | |||
|
Programming is generally easier for people who think in a very logical manner. It will obviously be harder for other people, but as with anything else, practice makes it easier. The problem with your code is that you're missing a closing brace after the loop:
| |||
|
|
|||
| Chervil (1206) | |
|
You could follow the suggestion from Peter7, to use a function to calculate the factorial, and the library function pow() and find each term of the series like that. In fact the code would probably look quite clean and tidy that way. But there is another approach. Since we are dealing with a series, and need to find every term, we can take advantage of the fact that there is a simple relationship of each successive term to the previous one. That means each term requires just one multiply and one divide. Considerably simpler and more efficient. Lets look again at the series: e^x = 1 + x/1! + x^2/2! + x^3/3!... first term = 1 second term = (first term) * x / 1 third term = (second term) * x / 2 fourth term = (third term) * x / 3 ... Now all you need to do is create a loop which calculates each term as described, and adds them to a variable which will accumulate the total. In fact, it is usually specified in the instructions for this sort of exercise that you are not allowed to use the functions in the <cmath> library. After all, if it is acceptable to use pow(), then why not just use exp() to get the answer? | |
|
Last edited on
|
|
| jayygerman (11) | |
| thats the thing, i dont know what to put in my loops. Im honestly lost on this problem like dead lost. i feel dumb cause I know once i see hte code, imma feel dumb for not figuring it out. | |
|
|
|
| voidmainvoid (10) | |||
This should work. | |||
|
|
|||
| Chervil (1206) | |||
|
You need to calculate the sum of the terms of a series. Here's an example which simply gets the sum of the integers from 1 to 10. 1+2+3+4+5+6+7+8+9+10 It uses a loop to do so. The principle is very common in many programs. Before the loop starts, do whatever initialisation is required. in this case that means define the total and set it to zero. Inside the loop, add the value of each term of the series to the total.
You should be able to adapt this idea to find the sum of any series. | |||
|
Last edited on
|
|||
| Chervil (1206) | |||
|
This is as near as I can get without solving the entire problem for you. The main thing you need to do is to replace the repeated code with a proper loop where the number of terms accumulated will agree the the number requested by the user.
Note, if you use a for-loop, the code will get shorter, as both line 18 and this line count = count + 1; are done in the for statement, which means the code ends up remarkably concise.
| |||
|
Last edited on
|
|||