Question

Given a double variable named x that has been declared and given a value, let's use a binary search technique to assign...

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 half!)-- 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.00001 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.00001){
mid=(a+b)/2;
mid=mid*mid;
if (mid<x)
a=mid;
else if(mid>x)
b=mid;
else
root=mid;
}

Help please!!!
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include
#include
const double PRECISION = 0.00001;
using namespace std;
int main(){
double x;
cout << "Enter a number :";
cin >> x;
double a = 1;
double b,mid;
b=x;
while ((b-a)>PRECISION){
mid = (a+b)/2;
if (mid*mid < x)
a = mid;
else b=mid;
}
cout <<"The square root of the number is : "<
system ("pause");
return 0;
}
Answer #2

#include

#include

using namespace std;

int main()

{

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

cout<<"Enter the number whose square root is to be found : ";

cin >> x;

if (x < 1)

{

cout << "Entered number should be greater than 1"<

return -1;

}

b = x;

while (((b - a) > 0.00001))

{

c = (a + b) / 2;

if ((c*c) < x)

{

a = c;

}

else

{

b = c;

}

}

cout << "Square root of " << x << " is " << c << "." << endl;

return 0;

}

Answer #3
DELETED
Know the answer?
Add Answer to:
Given a double variable named x that has been declared and given a value, let's use a binary search technique to assign...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The equation f(x) = (1 ‐ x) cos x ‐ sin x = 0 has at...

    The equation f(x) = (1 ‐ x) cos x ‐ sin x = 0 has at least one root between a = 0 and b = 1 since f(a)f(b) < 0. The bisection method of finding the root proceeds as follows: a. It finds the midpoint r = (a + b)/2. b. If f(r) = 0, then r is the root. If |b ‐ a| is very small less than ∈ then also we can take r as the root....

  • QUESTION 1 Given two double variables named x and y, which of the following statements could...

    QUESTION 1 Given two double variables named x and y, which of the following statements could you use to initialize both variables to a value of 0.0? a. x | y = 0.0; b. x = y = 0.0; c. x, y = 0.0; d. none of the above 1 points    QUESTION 2 When you use a range-based for loop with a vector, you a. can avoid out of bounds access b. must still use a counter variable c....

  • Designing functions Reminder: When designing functions, follow the given steps to reinforce good software development p...

    Designing functions Reminder: When designing functions, follow the given steps to reinforce good software development practices and problem solving strategies: a) Start by writing the documentation for the function. Use docstrings shown in lecture and lab. b) Write test calls to the function in your main function for the different cases of input the function will encounter. This will help you understand the behavior of the function and later behave as tests for your function. c) Define the function d)...

  • please use python and provide run result, thank you! click on pic to make it bigger...

    please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...

  • check my answers for Networking I came up with these answers, can check my answers Question...

    check my answers for Networking I came up with these answers, can check my answers Question 1: General What data rate is needed to transmit an uncompressed 4" x 6" photograph every second with a resolution of 1200 dots per inch and 24 bits per dot (pixel)? 691,200 kb/s 28.8 kb/s 8.29 Mb/s 829 Mb/s Question 2: Layering "Layering" is commonly used in computer networks because (check all that apply): -It forces all network software to be written in ‘C’....

  • I have this case study to solve. i want to ask which type of case study...

    I have this case study to solve. i want to ask which type of case study in this like problem, evaluation or decision? if its decision then what are the criterias and all? Stardust Petroleum Sendirian Berhad: how to inculcate the pro-active safety culture? Farzana Quoquab, Nomahaza Mahadi, Taram Satiraksa Wan Abdullah and Jihad Mohammad Coming together is a beginning; keeping together is progress; working together is success. - Henry Ford The beginning Stardust was established in 2013 as a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT