# Help writing a function w/ Pointers given specific specifications

Page 1 of 1

## 3 Replies - 2399 Views - Last Post: 13 October 2006 - 05:58 PMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=19641&amp;s=594c8738ddffe633d5a11d00444db89d&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 VexRizor

Reputation: 0
• Posts: 2
• Joined: 12-October 06

# Help writing a function w/ Pointers given specific specifications

Posted 13 October 2006 - 12:56 PM

Here are the instructions:

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

Is This A Good Question/Topic? 0

## Replies To: Help writing a function w/ Pointers given specific specifications

### #2 gregoryH

Reputation: 60
• Posts: 656
• Joined: 04-October 06

## Re: Help writing a function w/ Pointers given specific specifications

Posted 13 October 2006 - 03:26 PM

Hi

The first problem for novice programmers is the trap that thinking you have to write code to create a program. Professional programmers all learn to turn off their computers and take a notepad and pen, go outside, sit under a nice shady tree and start analysing the problem.

It is this abstraction from the computer that enables the programmer to really understand the problem.

Now, lets apply this analysis to your problem.. see where we go.

Based on your description, it appears that your function "perfectsquare" should return a value, either by returning a true/false or by using the pointer(aha) to return a 1 or 0 (hey - its right there in your blurb).

If you have read the tut on Pointers, you may now realise what the pointer is actually doing. Your variable perfect_ptr value normally cannot be changed, but using the & (reference) to pass the pointer(address in memory) your functions access the variables by address and change the values in the main function.

Given this situation, inside main you have num, perfect_ptr and root_ptr, of which perfect and root have their values altered by the function call... there is the first clue.. if you adjust the value of perfect_ptr inside perfectSquare to be a 1 or a 0 you can now write an if statement in main to detect that...

back to you

regards

Greg

This post has been edited by gregoryH: 13 October 2006 - 03:27 PM

### #3 VexRizor

Reputation: 0
• Posts: 2
• Joined: 12-October 06

## Re: Help writing a function w/ Pointers given specific specifications

Posted 13 October 2006 - 04:44 PM

Is that what you mean:

```//Function Prototypes
void perfSquare(int num, int *perfect_ptr, int *root_ptr);

int main(void) {

//Declare & initialize variables
int num, perfect_ptr, root_ptr;
int i;

//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);

if (perfect_ptr == 1) {

printf("%d is a perfect square of %d \n", num, i);
}
}

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) {
*perfect_ptr = 1;

}
else {

printf("%d is NOT a perfect square. The next perfect square is \n", num);
}
}

```

The problem now is when I run and say I enter 64 for an integer it'll tell me 64 is a perfect square of [some bogus #]. I can't return the value of i since it's not one of the parameters I'm supposed to use for the function according to the instructions.

### #4 gregoryH

Reputation: 60
• Posts: 656
• Joined: 04-October 06

## Re: Help writing a function w/ Pointers given specific specifications

Posted 13 October 2006 - 05:58 PM

VexRizor, on 13 Oct, 2006 - 04:44 PM, said:

Is that what you mean:

<snip>

The problem now is when I run and say I enter 64 for an integer it'll tell me 64 is a perfect square of [some bogus #]. I can't return the value of i since it's not one of the parameters I'm supposed to use for the function according to the instructions.

Vex,

Very good, you have now passed a value back using pointers.... if you read your own blurb, root_ptr is supposed to have the value of the root (i) passed to it too. Just follow the pattern you established.

to remove the last cout in your perfectsquare function, why not use an else when the test for perfect_ptr is not equal to 1?