[an error occurred while processing this directive]

CMSC201
Programming Project Two

Exponents

Out: Tuesday 10/7/97
Due: Midnight Tuesday 10/21/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 of a number, its cube, and also its square root. Each of these tasks should be performed in separate functions. 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 Square function to calculate the square of that number, the Cube function to calculate the cube of the number, 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 seven functions, other than main, for this project.

Here are the function prototypes:

Function Descriptions:

umbc9[1]% a.out
This program will find the square, the cube, and the approximate
square root of any positive real number you enter.  It finds the
square root by successive approximation.  This approximate 
square root will then be compared to the square root computed using 
the sqrt function found in the math library.  A comparison of these 
values will be done and the difference shown in exponential notation.  

Please enter a positive real number: 2.0
You entered 2.000000
Its square is 4.000000
Its cube is 8.000000
The approximate square root of 2.000000 is 1.414216
The sqrt function reports it as 1.414214
The difference is 2.123901e-06

umbc9[2]% a.out
This program will find the square, the cube, and the approximate
square root of any positive real number you enter.  It finds the
square root by successive approximation.  This approximate 
square root will then be compared to the square root computed using 
the sqrt function found in the math library.  A comparison of these 
values will be done and the difference shown in exponential notation.  

Please enter a positive real number: 4.0
You entered 4.000000
Its square is 16.000000
Its cube is 64.000000
The approximate square root of 4.000000 is 2.000000
The sqrt function reports it as 2.000000
The difference is 9.292229e-08

umbc9[3]% a.out
This program will find the square, the cube, and the approximate
square root of any positive real number you enter.  It finds the
square root by successive approximation.  This approximate 
square root will then be compared to the square root computed using 
the sqrt function found in the math library.  A comparison of these 
values will be done and the difference shown in exponential notation.  

Please enter a positive real number: -5.0
-5.000000 is not a positive real number.  Try again.
Please enter a positive real number: 120.5
You entered 120.500000
Its square is 14520.250000
Its cube is 1749690.125000
The approximate square root of 120.500000 is 10.977249
The sqrt function reports it as 10.977249
The difference is 1.531799e-09
  
umbc9[4]% a.out
This program will find the square, the cube, and the approximate
square root of any positive real number you enter.  It finds the
square root by successive approximation.  This approximate 
square root will then be compared to the square root computed using 
the sqrt function found in the math library.  A comparison of these 
values will be done and the difference shown in exponential notation.  

Please enter a positive real number: 10000
You entered 10000.000000
Its square is 100000000.000000
Its cube is 1000000000000.000000
The approximate square root of 10000.000000 is 100.000000
The sqrt function reports it as 100.000000
The difference is 0.000000e+00

umbc9[5]% a.out
This program will find the square, the cube, and the approximate
square root of any positive real number you enter.  It finds the
square root by successive approximation.  This approximate 
square root will then be compared to the square root computed using 
the sqrt function found in the math library.  A comparison of these 
values will be done and the difference shown in exponential notation.  

Please enter a positive real number: 88
You entered 88.000000
Its square is 7744.000000
Its cube is 681472.000000
The approximate square root of 88.000000 is 9.380832
The sqrt function reports it as 9.380832
The difference is 2.378364e-11

Submitting the Program

To submit the file you should use the command:

submit cs201 proj2 proj2.c

You can check your submission by using the command:

submitls cs201 proj2 [an error occurred while processing this directive] Tuesday, 07-Oct-1997 13:50:04 EDT