CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Help with understanding what his algorithm does and how it comes up with the numbers

    Just starting to lean about algorithm and programming and professor gave us this question and told us to figure out what i does thanks for the help
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main( )
    {
      double a, x_old, x_new;
      cout << "Enter a positive DECIMAL number => ";
      cin >> a;
      x_old = 1;
      x_new = 0.5*( 1 + a );
      while( fabs(x_old - x_new) >= 0.00005)
      {
        x_old = x_new;
        x_new = 0.5*( x_old * x_old + a )/x_old;
      }
      cout << "\nThe value that you are seeking is " << x_new << endl;
      cout << "Press the enter key to continue ...";
      cin.get();
      return 0;
    }
    Last edited by cilu; October 2nd, 2012 at 04:36 AM. Reason: code tags

  2. #2
    Join Date
    Apr 1999
    Posts
    26,738

    Re: Help with understanding what his algorithm does and how it comes up with the numb

    Quote Originally Posted by soldatik21 View Post
    Just starting to lean about algorithm and programming and professor gave us this question and told us to figure out what i does thanks for the help
    OK, so what's your question?

    Regards,

    Paul McKenzie

  3. #3
    GCDEF is offline Elite Member Power Poster GCDEF has a reputation beyond repute (3000+) GCDEF has a reputation beyond repute (3000+) GCDEF has a reputation beyond repute (3000+) GCDEF has a reputation beyond repute (3000+) GCDEF has a reputation beyond repute (3000+) GCDEF has a reputation beyond repute (3000+) GCDEF has a reputation beyond repute (3000+) GCDEF has a reputation beyond repute (3000+) GCDEF has a reputation beyond repute (3000+) GCDEF has a reputation beyond repute (3000+) GCDEF has a reputation beyond repute (3000+)
    Join Date
    Nov 2003
    Posts
    11,568

    Re: Help with understanding what his algorithm does and how it comes up with the numb

    Quote Originally Posted by soldatik21 View Post
    Just starting to lean about algorithm and programming and professor gave us this question and told us to figure out what i does thanks for the help
    Then you should probably do what your professor asked you to do. This isn't the do my homework for me forum.

  4. #4
    cilu's Avatar
    cilu is offline Moderator/Reviewer/MS MVP Power Poster cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+)
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,349

    Re: Help with understanding what his algorithm does and how it comes up with the numb

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Jan 2009
    Posts
    581

    Re: Help with understanding what his algorithm does and how it comes up with the numb

    As a first step, ignore the actual code and just try running it. See what output you get for various input values. Then, try to guess what function the code is calculating (it should be fairly obvious).
    Once you have an idea as to what function this code calculates, see if you can work out how it is doing it.

+ Reply to Thread

Bookmarks

Posting Permissions

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



HTML5 Development Center

Click Here to Expand Forum to Full Width