Thread: help writing a function definition

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    help writing a function definition

    I have to write a square root function but I'm stuck on the definition. Can I have an example of a function definition dealing with math functions?

    Here's the program:

    Write a function called SquareRoot that takes in a single parameter (type double) and returns an approximated square root (also a double), using the following calculation process:

    The square root of a number, num can be approximated by repeatedly performing a calculation using the following formula:

    nextGuess = (lastGuess + (num / lastGuess)) / 2

    When nextGuess and lastGuess are almost identical, nextGuess is the approximated square root. You can start the initial guess (i.e. the first value of lastGuess at 1.0, for the first computation. Once the difference between nextGuess and lastGuess is less than a small number (use the value 0.0001), you may claim that nextGuess is the approximated square root of num. (You may not use the square root function in the math library -- the point of this exercise is to write your own function).
    To test this function, write a main() routine that prompts the user and lets them enter a number (of type double), then passes the number into your square root function and prints out the returned value.

    Sample run 1:

    Input Number: 123 //user input
    square root of 123 = 11.0905




    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    
    double squareroot (double, double);
    
    int main ()
    
    {
    	double num;
    	
    
    
    	cout << " Input Number : ";
    	cin >> num;
    
    return 0;
    
    }
    
    double squareroot ( double x, double y)
    
    {
    	double lastGuess;
    	double num;
    
    	double nextguess= (lastGuess + (num / lastGuess)) / 2;
    
        
    }

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Re: help writing a function definition

    Originally posted by jlmac2001
    I have to write a square root function but I'm stuck on the definition. Can I have an example of a function definition dealing with math functions?

    Here's the program:

    Write a function called SquareRoot that takes in a single parameter (type double) and returns an approximated square root (also a double), using the following calculation process:

    The square root of a number, num can be approximated by repeatedly performing a calculation using the following formula:

    nextGuess = (lastGuess + (num / lastGuess)) / 2

    When nextGuess and lastGuess are almost identical, nextGuess is the approximated square root. You can start the initial guess (i.e. the first value of lastGuess at 1.0, for the first computation. Once the difference between nextGuess and lastGuess is less than a small number (use the value 0.0001), you may claim that nextGuess is the approximated square root of num. (You may not use the square root function in the math library -- the point of this exercise is to write your own function).
    To test this function, write a main() routine that prompts the user and lets them enter a number (of type double), then passes the number into your square root function and prints out the returned value.

    Sample run 1:

    Input Number: 123 //user input
    square root of 123 = 11.0905




    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    
    double squareroot (double, double);
    
    int main ()
    
    {
    	double num;
    	
    
    
    	cout << " Input Number : ";
    	cin >> num;
    
    return 0;
    
    }
    
    
    double squareroot ( double x, double y)
    
    {
    	double lastGuess;
    	double num;
    
    	double nextguess= (lastGuess + (num / lastGuess)) / 2;
    
        
    }
    Solution
    Code:
    int main()
    {
        double num;
        cout << "Enter a value " ;
        cin >> num;
        cout << "The square root of : " << num << " is " <<
                       squareroot(num) << endl;
    
         return 0;
    }
    
    double squareroot(double num)
    {
    
          double lastGuess = 1.0;
          double nextguess = (lastGuess + (num / lastGuess))/2.0;
          
          while(!( (nextguess <= (lastGuess + 0.0001)) && (nextGuess >= (lastGuess - 0.00001))) )
          {
             nextguess = (lastGuess + (num / lastGuess))/2.0;
           }
    
            return nextguess;
    }
    Hey. I have not had time to read your problem completely.. and I am quite buzy in here.. doing my own work.... that solution that I have coded must be close to what you are looking for... not sure whether it will be a bug free solution.... all the best...
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM