Web Analytics Made Easy -
StatCounter Can someone help me please with this c ++ problem!! - CodingForum

Announcement

Collapse
No announcement yet.

Can someone help me please with this c ++ problem!!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can someone help me please with this c ++ problem!!

    HI EVERYONE IM A NEW MEM AND A BEGINNER IN C++ PROGRAM , HOPE U GUYS CAN HELP ME WITH THIS PROBLEM :



    the square root of a number N can be approximated by repeated calculation using the formula : NG =( LG + N/LG)
    where ng stands for next guess and lg stands for last guess. Write a function that implements this computation. The first parameter will be a positive real number, the second will be an intial guess of the square root f that number, and the third will be the computed result.The intial guess will be the starting value of lg. The function will compute a value for ng using the formula above. To control the computation, we can use a whie loop. Each time through the loop, the dif between ng and lg is checked to see whether these two guesses are almost identical . If so, the function returns ng as the square root; otherwise, the guess becomes the last guess and the process is repeated . The loop should be repeated until the magnitude of the dif between lg and ng is less than 0.005.....

    my code is attached below , i keep getting : error C3861: 'fabs': identifier not found
    someone please help !



    #include <iostream>
    #include <iomanip>
    using namespace std ;


    void getValues(float&, float& ); //GET VALUES FROM USER FOR N AND LAST-GUESS
    void newGuess(float , float , float& ); //CALCULATE FIRST NEXT-GUESS



    int main()
    {
    float n = 0.0;
    float lg = 0.0;
    float ng = 0.0;
    int count = 0;


    getValues( n, lg); //GET VALUES FROM USER FOR N AND LAST-GUESS
    newGuess( n, lg, ng); //CALCULATE FIRST NEXT-GUESS

    count++; //COUNT GUESS
    cout << "The " << count << " guess of the square root is " << ng << endl;
    while(fabs(lg-ng)>=0.005) //IF LAST-GUESS AND NEXT-GUESS NOT CLOSE ENOUGH
    {
    lg = ng; //NEXT GUESS BECOMES LAST GUESS
    newGuess(n, lg, &ng); //AND PROCESS IS REPEATED
    count++;
    cout << "The " << count << " guess of the square root is " << ng << endl;
    }
    cout << endl;
    return 0;
    }

    void getValues(float& n,float& lg);
    {


    do{
    cout<< " PLEASE ENTER THE VALUE OF N"<< ; endl
    cin>> n
    cout<< " PLEASE ENTER THE VALUE OF LG"<<; endl
    cin>> lg>> endl;


    }while (lg >= n/2 && n < 0 || lg<0 );



    getValues( n, lg); }










    void newGuess (float n, float lg, float& ng )
    {

    ng = (lg/2) + ( n/2lg);

    }

  • #2
    You need to include the library where the fabs() routine is located. In this case it's in the math library. So include that.

    #include <math.h>

    And in the future please wrap your code in code tags so the forum will format the code so it is easier to read.
    http://www.codingforum.net/misc.php?do=bbcode#code
    Last edited by Spookster; Apr 10, 2009, 01:53 PM.
    Spookster
    CodingForum Supreme Overlord
    All Hail Spookster

    Comment

    Working...
    X