Page 1 of 1

My first C++ programs! ^-^

PostPosted: Tue Jan 24, 2006 3:21 am
by Slater
It's like my Java thread... only for C++.

http://www.savefile.com/files/3689115
Here is my very first (nonretarded... you know, the typical "lol this is my first prog written in <insert language here> !!!1112one!1!!" program. We all do them, and they don't count.) C++ program. It's an interesting little "game" involving ancient ways of trying to find the square root of numbers, and apparently this method works pretty well. The program has a small random number generator in it, so your results may differ by a little bit if you do the same search twice. If you search on uber or 1337 accuracy mode, chances are that the program will come up with an answer that is so close to the actual square root that it is unable to calculate for a difference between the two.

Oh, and "pretty accurate" isn't very accurate for large numbers. (speaking of which, you may enter any number within the long double range) The descriptions of accuracy really vary on the number being used.

Edit: Uploaded better version. I decided that the first one was way too inaccurate for any fun.

PostPosted: Tue Jan 24, 2006 1:33 pm
by Icarus
Cool. What is the algorithm used? (I'm a math geek, so I find it interesting.

PostPosted: Tue Jan 24, 2006 10:34 pm
by Slater
Here's how it works. First, you take the number you want to sqrrt and you guess at what the answer is. Then, you divide the first number by your guess and record that number (call it r) . After that, set your new guess to be your old guess + r and divide it by 2. Repeat steps 2 through 4 many times to get more accurate.

Here is a bit of code that summarizes it pretty darn well.
Code: Select all
        guess = (number/2) + (rand() % 15);
        for(accuracy; accuracy > 0; accuracy--)
        {
            r = number/guess;
            guess = (guess + r)/2;
        }
       

PostPosted: Wed Jan 25, 2006 1:42 am
by Warrior4Christ
I've found 2 is quite inaccurate, but 3 is exceptionally accurate. There's quite a gap between these two.

I remember doing a simple square root finding algorithm from first year, but I can't remember if it was the same one or a different one...

PostPosted: Wed Jan 25, 2006 3:34 am
by Slater
The gap grows exponentially the bigger the number you're solving for gets. That's why the descriptions are deciving, kinda.

Oh yeah, and negative numbers do bad things. gotta figure that exception handling out for C++...

PostPosted: Wed Jan 25, 2006 9:39 pm
by Icarus
Dude, negative numbers always do bad things in sqare roots. At least in the reals.

You might try putting the code for the algorithm in an if statement. Or, you could use the number you're getting the sqrt of to control the loop, having it exit for anything less than 0.

PostPosted: Tue Feb 14, 2006 1:30 pm
by Slater
http://www.savefile.com/files.php?fid=2621766

My first homework assignment. Please try to break it. And tell me if you do.