January 2nd, 2014, 06:06 PM

Generating e^x in C
Hi..
I am trying to generate the e series by having the user accept x (power of) and then generating the value.
this is the code i wrote:
Code:
#include<stdio.h>
int main()
{
int i, j, user_input, fact = 0;
float final_ans;
printf("\nEnter the power that you want to calculate e for: ");
scanf("%d", &user_input);
if(user_input == 0)
{
printf("The answer is 1");
}
for(i = 1; i <= user_input; i++)
{
for(j = 1; j <= user_input; j++)
{
fact = fact * j;
}
final_ans = 2 + ((1/fact)*i);
printf("%f", final_ans);
}
return 0;
}
I am not sure why it isn't working :(
Any help is greatly appreciated!
January 2nd, 2014, 06:49 PM

First Question: What do you mean by "it isn't working"? What exactly does that mean?
Example: You decide that your car "isn't working". So you call the auto mechanic to find out what's wrong with it and you tell him nothing more than "it isn't working". Oh, and maybe you text him a photo of the car. How well do you think that he will be able to perform that task you just gave him? He wouldn't be able to help you at al,right? So why treat us like that?
What did you expect your program to do? As in describing to us the exact output that you expect. Are you getting output that is different? Exactly what is that output? Or is your program crashing? Is it reporting a runtime error?
IOW, we cannot read your mind! And we do not appreciate your stupid guessing games either!
Second Question: You perform this assignment repeatedly: fact = fact * j;
fact is initialized to zero. Anything times zero is always zero, no matter how many times you do it. Nothing in the program ever changes fact to anything other than zero.
Third Question: What happens when you divide by zero? After repeatedly setting fact to zero, you perform this assignment: final_ans = 2 + ((1/fact)*i);
The term (1/fact) is performing division by zero. What happens when you divide by zero?
Fourth Question: How are you trying to calculate e**x? By a power series? If so, please tell us what that series is.
I thought it was supposed to:
e**x = 1 + x/1! + (x**2)/2! + (x**3)/3! + ... + (x**n)/n! + ...
At least it was according to the Russians back in 1956.
(HINT: user_input should go where x is)
Last edited by dwise1_aol; January 2nd, 2014 at 07:03 PM.
January 2nd, 2014, 07:23 PM

What did you expect your program to do?: I was guessing that is obvious. But if in case it isn't, i was expecting it to have the user input the value of x user_input) and generate the e^x series.
Second Question: I don't see a question. And yes, it was done in error. I am not a pro. You (i am hoping) are.
Third Question: I get it. It works in line with your second question/statement.
Fourth Question: Per Euler's formula. And yes. you're right. It is e^x = 1 + x/1! + x^2/2! + .. My bad,a gain.
January 2nd, 2014, 08:34 PM

You've used user_input as a number of terms, and you've ignored the pow(x, term_number) factor in the numerator. And whatever else. I expect a working solution to contain a fragment like
Code:
float x, term, e_to_the_x;
/*...*/
x = user_input;
term = 1;
e_to_the_x = 0;
for (n = 1; e_to_the_x < e_to_the_x + term; ++n) {
e_to_the_x += term;
term *= x / n;
}
Wow! Quite the observation. The next term can be simply computed from the current term.
Last edited by b49P23TIvg; January 2nd, 2014 at 08:44 PM.
[code]
Code tags[/code] are essential for python code and Makefiles!
January 3rd, 2014, 12:42 PM

will try that and let you know.. thnx a ton :)
January 3rd, 2014, 07:28 PM

I translated the code to python, ran it for a few values of x including negative values. These I compared with
Code:
^VECTOR NB. exponential function in j www.jsoftware.com
The values agree, I've tested the code, know it works, you need not report back to me for my sake. I expect the "finished" comparison needs work if you want to use complex arguments.
[code]
Code tags[/code] are essential for python code and Makefiles!