Program name: squareroot-function.c

Language features of C contained in "squareroot-function.c"

Function:
  uses a  function that knows how to calculate square roots. The function is contained within the program, written and defined by the user, not "borrowed" from any outside source.

Source code:

 

Explanations: The last program, library-function.c, used a function named sqrt( ) that calculates square roots. But that function was not present in library-function.c. Rather it was borrowed (from a place referred to as "math.h"). This current program, squareroot-function.c, does the square root work itself. It contains a function that it chooses the name "sqroot( )". That function contains a loop, that causes some code (lines 11 and 12) to be executed repeatedly for a while.

The main function asks the user for a number of which to take the square root, which it gives to the function. The function gives the square root back, and the main program prints it out for the user to see. How the code in the sqroot( ) function is able to figure out square roots is not obvious. The methodology or steps for accomplishing a specific job like this is called an algorithm.

The logic of this algorithm is that if you try to guess the square root of a number, the test for success will be that when you divide your guess into the number, you get your guess. If you don't, your guess is too small or too big. If it were too small, the division will result in something too big (or vice-versa). So take the average between the too-small and too-big numbers, and make that be your next guess. Repeating this a few times, the guesses will get closer and closer to the actual square root until they are so close that they are equal to it, in effect. Doing this repetition is the job of the so-called "while loop" in lines 10 through 13. It keeps going, refining the square root guesses till it reaches the correct square root, as evidenced when successive guesses stop being different (i.e., they stop being off).

I wrote a slightly modified version of this program in order to clarify the algorithm by printing mid-stream progress reports about what it's doing on the screen. Otherwise, it does the same thing as the above squareroot-function.c. I used it to calculate the square root of 101. I know the square root of 100 is 10, so the square root of 101 must be just a little bit more than 10. The program starts by guessing that the square root is half of 101, or 50.5. It tests that guess by dividing 101 by 50.5, to see if the answer is also 50.5. If so, then 50.5 is the correct square root. 

However, dividing 101 by 50.5 gives 2. So 50.5 is wrong. It's too large, and 2 is too small. So guess again, this time take the average of the too-big 50.5 and the too-small 2 and see what we get: 26.25. Let's make that be the second guess, we know it's better than 50.5, and apply the test to that. This is done repeatedly and you can see the guesses get closer and closer to 10, going to 15.05, then 10.88, ... eventually getting a number that passes the test: 10.049876.

You can watch the program run dynamically as it sneaks up on the answer step-by-step.

To do:

Key this program in, using a text editor. Save it in a file named "squareroot-function.c". Then, in order to convert it into a form that contains correct Intel machine instructions, thus enabling the computer to run it, bring up the Windows command box and give this command:

bcc32 squareroot-function.c

Alternatively use a browser to go to a C compiler web site. Cut your program's text from the text editor and paste it into the appropriate place in the web site. Tell the web site to compile (there's generally a button that says "compile" or maybe "run".)

If there are no error messages, a new file named "squareroot-function.exe" will result. Then give the command:

squareroot-function.exe

The program asks the user for a number (line 23). Then it acquires the user's typed response, capturing it into variable mynum (line 24). In line 26 it both gives mynum to the sqroot( ) function and, on the fly, prints the result given back by sqroot( ).