# little exponent problem

Discussion in 'C++' started by Usman, Feb 6, 2004.

1. ### UsmanGuest

Huy everyone ,

Well I am not a big C++ programmer , I am just a little

young kid on it tryint to learn . Actually I was given an

assignment last week by my teacher which I solved

completely but was unable to go through one question.

I did solved it but I myself wasn't satisfactory. I am sending in

the question and my program code for it. I hope some one

can guide me onto it.

Bye.

uSmAn

HERE GOES THE QUESTION :

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 the
above sum for n taken to be each of the values 1 to 100. To
compare the value calculated by your program and the exact value,
your program should also output ex calculated using the standard function
exp.

HERE IS MY CODE FOR IT : I was unable to store value for a factorial
of 100 so I deducted my loop to 34.

#include<iostream.h>
#include<conio.h>
#include<math.h>

unsigned long factorial ( unsigned long );

int main()
{
char ch;
do
{
clrscr();
unsigned long x,n,result=1;
unsigned long exres;
cout << " Enter a Number : " ;
cin >> x ;
cout << endl << endl;
result = 1 + x;
for ( n = 1; n <= 33 ; n++ )
{
result += ( x ^ n ) / factorial(n);
}
exres = exp(x);
cout << " Your result by programming formula is : " << result ;
cout << endl << endl ;
cout << " The exact value of the exp. func is : " << exres;
cout << endl ;
cout << "\n Want to perform again ? (y/n) ";
cin >> ch;
}
while ( ch != 'n' );
return 0;
}

unsigned long factorial( unsigned long facto )
{
if ( facto <= 1 )
return 1;
else
return facto * factorial(facto -1);
}

Usman, Feb 6, 2004

2. ### ququqaGuest

Usman wrote:

> Huy everyone ,
>
> Well I am not a big C++ programmer , I am just a little
>
> young kid on it tryint to learn . Actually I was given an
>
> assignment last week by my teacher which I solved
>
> completely but was unable to go through one question.
>
> I did solved it but I myself wasn't satisfactory. I am sending in
>
> the question and my program code for it. I hope some one
>
> can guide me onto it.
>
> Bye.
>
>
>
> Yours forever in Digital Paradise.
>
> uSmAn
>
>
>
>
>
> HERE GOES THE QUESTION :
>
>
>
> 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 the
> above sum for n taken to be each of the values 1 to 100. To
> compare the value calculated by your program and the exact value,
> your program should also output ex calculated using the standard function
> exp.
>
>
> HERE IS MY CODE FOR IT : I was unable to store value for a factorial
> of 100 so I deducted my loop to 34.
>
>
>
> #include<iostream.h>
> #include<conio.h>
> #include<math.h>
>
> unsigned long factorial ( unsigned long );
>
> int main()
> {
> char ch;
> do
> {
> clrscr();
> unsigned long x,n,result=1;
> unsigned long exres;
> cout << " Enter a Number : " ;
> cin >> x ;
> cout << endl << endl;
> result = 1 + x;
> for ( n = 1; n <= 33 ; n++ )
> {
> result += ( x ^ n ) / factorial(n);

=> ^^^^^^^
> }
> exres = exp(x);
> cout << " Your result by programming formula is : " << result ;
> cout << endl << endl ;
> cout << " The exact value of the exp. func is : " << exres;
> cout << endl ;
> cout << "\n Want to perform again ? (y/n) ";
> cin >> ch;
> }
> while ( ch != 'n' );
> return 0;
> }
>
>
> unsigned long factorial( unsigned long facto )
> {
> if ( facto <= 1 )
> return 1;
> else
> return facto * factorial(facto -1);
> }
>
>

I think you should use pow(double, double) function instead of ( x ^ n ).

result += pow(x, n) / factorial(n);

regards
q

---------------------------------------------------------
http://members.lycos.co.uk/ququqa2/

ququqa, Feb 6, 2004

3. ### osmiumGuest

Usman writes:

> Well I am not a big C++ programmer , I am just a little
>
> young kid on it tryint to learn . Actually I was given an
>
> assignment last week by my teacher which I solved
>
> completely but was unable to go through one question.
>
> I did solved it but I myself wasn't satisfactory. I am sending in
>
> the question and my program code for it. I hope some one
>
> can guide me onto it.
>
> Bye.
>
>
>
> Yours forever in Digital Paradise.
>
> uSmAn
>
>
>
>
>
> HERE GOES THE QUESTION :
>
>
>
> 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 the
> above sum for n taken to be each of the values 1 to 100. To
> compare the value calculated by your program and the exact value,
> your program should also output ex calculated using the standard function
> exp.
>
>
> HERE IS MY CODE FOR IT : I was unable to store value for a factorial
> of 100 so I deducted my loop to

