Project Pretend

This is based on my Project 2 from CMSC 201 in Fall 97.


The Objective

The objective of this assignment is to give you some practice writing functions, using the math library, and formatting output.

The Background

Although the square and cube of a number are very easy to calculate, finding the square root of a number, or the approximation of the square root, can be quite time consuming, especially if done by hand. Using a computer, we can calculate square roots by using a successive approximation algorithm. There are many such algorithms, but the one you will be using for this project is shown below:

The square root of a number n can be approximated by repeated calculation using the formula

nextGuess = 0.5 * (lastGuess + n / lastGuess)

where an initial guess of 1.0 will be the starting value of lastGuess. A value for nextGuess should then be computed using the formula given above. The difference between nextGuess and lastGuess should then be checked to see if those two values are almost identical. If they are, nextGuess is accepted as the square root of the number; otherwise, the nextGuess becomes the lastGuess and the process is repeated (another value is computed for nextGuess, the difference checked, and so on). The loop should be repeated until the difference is less than 0.005

The Task

You are to write a program that calculates the square root of a number. The square root function that you write will return an approximation of the square root of the number. You will then get the square root of the number by using the sqrt function found in the math library. You should report the absolute value of the difference between your approximate square root and the square root found by the function in the math library.

After printing the program explanation for the user, you should begin by getting a positive real number from the user. Then you should call the ApproxSquareRoot function to calculate the approximate square root of the number, the sqrt function in the math library to get a more precise square root approximation, and then compare your approximation of the square root to the square root of the number that was returned by the sqrt function in the math library. Finally, the results should be printed.

You will be expected to write five functions, other than main, for this project.

Here are the function prototypes:

Function Descriptions:


Main Page

Last Modified 3 October 2000