Reputation: 202
I'm having trouble grasping the concept of pointers. I was given the following problem.
The square root of a number N can be approximated by repeated calculation using the formula NG = 0.5( LG + N/ LG) where NG stands for next guess and LG stands for last guess. Write a function that calculates the square root of a number using this method. The initial guess will be the starting value of LG . The program will com-pute a value for NG using the formula given. The difference between NG and LG is checked to see whether these two guesses are almost identical. If they are, NG is accepted as the square root; otherwise, the next guess ( NG ) becomes the last guess ( LG ) and the process is repeated ( another value is computed for NG, the difference is checked, and so on). The loop should be repeated until the difference is less than 0.005. Use an initial guess of 1.0.
The program must be written using pointers wherever possible both in main and in all functions. Declare pointers in main for the number whose square root is to be guessed and for the approximated square root answer which will be calculated and returned/referenced back to main by the function. The function will be a void function, so the approximated square root answer must be returned/referenced to main as an output argument of the function, i.e., to an argument in the function calling statement. You must pass to the function the actual value of the number to be guessed using a pointer.
In the function, have the user enter a "guess" number which is the user's initial guess of the square root of the number passed to the function.
(sorry about the lengthy explanation, I felt I should be thorough)
I wrote the following code:
#include <stdio.h>
#include <math.h>
void fctn(double *nPtr,double *NGPtr){
double n;
nPtr=&n;
double NG;
double LG;
NGPtr=&NG;
printf("Enter guess\n");
scanf_s("%lf",&LG);
do{
NG=(.5*(LG+n/LG));
LG=NG;
}while(fabs(NG*NG-n)>.005);
}
int main(){
double *NGPtr;
double *nPtr;
printf("Enter number\n");
scanf_s("%lf",&nPtr);
fctn(NGPtr,nPtr);
double root=*NGPtr;
printf("The approximate root of the function is %f",root);
}
I was wondering if anyone had any ideas as to why it was not working? Thank you for reading.
Upvotes: 2
Views: 1120
Reputation: 110768
double *nPtr;
printf("Enter number\n");
scanf_s("%lf",&nPtr);
Let's look at what's happening here. First you declare a pointer to double
. That pointer doesn't point anywhere. No double
object has been created, only a pointer. You then pass &nPtr
, which is a pointer to the pointer, to scanf_s
, which attempts to read a value into the pointer. So you're asking the user to enter a memory address! That's not right.
What you want instead is to have a double
object that you then read into. To do this, you should declare a double
(not a pointer), and then pass a pointer to that double
to scanf_s
:
double n;
printf("Enter number\n");
scanf_s("%lf", &n);
You have a similar problem with NGptr
too. It should just be a double NG;
instead. Otherwise later, when you do *NGPtr
, you will be dereferencing a pointer that doesn't point at anything.
I believe you then pass your arguments to fctn
the wrong way round. The declaration suggests n
should come first and NG
should come second.
Since you only need to use the value of n
inside the function, you should just pass it by value:
void fctn(double n, double *NGPtr){
Passing a pointer to NG
is required because you wish to modify it inside the function, but a reference type would be nicer:
void fctn(double n, double& NG){
You then have a series of problems at the beginning of fctn
. You don't need to create n
and NG
double
s anymore because we are passing them in to the function.
Upvotes: 3
Reputation: 27548
The program must be written using pointers wherever possible both in main and in all functions.
Is this a homework assignment? If so, your teacher must be trying to teach you code obfuscation. If you take it literally, then why not take it to the extreme and use function pointers? Instead of calling fctn
, fabs
, printf
and scanf_s
directly, declare function pointer variables.
Here's how you would use a function pointer to call printf:
int (*output_function)(char const *, ...);
output_function = printf;
output_function("%s %d", "test", 123);
The real problem with "using pointers wherever possible", however, is that the definition is an endless recursion, for in C++, there can be pointers to pointers, and pointers to pointers to pointers, and pointers to pointers to pointers to pointers, and so on... There's no real limit to the number of pointers you can use in a program.
Here's a complete example for a pointer to a function pointer:
#include <stdio.h>
int main()
{
int (*output_function)(char const *, ...);
int (**pointer_to_output_function)(char const *, ...);
output_function = printf;
pointer_to_output_function = &output_function;
(*pointer_to_output_function)("%s %d", "test", 123);
}
This is horrible C++ programming style, of course, but so is the code example you gave. If this a real problem, forget about pointers for now. Use references or pass by value, and use <iostream>
instead of printf
et al.
Upvotes: 0
Reputation: 1
At least this is going to be problematic
double n;
nPtr=&n;
since you're returning a reference to a temporary object.
Upvotes: 1
Reputation: 29266
Without giving you the code: You never "point your pointers at anything". (aka initialize them). You probably want actual doubles in main and pass pointers to those double to fctn
.
Upvotes: 1