34.
>
>
>
> #include<iostream.h>
> #include<conio.h>
> #include<math.h>
>
> unsigned long factorial ( unsigned long );
>
> int main()
> {
> char ch;
> do
> {
> clrscr();
> unsigned long x,n,result=1;
> unsigned long exres;
> cout << " Enter a Number : " ;
> cin >> x ;
> cout << endl << endl;
> result = 1 + x;
> for ( n = 1; n <= 33 ; n++ )
> {
> result += ( x ^ n ) / factorial(n);
> }
> exres = exp(x);
> cout << " Your result by programming formula is : " << result ;
> cout << endl << endl ;
> cout << " The exact value of the exp. func is : " << exres;

<snip>

But the result is *not* exact, it is still an approximation.

osmium, Feb 6, 2004
4. ### Alf P. SteinbachGuest

On Sat, 7 Feb 2004 00:36:51 +0500, "Usman" <> wrote:

>Well I am not a big C++ programmer , I am just a little
>young kid on it tryint to learn . Actually I was given an
>assignment last week by my teacher which I solved
>completely but was unable to go through one question.
>I did solved it but I myself wasn't satisfactory. I am sending in
>the question and my program code for it. I hope some one
>can guide me onto it.
>
>
>HERE GOES THE QUESTION :
>
>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 the
>above sum for n taken to be each of the values 1 to 100. To
>compare the value calculated by your program and the exact value,
>your program should also output ex calculated using the standard function
>exp.
>
>HERE IS MY CODE FOR IT : I was unable to store value for a factorial
> of 100 so I deducted my loop to 34.

Note that each term, lets call it y = (x^i)/(i!), is (by simply dividing
out y/y[i-1] and noting the result),

y[0] = 1
y = y[i-1]*x/i for i > 0

This means you can update a variable y incrementally; for each iteration
through the loop multiply by x and divide by the loop count i.

That should get you further, and in a much more efficient way.

Btw., this isn't relly a C++ problem, and so is actually off-topic in this
group.

I suggest using [comp.programming], a more general group, for such questions.

Alf P. Steinbach, Feb 6, 2004
5. ### Default UserGuest

Usman wrote:

> The value ex can be approximated by the sum:
>
> 1 + x + x2/2! + x3/3! + . + xn/n!

> #include<iostream.h>

This is an obsolete, prestandard header, and should not be used.

> #include<conio.h>

This is completely nonstandard and not necessary for your program.

> #include<math.h>
>
> unsigned long factorial ( unsigned long );
>
> int main()
> {
> char ch;
> do
> {
> clrscr();

Don't do that, it's an unnecessary complication.

> result = 1 + x;
> for ( n = 1; n <= 33 ; n++ )
> {

You actually made things a bit more complicated than necessary with the
initialization. You understand that the actual series is:

x0/0! + x1/1! + x2/2! . . . .

You can just start your loop at 0 and init result to 0.

> result += ( x ^ n ) / factorial(n);

You seem to be under the impression that ^ is an exponentation operator,
it's not. It is the exclusive-or operator. You want the pow() function.

The algorithm doesn't look right either. You already set result to 1 +
x, then in the first iteration of the do-while you add in another x.

Brian Rodenborn

Default User, Feb 6, 2004
6. ### Default UserGuest

"Alf P. Steinbach" wrote:

> Btw., this isn't relly a C++ problem, and so is actually off-topic in this
> group.

I think he had plenty of C++ problems as well

Brian Rodenborn

Default User, Feb 6, 2004
7. ### Jens HannemannGuest

Usman wrote:

> The value ex can be approximated by the sum:
>
> 1 + x + x2/2! + x3/3! + . + xn/n!

> HERE IS MY CODE FOR IT : I was unable to store value for a factorial
> of 100 so I deducted my loop to
> 34.

You seem to have a pretty cool teacher who knows a thing or two about
computational science . You immediately fell for the computation of the
factorial which of cource blows up pretty fast.

The solution here is thinking in iterative terms. Note that the ratio
between the n+1-st and the n-th term is always x/n. You can use this to
compute the terms of the sum iteratively, which will rapidly become very
small. To make this on-topic, here is some stable sample code that computes
exp(1) aka e to n=50i (note how fast the thing converges):

#include <iostream>
#include <cmath>

int main()
{
const int n = 50;
const double x = 1.0;

double expo = 1.0;
double term = 1.0;
for(int i=1; i<=n; ++i){
term = term*(x/i);
expo = expo+term;
std::cout << "Term: " << term << " Series: " << expo << " Exact: " <<
std::exp(x) << std::endl;
}
}

Have fun,

Jens

--
Jens Hannemann --- --- OpenPGP Key Available
University of Kentucky - Dept. of Electrical and Computer Engineering

Jens Hannemann, Feb 6, 2004
8. ### rossumGuest

On Sat, 7 Feb 2004 00:36:51 +0500, "Usman" <>
wrote:

> Huy everyone ,
>
>Well I am not a big C++ programmer , I am just a little
>
>young kid on it tryint to learn . Actually I was given an
>
>assignment last week by my teacher which I solved
>
>completely but was unable to go through one question.
>
>I did solved it but I myself wasn't satisfactory. I am sending in
>
>the question and my program code for it. I hope some one
>
>can guide me onto it.
>
>Bye.
>
>
>
>
>uSmAn
>
>
>
>
>
>HERE GOES THE QUESTION :
>
>
>
>The value ex can be approximated by the sum:
>
>
>
> 1 + x + x^2/2! + x^3/3! + . + x^n/n!
>
>
>
>Write a program that takes a value x as input and outputs the
>above sum for n taken to be each of the values 1 to 100. To
>compare the value calculated by your program and the exact value,
>your program should also output e^x calculated using the standard function
>exp.
>
>
>HERE IS MY CODE FOR IT : I was unable to store value for a factorial
> of 100 so I deducted my loop to 34.
>
>
>
>#include<iostream.h>

Better to use "#include <iostream>", without the ".h". The ".h"
versions are obsolete and are included for backward compatibility with
old C code.

>#include<conio.h>

conio.h is non standard and probably not required.

>#include<math.h>

Use "#include <cmath>". Again this is the new C++ version. All the
old C standard headers "<xxxx.h>" have become <cxxxx> in C++.

>
>unsigned long factorial ( unsigned long );
>
>int main()
> {
> char ch;
> do
> {
> clrscr();

Not really required and not portable (important when you get a job
programming). Better to remove it.

> unsigned long x,n,result=1;

Better to put spaces after commas as it makes the code more readable
(also important when you get a job programming). Even better to have
one declaration per line (same reason).

> unsigned long exres;
> cout << " Enter a Number : " ;
> cin >> x ;
> cout << endl << endl;
> result = 1 + x;
> for ( n = 1; n <= 33 ; n++ )

Using ++n is never less efficient that n++. Better to get into the
habit of using ++n unless there is a specific reason not to.

> {
> result += ( x ^ n ) / factorial(n);

You need pow() here and not the ^ operator. C++ does not use the ^
are using.

> }
> exres = exp(x);
> cout << " Your result by programming formula is : " << result ;
> cout << endl << endl ;
> cout << " The exact value of the exp. func is : " << exres;
> cout << endl ;
> cout << "\n Want to perform again ? (y/n) ";
> cin >> ch;
> }
> while ( ch != 'n' );
> return 0;
> }
>
>
>unsigned long factorial( unsigned long facto )
> {
> if ( facto <= 1 )
> return 1;
> else
> return facto * factorial(facto -1);
> }
>

Recursion can be elegant to code, but it can also be slow and eat up
memory. Often it is more efficient to unwind a single recursion like
this into a simple loop, for example:
unsigned long factorial(unsigned long facto)
{
unsigned long retVal = 1;
for (int i = 2; i <= facto; ++i)
{
retVal *= i;
} // end for
return retVal;
} // end factorial()

>
>

Others have already mentioned that you need to look at the algorithm
you are using. Rather than calculating each new term from scratch
think about how to calculate each term from the previous term. If you
already have a value for x^n / n! think how can you work out x^(n + 1)
/ (n + 1)! [Using ^ to indicate exponentiation]

Asking questions is a good habit to get into. You can learn a lot

rossum

--

The Ultimate Truth is that there is no Ultimate Truth

rossum, Feb 6, 2004
9. ### UsmanGuest

Well , thank you all for helping me out on this problem.
I really wasn't aware of such precious tips which I got
from this group. More over the using of exp() function
instead of the ^ operator solved the problem. Also the
special tips and the hot ways of a C++ programmer really
helped me a lot. I really want to express my gratitude to all
of you. Thanks , Bye