My first C++ programs! ^-^

The geek forum. PHP, Perl, HTML, hardware questions etc.. it's all in here. Got a techie question? We'll sort you out. Ask your questions or post a link to your own site here!

My first C++ programs! ^-^

Postby Slater » Tue Jan 24, 2006 3:21 am

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.
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia

Postby Icarus » Tue Jan 24, 2006 1:33 pm

Cool. What is the algorithm used? (I'm a math geek, so I find it interesting.
The Forsworn War of 34

††
User avatar
Icarus
 
Posts: 1477
Joined: Sun Nov 09, 2003 5:00 am
Location: 34

Postby Slater » Tue Jan 24, 2006 10:34 pm

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;
        }
       
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia

Postby Warrior4Christ » Wed Jan 25, 2006 1:42 am

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...
Everywhere like such as, and MOES.

"Expect great things from God; attempt great things for God." - William Carey
User avatar
Warrior4Christ
 
Posts: 2045
Joined: Sat Aug 20, 2005 8:10 pm
Location: Carefully place an additional prawn on the barbecue

Postby Slater » Wed Jan 25, 2006 3:34 am

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++...
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia

Postby Icarus » Wed Jan 25, 2006 9:39 pm

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.
The Forsworn War of 34

††
User avatar
Icarus
 
Posts: 1477
Joined: Sun Nov 09, 2003 5:00 am
Location: 34

Postby Slater » Tue Feb 14, 2006 1:30 pm

http://www.savefile.com/files.php?fid=2621766

My first homework assignment. Please try to break it. And tell me if you do.
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia


Return to Computing and Links

Who is online

Users browsing this forum: No registered users and 68 guests