little exponent problem

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

  1. Usman

    Usman Guest

    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);
    }
    Usman, Feb 6, 2004
    #1
    1. Advertising

  2. Usman

    ququqa Guest

    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
    #2
    1. Advertising

  3. Usman

    osmium Guest

    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
    #3
  4. 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
    #4
  5. Usman

    Default User Guest

    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
    #5
  6. Usman

    Default User Guest

    "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
    #6
  7. 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
    #7
  8. Usman

    rossum Guest

    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.
    >
    >
    >
    >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
    rossum, Feb 6, 2004
    #8
  9. Usman

    Usman Guest

    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
    Usman, Feb 8, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Protoman

    Problem with Exponent program

    Protoman, Dec 18, 2005, in forum: C++
    Replies:
    11
    Views:
    487
    Protoman
    Dec 20, 2005
  2. Wayne Shu
    Replies:
    1
    Views:
    427
    John Carson
    Mar 18, 2007
  3. ThaDoctor
    Replies:
    3
    Views:
    367
    Alan Woodland
    Sep 28, 2007
  4. asp exponent problem

    , Mar 20, 2006, in forum: ASP General
    Replies:
    2
    Views:
    95
    Dave Anderson
    Mar 20, 2006
  5. Daniel
    Replies:
    1
    Views:
    193
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page