Calculating Square Root Using Expression

http://prntscr.com/dwer81 Could someone guide me on how to write this program? I'm not even remotely sure on what to do. Note: I'm limited in what I can use. I can't use strings. I'm stuck using if else, while, for, and switch.
Last edited on
Do you understand the method?

It is saying, start with an estimated value.

Then calculate a better estimate based on the current estimate.
repeat.

It also says, stop the process when the difference between the current and previous estimate is less than some specified value (named epsilon).



I'd express it like this. Example , find square root of 4.

Original number = y = 4
start estimate = x = y
result: 4

next estimate
x = 0.5*(x + y/x)
result: 2.5

next estimate
x = 0.5*(x + y/x)
result: 2.05

... and so on.



Last edited on
Ugh, okay. I get the gist of it, but I'm still having trouble figuring out how I would set it up. Like let's say for example, the process loops until the current estimate - the previous estimate < .05, how would I be able to make it so it'll take current value and the previous one and subtract it like that?
Last edited on
how would I be able to make it so it'll take current value and the previous one and subtract it like that?

Just before calculating the next value, store the current one in another temporary variable. After getting the next value, subtract it from the saved value.

Also, you need to use the absolute (unsigned) value of the resulting difference. You could use the std::abs() function. Or, since you may be expected to write everything yourself, you could test the value, and if it is less than zero, use diff = -diff; to flip the sign.
Topic archived. No new replies allowed.