# Thread: Creating a program without using any libraries

1. ## 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. 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. 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. 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. 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.

6. Originally Posted by Babkockdood
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. Originally Posted by Babkockdood
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.

8. Originally Posted by benrogers
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.

9. Originally Posted by quzah
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?

10. Originally Posted by Babkockdood
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.

11. Originally Posted by benrogers
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. Originally Posted by benrogers
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. 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```

14. Originally Posted by Babkockdood
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.

15. 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.