November 10th, 2013, 01:23 AM

Originally Posted by Jacques1
And that's correct for x = 1.1. You're doing the calculation of e^x after the for loop, at which point x has just exceeded 1.0. I guess you meant the calculation to be inside of the loop.
This is clearly a problem of your completely messed up formatting. You've stumbled upon your sloppy indentation.
Correct formatting is crucial for correct code. It's not an optional prettifying step you may or may not do at the end. If you mess up the the layout, you'll almost always mess up the content as well, because you simply won't understand your own code.
So I strongly suggest you grab a proper editor or IDE, choose your favorite indent style, choose an indent depth and then stick to that. If you accidentally break the formatting in your code, you fix it just like you fix a bug  because it actually is a bug.
I know that things like formatting or variable names don't seem to be important when you've just started to program ("The compiler doesn't care, so why should I?"). But you'll find out that those seemingly trivial things have a huge impact on the quality and correctness of your code. The compiler doesn't care about readbility. But your brain does, and that's where the code actually gets produced.
I've redone the code.
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
long factorial (int);
double exponent (double);
double sinh (double);
int main()
{
double x;
double result;
double e=0;
double sin_h;
for (x=1.0; x<=1.0; x=x+0.1)
{
printf("X is: %.1lf\n", x);
sin_h= sinh(x);
e = exponent (x);
}
system("pause");
return 0;
}
double exponent (double x)
{
double e;
double sin_h;
double sum=0;
int n;
n=0;
while (1)
{
e = pow(x, n) / factorial(n);
sum+= e;
if( e < 0.000001 && e > 0.000001)
break;
n++;
}
printf("e is %lf\n",sum);
}
long factorial (int n)
{
int factres= 1;
while (n > 1)
{
factres = factres*n;
n;
}
return (factres);
}
double sinh (double x)
{
double sin_h;
sin_h= (exponent(x)exponent(x))/2;
printf("sinh is %lf\n",sin_h);
}
I know its almost the same code. However I want to know why when I run the code for every value of x sinh is equal to: 1.#QNAN0
November 10th, 2013, 02:14 AM

C'mon, it shouldn't be so hard to properly format that stuff.
 Statements on the same level start in the same column.
 If you go one level down, you hit <tab> exactly once.
 Consistent spacing around operators and functions.
 When you change the code, you reformat it according to the rules above.
Here, let me help you:
c Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
long factorial(int);
double exponent(double);
double sinh(double);
int main()
{
double x;
double result;
double e = 0;
double sin_h;
for (x = 1.0; x <= 1.0; x = x + 0.1)
{
printf("X is: %.1lf\n", x);
sin_h = sinh(x);
e = exponent(x);
}
system("pause");
return 0;
}
double exponent(double x)
{
double e;
double sum = 0;
int n;
n = 0;
while (1)
{
e = pow(x, n) / factorial(n);
sum += e;
if (e < 0.000001 && e > 0.000001)
break;
n++;
}
printf("e is %lf\n", sum);
}
long factorial(int n)
{
int factres = 1;
while (n > 1)
{
factres = factres * n;
n;
}
return factres;
}
double sinh(double x)
{
double sin_h;
sin_h = (exponent(x)  exponent(x)) / 2;
printf("sinh is %lf\n", sin_h);
}
You should also consider refactoring the while (1) ... break with a proper dowhile loop.
Your exponent() doesn't return anything.
November 10th, 2013, 05:23 AM

Originally Posted by Jacques1
C'mon, it shouldn't be so hard to properly format that stuff.
 Statements on the same level start in the same column.
 If you go one level down, you hit <tab> exactly once.
 Consistent spacing around operators and functions.
 When you change the code, you reformat it according to the rules above.
