I need help getting started writing a program to determine the square root of number using the iteration formula. Printing out the first consecutive pair which are within 0.001 of each other.
Any help would be greatly appreciated.
Thanks.
This is a discussion on Square root within the C Programming forums, part of the General Programming Boards category; I need help getting started writing a program to determine the square root of number using the iteration formula. Printing ...
I need help getting started writing a program to determine the square root of number using the iteration formula. Printing out the first consecutive pair which are within 0.001 of each other.
Any help would be greatly appreciated.
Thanks.
There. Consider your homework done. I'm feeling incredibly generous today.Code:for ( d = 0.0000; d < totest; d += 0.0001 ) { if( d*d == totest ) printf("%f is the square root of %f", d, totest ); }
Quzah.
Hope is the first step on the road to disappointment.
read the first 3 chapters in your C programming text book first..
that will explain something about the basic format
when i was a kid, they taught us to compute square roots using division... i think it was consecutive dividing by the largest square... something like that....
check out the following explanation, and try to code it.
the { is the square root symbol
4. 3 5 8
Example: {19.00000000000
4^2 = 16
-----
3 00
83 x 3 = 2 49
------
5100
865 x 5 = 4325
------
77500
8708 x 8 = 69664
Step 1:
If the number of digits to the left of the decimal point is odd, then
the first digit of the answer will be the largest number whose square
is less than the first digit of the original number. If the number of
digits to the left of the decimal point is even, then the first digit of
the answer will be the largest number whose square is less than the
first _two_ digits of the dividend. In this case, 19 is a 2-digit
number, so the first digit of the quotient is the largest number
whose square is less than 19, i.e. 4. Write that number above
either the first digit of the dividend (if odd) or the second digit of
the dividend (if even)
Step 2:
Subtract the square of the number in Step 1, then bring down two
more digits.
Step 3:
Double the current quotient (on top, ignoring any decimal point), and
put a blank space in front of it. Here 4 x 2 = 8. Put down 8_ x _ to
the left of the current remainder, in this case 300.
Step 4:
The next digit of the quotient will be the largest number that can
be put in both blanks so that the resulting multiplication problem
is less than or equal to the current remainder. In this case, the
number is 3, because 83 x 3 = 249, whereas 84 x 4 = 336 is too
high. Write this number above the second digit of the next two
numbers; in this case the 3 would go above the second 0. We now
have a quotient of 4.3.
Step 5:
As long as you want more digits, subtract the product from the
remainder (i.e. 300 - 249 = 51) and bring down the next two digits,
in this case 51 turns into 5100, which becomes the current
remainder. Now repeat steps 3 and 4.
If you ever get a remainder of zero, you've got a perfect square
on your hands. I hope this makes sense to you.
Thanks for the input, I understood the test for finding 0.001 but was hung up on the process of how to loop the test.
Again, thanks.
Ever take a course in algorithms or advanced data structures where they discuss the concept of "Big O"
The solution you provided would surely get a big "D" everywhere I went to school... a "D" is being generous.
That solution was like creating a busy loop for a timer.
Try it again.
I provided exactly what he asked (no, I didn't create a break point for him, but then again, I tend not to do everything for them. Hell, I practicly gave him his entire homework program.), nothing more.Originally posted by David21078
Ever take a course in algorithms or advanced data structures where they discuss the concept of "Big O"
The solution you provided would surely get a big "D" everywhere I went to school... a "D" is being generous.
That solution was like creating a busy loop for a timer.
Try it again.
He asked how to use a loop, by .001 increments, to test for the square root. Yeah, I had one more digit of precision in there.
Again, it's not my job to do everything for them. He wanted a loop. He got a loop.
Vola. Loop that tests for square root within .001 increments of eachother.need help getting started writing a program to determine the square root of number using the iteration formula. Printing out the first consecutive pair which are within 0.001 of each other.
He didn't ask: "What is the most efficient possible way to test for square root?"
He asked how to find the square root to the .001 precision "the iteration forumlae". Iteration = incrementation. Since the precision required was .001, the simplest method is what I've shown above.
One thing you'll learn when asking me questions: be precise. I'll freely interpret your words as I see fit, so you'd better be damn sure you say exactly the problem, exactly the need, exactly what's wrong.
Quzah.
Hope is the first step on the road to disappointment.
busting your chops...
Really though,
I think his instructor would really appreciate a more elegant solution; i.e. an algorithm that makes less iterations.
You know like the old manual method of using division by hand when you were a kid in elementary school.
Assuming his instructor actually even realizes that 'void main' is wrong...Originally posted by David21078
busting your chops...
Really though,
I think his instructor would really appreciate a more elegant solution; i.e. an algorithm that makes less iterations.
As for classes and instructions, my C is self taught. I've never taken classes on it. I honestly doubt they'd be usefu now. I'm sure there are some classes on advanced theory or algorithms that would be handy, but I don't need them for what I do (unless someone actually decides to pay me to program).
Again, he didn't know how to do a for loop. Reread that. Then ask me why I gave him the simplest implementation.
Quzah.
Hope is the first step on the road to disappointment.
You seem to be interested in this stuff...why not pursue a formal program of instruction...start by taking C for credit..they will teach you some basic algorithm stuff...then C++ where you will get more...and even go into some Object Oriented programming basics... ...
I am finishing my Master's...in CS, but I am by no means an "expert".. I am also a Software Engineer...and program now in C mostly... but will be doing some C++ stuff really soon... before I did some stuff in Ada..
Ada is a really good language.