473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

little exponent problem

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);
}
Jul 22 '05 #1
8 1928


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/
Jul 22 '05 #2
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.
Jul 22 '05 #3
On Sat, 7 Feb 2004 00:36:51 +0500, "Usman" <ga*****@hotmail.com> 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[i] = (x^i)/(i!), is (by simply dividing
out y[i]/y[i-1] and noting the result),
y[0] = 1
y[i] = 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.

Jul 22 '05 #4
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
Jul 22 '05 #5
"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
Jul 22 '05 #6
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 --- j.*********@ieee.org --- OpenPGP Key Available
University of Kentucky - Dept. of Electrical and Computer Engineering

Jul 22 '05 #7
On Sat, 7 Feb 2004 00:36:51 +0500, "Usman" <ga*****@hotmail.com>
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 + 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 ^
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
--

The Ultimate Truth is that there is no Ultimate Truth
Jul 22 '05 #8
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
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bryan | last post by:
is there a way to make the Decimal class not print the exponent version of the decimal? >>> str(Decimal('1010')) '1010' >>> str(Decimal((0, (1, 0, 1), 1))) '1.01E+3' >>>
1
by: adahili | last post by:
Hi, I use cout to print numbers in scientific notation. I noticed that a number like 12.34 is sometimes (compiler-dependent) printed as 1.234e+1 or 1.234e+01 or 1.234e+001. Since I know the...
5
by: sankar | last post by:
Hi, I am using a Q14.18 value. There are tables used in my program which are being indexed by the exponent and mantissa parts of the corresponding floating point value. So how can I get the...
2
by: Jens Bloch Helmers | last post by:
How can I control the number of digits in the exponent when writing floats to a file? It seems that Python2.4.2(winXP) prints three digits anyway. >>> print 1.0e50 1e+050 >>> print '%e' %...
11
by: Protoman | last post by:
I'm getting a compilation error with this program, something about the instation of invArg; here's the code: #include <iostream> #include <cstdlib> using namespace std; template<class T>...
6
by: hrbigelow | last post by:
hi, i was wondering if anyone has ever seen a floating point library that uses at least 20 bits to represent the exponent, rather than the 11 bits for a double-precision standard IEEE-754 float. ...
1
by: Wayne Shu | last post by:
Hei everyone: Just see the output of the following program #include <iostream> #include <cstdlib> #include <limits> int main() { std::cout << "minimum exponent of double: " <<
1
by: raj4ever188 | last post by:
Hi, I have a problem. I want to get output in terms of fixed exponent from sprintf. for example if i write sprintf(buf, "%.3e", ground_cap); I want the exponent part will be in terms of...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, By default, the "g" format specifier seems to use 2 digits in the exponent if it decides to use the scientific format. I.e., Double.ToString("g"). How do I control the number of...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.