# Thread: square root bisection?? help

1. ## 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. Google this term : "root with bisection". You'll find out what you obviously missed in class!

3. 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

4. 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. 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