square root bisection?? help

This is a discussion on square root bisection?? help within the C++ Programming forums, part of the General Programming Boards category; i have this asignment due soon but i can only get the absolute value part right i have no clue ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    square root bisection?? help

    i have this asignment due soon but i can only get the absolute value part right i have no clue how to get the square root with bisection please help. here is the assignment and what i have done.


    Requirements:
    − You can only use the iostream library. No other libraries are allowed.
    − You must format your code according to the programming standards used in this course. If
    the programming standards are not followed, some marks will be deducted even if the code
    is working correctly.
    − You must submit your source code (lab4.cpp) via webct.
    Lab 4
    − Write a C++ function called myAbs that accepts a number (double) and returns the absolute
    value of the given number.
    − Write a C++ function called mySqrt that accepts a positive number (double) and returns the
    square root of the given number. This function must implement the bisection algorithm for
    computing square roots (discussed in class). The iteration must stop when the error is less
    than or equal to 0.00001. This function then returns the last approximation of the square
    root. If the mySqrt function is given an invalid argument (e.g., a negative number), return
    0xffffffff. This is a convention that indicates the result is not a number.
    − Put both myAbs and mySqrt into lab4.cpp and use the following main function to test your
    functions. Add required comments (e.g., name, student number, etc.) and other C++
    statements (e.g., #include, function declarations, etc.) to make your code work correctly.
    Code:
    int main()
    {
    cout << "Please enter a positive number: ";
    double number;
    cin >> number;
    cout << "mySqrt(" << number << "): " << mySqrt(number) << endl;
    double num = 5.5;
    cout << "myAbs(" << num << "): " << myAbs(num) << endl;
    cout << "myAbs(" << -num << "): " << myAbs(-num) << endl;
    return 0;
    }



    This is what i have so far


    Code:
    #include <iostream>
    using namespace std;
    
    double myAbs(double num);
    double mySqrt(double num);
    int main()
    {
    cout << "Please enter a positive number: ";
    double num;
    cin >> num;
    double number = num;
    cout << "mySqrt(" << number << "): " << mySqrt(number) << endl;
    cout << "myAbs(" << num << "): " << myAbs(num) << endl;
    cout << "myAbs(" << -num << "): " << myAbs(-num) << endl;
    return 0;
    }
    
    double myAbs(double num)
    {	
    	if(num > 0)
    	{
    		num = num;
    	}
    	if(num<0)
    	{	num = -num;
    	}
    return (num);
    }
    
    double mySqrt(double number)
    {	
    double x = 1;
    double y = x;
    
    do
    	x=.5*(x-(number/x));
    while (-.00001 < x - y < .00001);
    
    return number;
    }

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Google this term : "root with bisection". You'll find out what you obviously missed in class!

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Don't need quite so complex code here:
    Code:
    double myAbs(double num)
    {	
    	if(num < 0)
    	{
    		return -num;
    	}
            return num;
    }
    Or if you want to be REALLY clever:
    Code:
    double myAbs(double num)
    {	
        return (num < 0)?-num:num;
    }

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    2
    thanks the the replies guys, matsp that is cool i never thougth of that, but like i said i already had that handled, maybe i could get some bonus or something tho haha. and kcpilot, its not that i dont know the bisection method i just cant get my head around how to put that into C++ language, im clueless me and three buddies in my class worked on it for a few hours and we got no where. im just trying to take advantage of all the resources available to me it would be great if someone could give me a real good hand, i want to understand whats done after its done. thanks tho guys

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, give yourself a try to do it, then post your code.

    Your current code returns the input number, so it's fairly obviously not the right result [unless the input is "1"]. And to avoid problems, you may want to do "abs" on the input first - or check for negative numbers and refuse to do them some way.

    Edit: And this is "broken" too.
    -.00001 < x - y < .00001

    The reason is that this is the actual comparison you do:
    (-.00001 < (x - y)) < .00001

    Since any compariso is always going to be either 0 or 1, it's probably not what you actually want.


    You probably want to do "myAbs(x-y) < 0.00001" or some such.

    --
    Mats
    Last edited by matsp; 10-19-2007 at 03:00 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 12:53 PM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Square Root ?!?
    By Tm687 in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 03:38 PM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 01:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21