Here, let me help you:
c Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
long factorial(int);
double exponent(double);
double sinh(double);
int main()
{
double x;
double result;
double e = 0;
double sin_h;
for (x = 1.0; x <= 1.0; x = x + 0.1)
{
printf("X is: %.1lf\n", x);
sin_h = sinh(x);
e = exponent(x);
}
system("pause");
return 0;
}
double exponent(double x)
{
double e;
double sum = 0;
int n;
n = 0;
while (1)
{
e = pow(x, n) / factorial(n);
sum += e;
if (e < 0.000001 && e > 0.000001)
break;
n++;
}
printf("e is %lf\n", sum);
}
long factorial(int n)
{
int factres = 1;
while (n > 1)
{
factres = factres * n;
n;
}
return factres;
}
double sinh(double x)
{
double sin_h;
sin_h = (exponent(x)  exponent(x)) / 2;
printf("sinh is %lf\n", sin_h);
}
You should also consider refactoring the
while (1) ... break with a proper
dowhile loop.
Your
exponent() doesn't return anything.
Sorry I didn't exactly know how to properly format it. Ok so now my sinh does give a result after I made my exponent function return a value. However I want it to return 2.71 (e) as the value.
I want my output to be
when x= 1 sinh(x)= 1.175201
x= 0.9 sinh(x)= −1.026517
and so on until x=1.
Right now i get for x=1 sinh(x)= 0.91261
November 10th, 2013, 08:12 AM

Originally Posted by vilodic
Right now i get for x=1 sinh(x)= 0.91261
Are you sure you're looking at the right numbers? My output is
Code:
X is: 1.0
e is 2.718282 > this is exponent(1.0)
e is 0.367879 > this is exponent(1.0)
sinh is 1.175201
And that's correct (within the precision limits you've chosen).
November 10th, 2013, 01:24 PM

Originally Posted by Jacques1
Are you sure you're looking at the right numbers? My output is
Code:
X is: 1.0
e is 2.718282 > this is exponent(1.0)
e is 0.367879 > this is exponent(1.0)
sinh is 1.175201
And that's correct (within the precision limits you've chosen).
I want the exponent function to always use e= 2.71... so that exponent(x) = e^x
I don't get those readings for sinh. I only did the printing of the e value to check that my program was doing something at least. I just need to print x from 1 t0 1 (1,0.9,0.8...) (which my program does) and for each x print the corresponding value of sinh (x).
So when x=0.9 sinh= exponent(0.9)  exponent(0.9) / 2
and exponent should be e(2.71..)^x so e^0.9.
November 10th, 2013, 01:52 PM

Originally Posted by vilodic
I want the exponent function to always use e= 2.71... so that exponent(x) = e^x
I don't get those readings for sinh. I only did the printing of the e value to check that my program was doing something at least. I just need to print x from 1 t0 1 (1,0.9,0.8...) (which my program does) and for each x print the corresponding value of sinh (x).
So when x=0.9 sinh= exponent(0.9)  exponent(0.9) / 2
and exponent should be e(2.71..)^x so e^0.9.
But you're code is producing the right number.
November 10th, 2013, 02:04 PM

Give us the exact code you're currently using (formatted, please).
November 10th, 2013, 02:58 PM

It is the same code but I added return sum to this.
Code:
double exponent (double x)
{
double e;
double sum = 0;
int n;
n=0;
while (1)
{
e = pow(x, n) / factorial(n);
sum += e;
if (e < 0.000001 && e > 0.000001)
break;
n++;
}
printf("e is %lf\n",sum);
return sum;
}
November 10th, 2013, 03:02 PM

Are you still not getting correct answers?
November 10th, 2013, 03:36 PM

Originally Posted by bullet
Are you still not getting correct answers?
Ok I figured it out it was something with the compiler rather than my code. I'm getting the right answers.
One question. Is there a way to Print values in a table like form
like
X =1 Sinh(x)=1.175
X =0.9 Sinh(x)=1.026
rather than how it is now
x=1
sinh(x)= 1.175
November 10th, 2013, 06:49 PM

Originally Posted by vilodic
Ok I figured it out it was something with the compiler rather than my code. I'm getting the right answers.
One question. Is there a way to Print values in a table like form
like
X =1 Sinh(x)=1.175
X =0.9 Sinh(x)=1.026
rather than how it is now
x=1
sinh(x)= 1.175
You can try inserting tabs
November 10th, 2013, 10:57 PM

Originally Posted by bullet
You can try inserting tabs
Thanks a lot. And thanks to all that helped and remained patient with me.