Thread: Squareroot help

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    100

    Squareroot help

    Hey i'm trying to do a program that returns the answer to a squareroot!!! Please help...the answer I keep on getting is "1". Frustrating frustrating.

    Code:
    #include <iostream>
    using namespace std;
    
    float Squareroot(float x);
    
    int main() {
        float numberA;
        float squrtquotient;
        
        cout << "Hey maya here's your squareroot!" << endl;
        cout << "Enter your number to squareroot and you'll be off into happyland!\n\t" ;
        cin >> numberA;
        squrtquotient = Squareroot(numberA);
        cout << "\t" << squrtquotient << endl;
        cout << "\t\tWOWSERS!!!!!!!!!!!!!!" << endl;
        cin.get();
        return 0;
        
    }
    
    float Squareroot(float x) {
          cin.get();
          return (x/x) ;
          }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Code:
    return (x/x);
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If you want to roll your own square root method, do a search on Newton-Raphson root-finding.

    If you just want to get the square root, include the header <cmath>, and call the std::sqrt function.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    x/x will always return 1, unless x is zero (in which case, the behaviour is implementation defined).

    Why not use the std::sqrt() function, which is declared in the <cmath>? The only difference is that it accepts a double argument and returns a double ......

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by MrWizard
    Code:
    return (x/x);
    Hehe. Short, sweet, and to the point.

    If you have no reason not to use cmath's sqrt(), use it. Otherwise, I'd recommend Newton's iteration. It's a beautiful thing. Like magic.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    What is Newton's iteration?

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    when someone give you a specific name, this would
    be a good time to go to

    www.google.com and type that name into search feild,
    possibly adding the word "c++" in there somewhere
    to possibly narrow the field down.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Squareroot User Defined Function
    By Air in forum C Programming
    Replies: 2
    Last Post: 01-25-2009, 05:21 PM
  2. Results: Calculate square roots
    By Sang-drax in forum Contests Board
    Replies: 7
    Last Post: 10-01-2004, 06:52 PM