little exponent problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Usman

    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<iostre am.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);
    }


  • ququqa

    #2
    Re: little exponent problem



    Usman wrote:
    [color=blue]
    > 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<iostre am.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);[/color]
    => ^^^^^^^[color=blue]
    > }
    > 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);
    > }
    >
    >[/color]

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

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



    regards
    q





    ---------------------------------------------------------



    Comment

    • osmium

      #3
      Re: little exponent problem

      Usman writes:
      [color=blue]
      > 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[/color]
      34.[color=blue]
      >
      >
      >
      > #include<iostre am.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;[/color]
      <snip>

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


      Comment

      • Alf P. Steinbach

        #4
        Re: little exponent problem

        On Sat, 7 Feb 2004 00:36:51 +0500, "Usman" <game_pk@hotmai l.com> wrote:
        [color=blue]
        >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.[/color]


        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.programmin g], a more general group, for such questions.

        Comment

        • Default User

          #5
          Re: little exponent problem

          Usman wrote:
          [color=blue]
          > The value ex can be approximated by the sum:
          >
          > 1 + x + x2/2! + x3/3! + . + xn/n![/color]
          [color=blue]
          > #include<iostre am.h>[/color]

          This is an obsolete, prestandard header, and should not be used.
          [color=blue]
          > #include<conio. h>[/color]

          This is completely nonstandard and not necessary for your program.
          [color=blue]
          > #include<math.h >
          >
          > unsigned long factorial ( unsigned long );
          >
          > int main()
          > {
          > char ch;
          > do
          > {
          > clrscr();[/color]

          Don't do that, it's an unnecessary complication.
          [color=blue]
          > result = 1 + x;
          > for ( n = 1; n <= 33 ; n++ )
          > {[/color]

          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.

          [color=blue]
          > result += ( x ^ n ) / factorial(n);[/color]

          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

          Comment

          • Default User

            #6
            Re: little exponent problem

            "Alf P. Steinbach" wrote:
            [color=blue]
            > Btw., this isn't relly a C++ problem, and so is actually off-topic in this
            > group.[/color]


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



            Brian Rodenborn

            Comment

            • Jens Hannemann

              #7
              Re: little exponent problem

              Usman wrote:
              [color=blue]
              > The value ex can be approximated by the sum:
              >
              > 1 + x + x2/2! + x3/3! + . + xn/n![/color]
              [color=blue]
              > HERE IS MY CODE FOR IT : I was unable to store value for a factorial
              > of 100 so I deducted my loop to
              > 34.[/color]

              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.hannemann@iee e.org --- OpenPGP Key Available
              University of Kentucky - Dept. of Electrical and Computer Engineering

              Comment

              • rossum

                #8
                Re: little exponent problem

                On Sat, 7 Feb 2004 00:36:51 +0500, "Usman" <game_pk@hotmai l.com>
                wrote:
                [color=blue]
                > 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<iostr eam.h>[/color]
                Better to use "#include <iostream>", without the ".h". The ".h"
                versions are obsolete and are included for backward compatibility with
                old C code.
                [color=blue]
                >#include<conio .h>[/color]
                conio.h is non standard and probably not required.
                [color=blue]
                >#include<math. h>[/color]
                Use "#include <cmath>". Again this is the new C++ version. All the
                old C standard headers "<xxxx.h>" have become <cxxxx> in C++.
                [color=blue]
                >
                >unsigned long factorial ( unsigned long );
                >
                >int main()
                > {
                > char ch;
                > do
                > {
                > clrscr();[/color]
                Not really required and not portable (important when you get a job
                programming). Better to remove it.
                [color=blue]
                > unsigned long x,n,result=1;[/color]
                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).
                [color=blue]
                > unsigned long exres;
                > cout << " Enter a Number : " ;
                > cin >> x ;
                > cout << endl << endl;
                > result = 1 + x;
                > for ( n = 1; n <= 33 ; n++ )[/color]
                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.
                [color=blue]
                > {
                > result += ( x ^ n ) / factorial(n);[/color]

                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.
                [color=blue]
                > }
                > 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);
                > }
                >[/color]
                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(unsig ned long facto)
                {
                unsigned long retVal = 1;
                for (int i = 2; i <= facto; ++i)
                {
                retVal *= i;
                } // end for
                return retVal;
                } // end factorial()
                [color=blue]
                >
                >[/color]
                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

                Comment

                • Usman

                  #9
                  Re: little exponent problem

                  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


                  Comment

                  Working...