subject

Given a double variable named x that has been declared and given a value, let's use a binary search technique to assign an estimate of its square root to another double variable, root that has also been declared. let's assume that x's value is greater than 1.0 -- that will simplify things a bit. here's the general idea:

since x> 1, we know its square root must be between 1 and x itself. so declare two other variables of type double (a and b say) and initialize them to 1 and x respectively. so we know the square root must be between a and b. our strategy is to change a and b and make them closer and closer to each other but alway make sure that the root we're looking for is between them. (such a condition that must always hold is called an invariant.)

to do this we will have a loop that at each step finds the midpoint of a and b. it then squares this midpoint value and if the square of the midpoint is less than x we know that the root of x must be bigger than this midpoint: so we assign the midpoint to a (making a bigger and shrinking our a and b interval by and we still can be sure that the root is between a and b. of course if the midpoint's square is greater than x we do the opposite: we assign b the value of midpoint.

but when to stop the loop? in this exercise, just stop when the interval between a and b is less than 0.1 and assign root the midpoint of a and b then.

we call this a binary search also because at each stage we cut the interval under consideration in half. efficient as this method is, old isaac newton discovered an algorithm that is even more efficient and that's what the library function sqrt uses.

i have this:

double a=1, b=x;
double mid;

while(a-b> 0.1){
mid=(a+b)/2;
mid=mid*mid;
if (mid a=mid;
else if(mid> x)
b=mid;
else
root=mid;
}

!

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 24.06.2019 02:30
Write the pseudo code for this problem based on what you learned from the video. the purpose is to design a modular program that asks the user to enter a distance in kilometers, and then converts that distance to miles. the conversion formula is as follows: miles = kilometers x 0.6214
Answers: 3
question
Computers and Technology, 24.06.2019 14:00
When creating a field in a table, you must set the to determine what type of data the field can store. field property data type field type data property
Answers: 1
question
Computers and Technology, 25.06.2019 12:00
Which describes which ctso each student should join
Answers: 1
question
Computers and Technology, 25.06.2019 12:10
Create a function called quadform( ) that takes as input the coefficients of the quadratic equation (a,b,c) and returns the two distinct real roots (x1,x2) as output, if they exist. in addition, the function returns a message (flag) that informs the user if an error occurred when trying to find two distinct real roots. the possible errors are: (1) "only one root is found"; (2) "imaginary roots are found"; (3) "any value of x is a solution"; (4) "no zeroes exist." if no errors occur, then flag should be "no errors".
Answers: 1
You know the right answer?
Given a double variable named x that has been declared and given a value, let's use a binary search...
Questions
Questions on the website: 13722359