little exponent problem

U

Usman

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);
}
 
Q

ququqa

Usman said:
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
 
O

osmium

Usman said:
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.
 
A

Alf P. Steinbach

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.
 
D

Default User

Usman said:
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
 
D

Default User

Alf P. Steinbach said:
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
 
J

Jens Hannemann

Usman said:
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
 
R

rossum

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 + 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 said:
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 ^
operator for powering. See my comments below about the algorithm you
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
just by asking.


rossum
 
U

Usman

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

Yours forever in Digital Paradise
uSmAn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top