Thread: C++problem to solve

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    10

    Question C++problem to solve

    ok im trying to write a c++ program that will determine the square root of 138 without using a calculator or the math library. any suggestions

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56
    Here, I found this searching my old codes... I wrote it when I was still learning C and C++... Yes, I keep my old codes and the codes I find interesting today, just to compare to the code of tomorrow.

    I remember that I wrote it using a square root algorithm I found on the web... But I dont remember very well how it works... Anyway, here it is.. Hope it helps in some way:

    Code:
    #include <stdio.h>
    
    int getroot(int value)
    {
            int count = 0;
            for(count = 0; (count * count) != value &&
                    (count * count) <= value; count++)
                    continue;
            if((count * count) == value)
                    return(count);
            else
                    return(0);
    }
    double fullroot(double value)
    {
            double count;
            double reval;
            for(count = 0; (count * count) != value &&
                    (count * count) <= value; count++)
                    continue;
            if((count * count) == value)
                    return(count);
            else
            {
                    reval = (count - 1) + 0.1;
                    for(; (reval * reval) < value && reval != (reval + 0.999998);
                            reval = reval + 0.000001)
                            continue;
                    return((reval - 0.000001));
            }
    }
    
    
    int main(int argc, char **argv)
    {
        printf("Square root of 16:%d\n", getroot(16));
        printf("Square root of 10:%f", fullroot(10));
        getchar();
        return(0);
    }

  4. #4
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    @Dante: you know, I understand you meant well. but you shouldn't help those who violated the Homework Policy with a fully working code. Don't give them a fish but teach them how to fish instead. Give them a fishing pole, a net, a harpoon, a poison-for-fishing, a tesla-coil-to-electrocute-those-fishes, etc. If you know what I mean.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Do not use Dante's code. It is very poor.
    A propper solution such as the babylonian square root is far more efficient, more accurate, easier to understand, and much less code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    10
    Thanks for the help @ dante and i understand what you're saying @ g4j31a5, all i wanted was a little direction not a full layout but i know @ dante was just trying to help

  7. #7
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by iMalc View Post
    Do not use Dante's code. It is very poor.
    A propper solution such as the babylonian square root is far more efficient, more accurate, easier to understand, and much less code.
    Or quake's fast square root.

    Quote Originally Posted by truetrini20 View Post
    Thanks for the help @ dante and i understand what you're saying @ g4j31a5, all i wanted was a little direction not a full layout but i know @ dante was just trying to help
    I'm glad you understand. Well I can give you a slight hint. Mathematical functions are derived from bitwise operations. So if you understand the concept of bitwise operations like "AND", "OR", "XOR", bitshifting, etc, maybe you can make your own function.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by g4j31a5 View Post
    Or quake's fast square root.
    "Quake's fast inverse square root" to be more precise
    Last edited by iMalc; 06-29-2010 at 01:50 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Quote Originally Posted by iMalc View Post
    "Quake's fast inverse square root" to be more precise
    O_o

    Code:
    sqrtn = n * invsqrt(n);
    If you really don't mind the loss of precision.

    Soma

  10. #10
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by phantomotap View Post
    O_o

    Code:
    sqrtn = n * invsqrt(n);
    If you really don't mind the loss of precision.

    Soma

    http://en.wikipedia.org/wiki/Fast_inverse_square_root

    Well, he said without math library so...
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solve This Problem Within 3 Hr Urgent Need
    By annum in forum C Programming
    Replies: 12
    Last Post: 10-04-2009, 09:56 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. problem solve
    By coolnarugodas in forum C Programming
    Replies: 7
    Last Post: 04-26-2005, 12:31 PM
  4. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  5. problem cant solve please help.
    By sarah in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:32 PM