# small program to calculate e

• 12-27-2013
Dave11
small program to calculate e
the program goes like this:
it should calculate the value of e^x by the series
1+x+x^2/2!+x^3/3!...+x^n/n!
when the user inputs a value , eps , which the program has to stop compute e^x when x^n/n<eps.

I wrote 2 function , one calculating the factorial result (fact), and one calculates the value x^n/n! (monom).
these functions works well, but the program keeps giving me 1 as result for e^x (or it gets stuck..)
it seems that the problem comes from the condition monom (x,j)<eps, but I can't see why...
Code:

#include <stdio.h>
#include <math.h>

float fact (int);
double monom (float x, int n);

int main (void)
{
float x;
printf ("please enter x value for e^x: ");
scanf ("%f", &x);
double exp=1;
float eps;
scanf ("%f", &eps);
int j;
for (j=1; monom (x,j)<eps; j++)
exp+=monom (x,j);
printf ("%f",exp);

return 0;
}

float fact (int a)
{
float result=1;
int i;
for (i=1;i<=a;i++)
result*=i;
return result;
}

double monom (float x, int n)
{
return (pow(x,n)/fact(n));
}

can you guys help me? thanks a lot!
• 12-27-2013
Don't you want your loop to terminate once the difference of the successive terms in the series becomes less than epsilon rather than greater than?

Also it seems rather inconsistent that you name your variable eps (suggesting epsilon) and then ask the user to enter delta :).

Otherwise though the code is quite good, you need to work on your indentation though and personally I'm not a fan of declaring my variables throughout my code but to each his own.
• 12-27-2013
Dave11
Hi deadPlanet , isn't what you suggesting is what the program does?
I want to stop the loop when x^n/n!<eps, eps is the amount of accuracy we want for e^x.
how would you rephrase the condition? (I admit I don't quit get your thought..)

edit:
well, I chang the < to be > and it worked , I guess I got confused with the middle component of the for loop. the middle component is actually "do it while.." and not "do it until" and that what got me confused ... beginners things ..
thank you very much!
• 12-27-2013
c99tutorial
Also notice the name "exp" is already used by the standard library, so using that as a variable name is not a good idea:

Man Page for exp (freebsd Section 3) - The UNIX and Linux Forums
• 12-27-2013
Hodor
Quote:

Otherwise though the code is quite good.

Serious question, what makes it good? To be honest (sorry Dave11, you're learning so I understand) I can't find much good about it.
• 12-27-2013
Quote:

Originally Posted by Hodor
Serious question, what makes it good? To be honest (sorry Dave11, you're learning so I understand) I can't find much good about it.

The code is not unnecessarily long. All functions and variables have close to reasonable names. Functions are correctly prototyped. Proper standard header files are included. We even have int main( void ) and return 0. These are all things that lots of learning programmers struggle with.

This is not production code and I won't be including it in my own projects but Dave11 is very much on the right track. One of the major obstacles that stand in the way of him becoming a good programmer is quitting too easily. I'm of the belief that encouragement gives people some self-belief and so they are less likely to quit.

I only ever compliment people on things that I think they are actually doing well at (relative to their skill level). I do not just make things up to make people feel good.

I guess it's more fun to be a dickhead towards people though so we can try your idea of telling people their code is bad from now on.
• 12-27-2013
Hodor
Quote:

I guess it's more fun to be a dickhead towards people though so we can try your idea of telling people their code is bad from now on.

If you think I was being a dickhead, then you're mistaken. It was an honest question. I don't mince words (often) but at the same time I did not say anything at all about the programmer; I was referring to the code and my comment (question, actually) was purely objective.
• 12-27-2013
kermit
Code:

#include <stdio.h>
#include <math.h>

float fact(int);
double monom(float x, int n);

int main(void)
{
float x;
printf("please enter x value for e^x: ");
scanf("%f", &x);
double exp = 1;
float eps;
scanf("%f", &eps);
int j;
for (j = 1; monom(x, j) < eps; j++)
exp += monom(x, j);
printf("%f", exp);

return 0;
}

float fact(int a)
{
float result = 1;
int i;
for (i = 1; i <= a; i++)
result *= i;
return result;
}

double monom(float x, int n)
{
return (pow(x, n) / fact(n));
}

I just felt like indenting the code. Ah, much nicer to look at. :D

On a serious note, Dave11, do you have access to a debugger, where you can step through your program while it is running, and examine your variables to see what is going on? This can be a very instructive thing to do sometimes.
• 12-27-2013
iMalc
Your for loop condition is both unnecessarily inefficient, and incorrect.
• 12-28-2013
Dave11
hi all!
I posted this code after many attempts to make it work , so I kind of abandoned the indentation at some point, usually I DO write the code with indentation.
about the delta-epsilon issue - this is an exercise for me, so I didn't put much thought on the output text..

kermit: I use dev-c++ , so I do have excess to debugger, but this program has good syntax , but bad logic, so debugger wasn't helpful at the time
thanks all for your comments ! I will surly post more questions as they come!
• 12-28-2013
tabstop
Quote:

Originally Posted by Dave11
kermit: I use dev-c++ , so I do have excess to debugger, but this program has good syntax , but bad logic, so debugger wasn't helpful at the time

That is an interesting statement, as a debugger is useless for syntax errors but excellent for logic errors. I hope you're not thinking of the warnings you get from the compiler as your debugger, because it isn't. A debugger is a program (like gdb) that allows you to step through a program's execution, one step at a time, which will allow you to see when and how your logic errors manifest themselves.
• 12-28-2013
King Mir
I recommend using a more recent IDE than Dev-C++. The compiler it comes with is very out of date.
• 12-29-2013
kermit
Quote:

Originally Posted by King Mir
I recommend using a more recent IDE than Dev-C++. The compiler it comes with is very out of date.

At the very least make sure it is the most up to date version. There is a new (forked) version of Dev-C++. You can find it here