getting wrong result with my program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: getting wrong result with my program

  1. #1
    Join Date
    Oct 2010
    Posts
    15

    getting wrong result with my program

    program will approximate the value of e...when x=5.7.. but i am not getting the result right . could anybody help me and show my mistake please...
    x=1+1/1!+1/2!+1/3!+1/4!+1/5!+.......
    when the difference between two successive approximations is less tan 10e-9

    Code:
    int main()
    {
    double x,myexp1,myexp2,expx,fact,i;
    fact=1;
    ofstream output("e:result12.dat");
    cout<<" ....."<<endl;
    output<<" ..... "<<endl;
    x=5.7;
    myexp1=1;
    myexp2=x;
    i=2;
    expx=myexp1+myexp2;
    while (fabs(expx-myexp1 >= 10e-9))
           {
           fact=fact*1;
           myexp1=expx;
           expx=expx+pow(x,i)/fact;
           i=i+1;
           cout<< "..... " <<"i= "<<expx<<endl;
           output<< ".... " <<"i= "<<expx<<endl;
           }
    
    
          system("PAUSE");
          return 0;
    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,646

    Re: getting wrong result with my program

    Your factorial is incorrect :

    Code:
    fact = fact * 1;   // should be fact*i

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: getting wrong result with my program

    Quote Originally Posted by buldogao87 View Post
    Code:
    int main()
    {
    double x,myexp1,myexp2,expx,fact,i;
    fact=1;
    ofstream output("e:result12.dat");
    cout<<" ....."<<endl;
    output<<" ..... "<<endl;
    x=5.7;
    myexp1=1;
    myexp2=x;
    i=2;
    expx=myexp1+myexp2;
    while (fabs(expx-myexp1 >= 10e-9))
           {
           fact=fact*1;
    
           myexp1=expx;
           expx=expx+pow(x,i)/fact;
           i=i+1;
           cout<< "..... " <<"i= "<<expx<<endl;
           output<< ".... " <<"i= "<<expx<<endl;
           }
    
    
          system("PAUSE");
          return 0;
    }
    The red line doesn't make any sense!
    Did you mean
    Code:
           fact=fact * i;
    // Edit: Wow! Philip was faster!
    Victor Nijegorodov

  4. #4
    Join Date
    Oct 2010
    Posts
    15

    Re: getting wrong result with my program

    yes ... Thanks so much...

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    Re: getting wrong result with my program

    This is also incorrect:

    Code:
    while (fabs(expx-myexp1 >= 10e-9))
    It should be:

    Code:
    while (fabs(expx-myexp1) >= 10e-9)
    Without that change I get an error C2668. BTW: If the compiler would tolerate the wrong statement, it would even work, since e is approached from below...

    But I had to make some more changes to your program to actually compile and correctly calculate the (approximate) value of e. And only that way it matches the approximation formula from post #1. Here is the corrected version, with the changes marked in red:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <math.h>
    
    using namespace std;
    
    int main()
    {
    double /* x, */myexp1,myexp2,expx,fact,i;
    fact=1;
    ofstream output("e:result12.dat");
    cout<<" ....."<<endl;
    output<<" ..... "<<endl;
    // x=5.7;  // That's not needed anymore
    myexp1=1;
    myexp2=1;
    i=2;
    expx=myexp1+myexp2;
    cout << setprecision(12);  // In order to see all changes up to the last iteration
    output << setprecision(12);  // ditto
    while (fabs(expx-myexp1) >= 10e-9)
           {
           fact=fact*i;
           myexp1=expx;
           expx=expx+1.0/fact;
           i=i+1;
           cout<< "..... " <<"i= "<<expx<<endl;
           output<< ".... " <<"i= "<<expx<<endl;
           }
    
    
          system("PAUSE");
          return 0;
    }
    I hope I didn't miss any change.

    ... And I hope this wasn't homework I now have done for you...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center