Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,374 Programmers. There are 1,417 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

squareroot function in basic

 
Reply to this topicStart new topic

squareroot function in basic, making it work

tootintorrey
post 11 Apr, 2008 - 10:12 AM
Post #1


New D.I.C Head

*
Joined: 24 Mar, 2008
Posts: 13



well my school has proved to be even more retarded then ever. my assignment is to make a function that calculates the squareroot of a number. yes yes, sqr works perfectly fine but somehow actually making the function is what we need to know blink.gif

anyways this is what i have so far and it comes pretty close on certain numbers but way off on others. im using these to help

http://mathforum.org/dr.math/faq/faq.sqrt.by.hand.html
and to check the sqr(number)

http://www.math.com/students/calculators/s...square-root.htm


a little more info, its to find sqr(number >=100 and <= 1000) and i used the if statements to generate a guess close to what the square should be although technically any random guess would work but then that would make it nearly impossible to follow and write out on paper since there would be two random numbers being generated lol

From what my understanding is of the mathforum.org post is that i need to do another loop of averaging and division but i dont know how to add that into the code since it would be using the current value of squareroot.

CODE


CLS

RANDOMIZE TIMER

number = INT(RND * (1000 - 100 + 1) + 100)

IF number > 100 < 200 THEN
        x = 11
ELSEIF number >= 200 < 400 THEN
        x = 18
ELSEIF number >= 400 < 600 THEN
        x = 21
ELSEIF number >= 600 < 800 THEN
        x = 26
ELSEIF number >= 800 THEN
        x = 28
END IF

square = squareroot(number, x)

PRINT number
PRINT square



FUNCTION squareroot (number, x)

squareroot = ((number / ((((number / x) + x) / 2))))

END FUNCTION


User is offlineProfile CardPM
Go to the top of the page
+Quote Post


Martyr2
post 11 Apr, 2008 - 11:25 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 3,561

Well it is good that your school is teaching you these things because in the industry you sometimes have to rebuild some basic functions from scratch because you will have to add some sort of business twist.

Now the way you have setup the squareroot function is going to be a little more troublesome because you have done (according to mathforum.org) steps 2, 3 and 2 again. Your function should just do steps 2 and 3. Right now as you got it the result of your first squareroot iteration for 12 and a guess of 2 would return 3 which is not right. The answer is 4 (the result of step 3 not the result of step 2).

So if you do just steps 2 and 3 in the function your loop will look like this (in pseudocode)

CODE

Collect number and guess from user
Start for loop from 1 to number of iterations (n)
     Call squareroot function and pass in number and guess
     Store result as the square root
     Set new guess to the calculated square root
Loop


So hopefully this makes sense. Your function should only be doing steps 2 and 3 to get the square root. Use that answer for additional calls to squareroot for as many times as you wanted.

Enjoy!

"At DIC we be squared code ninjas because everyone else is the square root of us!" decap.gif

This post has been edited by Martyr2: 11 Apr, 2008 - 11:25 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Eighty
post 18 Apr, 2008 - 07:10 PM
Post #3


New D.I.C Head

*
Joined: 16 Apr, 2008
Posts: 19

Newton's Method will always rock
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

tootintorrey
post 22 Apr, 2008 - 02:13 PM
Post #4


New D.I.C Head

*
Joined: 24 Mar, 2008
Posts: 13

lol thanks

so many people couldnt figure this out that our professor ended up pretty much giving us the answer.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 02:48AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month