Thread: Sqrt() Function

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    Sqrt() Function

    hi everyone...just wanna say thanks for everyone that help me so far since i'm very new to C.

    the problem asked to write a C function name root4() that returns the fourth root of the argument passed to it. in writing root4() use the sqrt() library function.

    this is what i came up with and i feel like its wrong...please help..

    #include<stdio.h>
    #include<math.h>
    double sqrt(double num);//function type

    int main()
    {
    double num,total;
    total = sqrt(4.0 + 7*3);
    printf("\nthe 4th root is %lf", total);
    return 0;
    }
    output:
    the 4th root is 5.000000Press any key to continue

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>total = sqrt(4.0 + 7*3);
    This is

    7*3 = 21
    4 + 21 = 25
    sqrt(25) = 5

    [edit]Obvious typo fixed (thanks ronin)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    4+21 = 5? Not in my math class.

    whadda bout

    double root4(double argument){ return sqrt(sqrt(argument)); }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What about root( double n, double number ) { return pow( number, 1.0 / n ); }

    Edit:
    Oops. Forgot to use sqrt.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    For a bit of math to explain the double square root above, incase you need it...

    sqr(x)=x^(1/2)
    fourth root (x)=x^(1/4)

    ((x^n)^m)=x^(n*m)

    so...

    sqr(sqr(x))=(x^(1/2))^(1/2)=x^(1/4)=fourth root (x)
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM