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, 22.06.2019 13:30
Jane’s team is using the v-shaped model for their project. during the high-level design phase of the project, testers perform integration testing. what is the purpose of an integration test plan in the v-model of development? a. checks if the team has gathered all the requirements b. checks how the product interacts with external systems c. checks the flow of data in internal modules d. checks how the product works from the client side
Answers: 1
question
Computers and Technology, 23.06.2019 14:00
Select the correct answer. andre was recently hired by an organization to check for system vulnerabilities. he is supposed to exploit these vulnerabilities and create a report on the extent of damage to which the system was susceptible. what position does andre hold in this organization? a. information security analyst b. information assurance manager c. penetration tester d. network security engineer e. chief information security officer
Answers: 2
question
Computers and Technology, 23.06.2019 22:20
Read “suburban homes construction project” at the end of chapters 8 and 9 (in the textbook) and then develop a wbs (work breakdown structure) in microsoft excel or in microsoft word (using tables)
Answers: 1
question
Computers and Technology, 24.06.2019 10:20
Write a program that keeps asking the user for new values to be added to a list until the user enters 'exit' ('exit' should not be added to the list). these values entered by the user are added to a list we call 'initial_list'. then write a function that takes this initial_list as input and returns another list with 3 copies of every value in the initial_list. finally, inside print out all of the values in the new list. for example: input: enter value to be added to list: a enter value to be added to list: b enter value to be added to list: c enter value to be added to list: exit output: a b c a b c a b c note how 'exit' is not added to the list. also, your program needs to be able to handle any variation of 'exit' such as 'exit', 'exit' etc. and treat them all as 'exit'.
Answers: 2
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
question
Mathematics, 19.11.2020 21:20
question
Mathematics, 19.11.2020 21:20
question
Mathematics, 19.11.2020 21:20
question
Mathematics, 19.11.2020 21:20
question
English, 19.11.2020 21:20
question
Chemistry, 19.11.2020 21:20
Questions on the website: 13722360