Creating a program without using any libraries

This is a discussion on Creating a program without using any libraries within the C Programming forums, part of the General Programming Boards category; Is it possible without using any standard library functions, to create a function called asqrt that calculates the square root ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    34

    Creating a program without using any libraries

    Is it possible without using any standard library functions, to create a function called asqrt that calculates the square root of a double. Then create a program (this is the same question) that reads in a double from the user and calling the asqrt function, prints out the square root of the number. Your function should produce a square root within 0.001 accuracy of the perfect square root. e.g. if your program reads in 4, your answer should be between 1.999 and 2.001 (even though I'd expect you to have the right answer here, which is 2)

    I have searched everywhere and haven't found any information on how to create a program without using c libraries.

    How do I go about starting the program?

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    This is as much a mathematical question as a programming one.

    My approach would be to find a mathematical formula for approximating the square root function. Try a Taylor series, which uses only multiplication, addition, division and subtraction and so does not depend on any library functions.

    I would use a for loop to calculate the terms of the Taylor series, testing after each one to see if it's accurate enough.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Another approach I just thought of: treat it as a "guess the number" game.

    To begin with, you know the square root of x will be greater than zero and less than x. So guess it's x/2, and square it to test that. If the result is higher than x, then you know the square root must be between zero and x/2 and if it's lower you know it must be between x/2 and x. Keep doing this, keeping track of upper and lower limits, to "home in" on the answer until it's accurate enough.

    If x is negative there is no answer, and if x is between zero and one the square root of x is actually higher than x. You will need to deal with these cases- but that should be easy.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    this is what I have so far

    Code:
    int main ()
    {
        double d, mult, asqrt;
        
        printf("Please enter in a double: \n");
        scanf("%f", &d);
        
        if ( d >= 0)
        {
        mult = d * d;
        asqrt = mult / d;
        
        printf("The square of %f is %f.\n", d, asqrt);
        }
    but the output is some crazy number.

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You could do something like this.

    Code:
    double asqrt(double x) {
    return ( /* square root function here */ );
    }
    You can't receive input from the user without using the C standard library. But you can pass input and receive output from functions without it.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by Babkockdood View Post
    You could do something like this.

    Code:
    double asqrt(double x) {
    return ( /* square root function here */ );
    }
    You can't receive input from the user without using the C standard library. But you can pass input and receive output from functions without it.
    is the square roote function eg: b= a * a, c = b / a ?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Babkockdood View Post
    You can't receive input from the user without using the C standard library.
    Not entirely true. You could write your own compiler and use your own functions to get input and provide output, without using standard functions or libraries.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by benrogers View Post
    is the square roote function eg: b= a * a, c = b / a ?
    Possibly. I don't feel like looking it up right now :P

    But you don't need any libraries to receive input, pass it through the square root equation, and return it to the caller.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  9. #9
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by quzah View Post
    Not entirely true. You could write your own compiler and use your own functions to get input and provide output, without using standard functions or libraries.


    Quzah.
    Without using assembly?
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Babkockdood View Post
    Without using assembly?
    He didn't say that was a requirement. But, you could write a compiler in any language you wanted, as long as it can generate the correct output, it doesn't matter. There's a book I've got around here some place that is about implementing your own version standard compliant library functions, but I can't find it. I guess I got rid of it.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,496
    Quote Originally Posted by benrogers View Post
    is the square roote function eg: b= a * a, c = b / a ?
    Nope. Look at what you're doing there. You're setting b equal to a^2, then setting c equal to b / a. That ends up looking like:
    b = a * a
    c = b / a = (a * a) / a = a

    So all you've done is wasted some clock cycles with c = b / a instead of c = a, and you have nothing resembling a square root.

    Google "calculate square root by hand" to find numerous methods of finding a square root without using the sqrt() function.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by benrogers View Post
    Is it possible without using any standard library functions, to create a function called asqrt that calculates the square root of a double. Then create a program (this is the same question) that reads in a double from the user and calling the asqrt function, prints out the square root of the number.
    Libraries are simply collections of functions. Probably a lot of the standard C library is written in C as it is... So yes it's possible to "start from scratch"... That's what the guys who wrote the original libraries had to do.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I wrote a quick Babylonian method function that appears to work pretty well:
    Code:
    double my_sqrt(double num)
    {
      double s = num / 2;
      int i;
    
      for(i = 0;i < 5;++i)
        s = (s + (num / s)) / 2;
    
      return s;
    }
    ...at least according to my little test:
    Code:
      for(i = 2;i < 20;++i)
        printf("Square root of %d = %f, %f\n", i, my_sqrt(i), sqrt(i));
    Output...
    Code:
    Square root of 2 = 1.414214, 1.414214
    Square root of 3 = 1.732051, 1.732051
    Square root of 4 = 2.000000, 2.000000
    Square root of 5 = 2.236068, 2.236068
    Square root of 6 = 2.449490, 2.449490
    Square root of 7 = 2.645751, 2.645751
    Square root of 8 = 2.828427, 2.828427
    Square root of 9 = 3.000000, 3.000000
    Square root of 10 = 3.162278, 3.162278
    Square root of 11 = 3.316625, 3.316625
    Square root of 12 = 3.464102, 3.464102
    Square root of 13 = 3.605551, 3.605551
    Square root of 14 = 3.741657, 3.741657
    Square root of 15 = 3.872983, 3.872983
    Square root of 16 = 4.000000, 4.000000
    Square root of 17 = 4.123106, 4.123106
    Square root of 18 = 4.242641, 4.242641
    Square root of 19 = 4.358899, 4.358899
    Last edited by itsme86; 02-28-2011 at 03:45 PM.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by Babkockdood View Post
    You can't receive input from the user without using the C standard library. But you can pass input and receive output from functions without it.
    Why even bring this up? According to the OP, the requirements only said that the asqrt() function couldn't use a standard library (i.e. sqrt()) function. The rest of the program doesn't have that restriction.
    If you understand what you're doing, you're not learning anything.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could take the input trough the argv, and you could multiply the end result enough times to make it an integer, and return it from main!


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 03:50 PM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  3. creating a program to help with modding
    By chuck22 in forum C++ Programming
    Replies: 9
    Last Post: 02-16-2006, 09:16 AM
  4. creating libraries for c++
    By Klinerr1 in forum C++ Programming
    Replies: 33
    Last Post: 08-04-2002, 02:34 AM
  5. creating a simple program to accept a password.
    By boy1015 in forum C++ Programming
    Replies: 11
    Last Post: 02-05-2002, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21