The Problem

This program deals with finding the square root of a number. If the square root happens to be an integer, the number is said to be a perfect square. Thus the squares of 1,2,3,4,5,6 . . . .are all perfect squares. The first part of this assignment is to determine whether a given number N is a perfect square. If it is not a perfect square then we are interested in knowing the next perfect square. A simple solution to this problem is to compare N with i * i, where i can take on values 1, 2, 3, 4, . .. If there is a match, N can be declared a perfect square. If there is no match, then the value of i * i which just exceeds N, gives the next perfect square. Thus, given the number 64, one can declare it to be a perfect square as N equals i * i for i = 8, while the number 70 is not a perfect square and the next perfect square for it is 81. (i= 9).

The second part of the assignment is to find the square root of any integer. The method we shall use is quite old. The method starts off with a guess for the square root, and keeps on refining it till the desired accuracy is met.

Given a number N, let the first guess to the square root be r, a number smaller than N. Calculate p = N / r. The square root of N now lies between the limits r and p. The next guess to the square root would be the mid value of r and p. The process can be repeated with the improved guess of the square root. To illustrate the method, let us choose a number whose square root is well known to us.

Let N = 36 with r = 2.

p = N / r

= 36/ 2

= 18.

The square root of 36 now lies between 2 and 18. The first guess to the square root can be improved by computing:

(r + p) / 2

= (2+18)/2

= 10.0 which is the square root of 100.

To refine the value of the square root repeat the process by finding p again:

p = 36/10

= 3.6

The value of the square root now improves to

r = (10 + 3.6) / 2

= 6.8, which is closer to the actual square root.

The process can be repeated to get the value of r to any desired accuracy. Every time a new value of r is found, the accuracy of the square root is given by the difference between N and square of r. The difference is called error e. Thus e = N – r*r. The computation process can be stopped when the value of e goes below a pre-specified value. Note that we are interested only in the magnitude of the error. Any time the error becomes negative, we just set error = – error to get the absolute value of error.

Main function

In this assignment you have to write two functions which will be called by function main. In function main, you will prompt the user to enter an integer N. Function main should first call Function A to check whether the integer is a perfect square or not, and then call Function B to find its square root. (Note: Please don't actually call your functions FunctionA or FunctionB. Please give each function an appropriate name.) You will then prompt the user to enter the next integer and repeat the above steps. The program should terminate when the user enters – 1.

Function A: Checking to see if the integer is a Perfect square

This function should determine whether the integer entered by the user is a perfect square or not. It should take in three parameters. The first parameter should be an integer N, for which we are determining the square root. The next two parameters will be pointers to integers perfect_ptr and root_ptr. The function sets the variable pointed to by the first parameter to 1, if the integer happens to be a perfect square and the variable pointed to by root_ptr to the square root of the integer. If the integer is not a perfect square then it should set the variable pointed to by the first parameter perfect_ptr to 0 and the variable pointed to by root_ptr to the square root of next perfect square.

If the integer passed to the function is a perfect square, the main function should print “N is a perfect square of root .”

Otherwise, if the integer passed to the function is NOT a perfect square, the main function should print “N is not a perfect square. The next perfect number is NPS.” See the sample output.

The parameter list contains 3 parameters, namely

i) N, the number for which we want the square root

ii) perfect_ptr, an integer pointer to the integer storing whether or not N is a

perfect square

iii) root_ ptr, an integer pointer to the integer storing either the square root of N, or

the next perfect square after N.

Function B: Computing the square root

This function takes in three parameters. The first parameter is N, the integer of which to take the square root. The last two parameters are sqroot_ptr and error_ptr, pointers to variables storing the current guess to the square root and the error of that guess. This function should change both the value of the variables pointed to by sqroot_ptr and error_ptr based on the method described above. Every time the function is called it should print “The next value of root is sqroot and the error is error”.

The function main should examine the value of the variable storing the error, and if its value is more than 0.001, main should call Function B again with pointers to the variables storing both the current guess of the square root and the error. The value of error keeps on reducing every time the function is called, as the function improves the value of the square root, and when error is 0.001or less, function main prints “The square root of N is sqroot.” and asks the user to enter the next integer. Note that the square root must be printed with 3 decimal places of accuracy and that the initial guess for the root should be 2. The program is terminated when the user enters –1.

Sample output

Enter an integer.

64

64 is a perfect square of 8.

The next value of root is 17.000 and the error is 225.000

The next value of root is 10.382 and the error is 43.793

The next value of root is 8.273 and the error is 4.448

The next value of root is 8.005 and the error is 0.072

The next value of root is 8.000 and the error is 0.000

The square root of 64 is 8.000.

Enter an integer.

85

85 is NOT a perfect square. The next perfect square is 100.

The next value of root is 22.250 and the error is 410.062

The next value of root is 13.035 and the error is 84.914

The next value of root is 9.778 and the error is 10.609

The next value of root is 9.235 and the error is 0.294

The next value of root is 9.220 and the error is 0.000

The square root of 85 is 9.220.

Enter an integer.

– 1

Thank you !

Here is what I have so far:

#include <stdio.h> #include <math.h> //Function Prototypes void perfSquare(int num, int *perfect_ptr, int *root_ptr); int main(void) { //Declare & initialize variables int num, perfect_ptr, root_ptr; //Continue until the users quits while (num != -1) { printf("Enter an integer. \n"); scanf("%d", &num); if (num == -1) break; //Call the perfect square function perfSquare(num, &perfect_ptr, &root_ptr); } printf("Thank you! \n"); system("PAUSE"); return 0; } //Check to see if the integer is a perfect square void perfSquare(int num, int *perfect_ptr, int *root_ptr) { //Declare & initialize variables int i = 0; //Create values for i and then compare to num for (i = 0; i < 101; i++) { //If there is a match get out of the loop if (num == i * i) break; } if (num == i * i) { printf("%d is a perfect square of %d \n", num, i); } else { printf("%d is NOT a perfect square. The next perfect square is \n", num); } }

I don't understand how to implement the pointers based on the given instructions. I don't really even see what they're for. I'm not supposed to even have the printf statements within the perfSquare function but when I try to put them into main it can create bogus output or a infinite loop. Basically I need help writing the first function, including the error output part, or given some direction and hopefully I can do Function B after seeing how FuncA (perfSquare) is done and how it interacts with the main function while using pointers.

Thanks

This post has been edited by **VexRizor**: 13 October 2006 - 01:00 PM