# Thread: Newton-Raphson number squaring method/pointers

1. ## Newton-Raphson number squaring method/pointers

Hello and thank you for any that reads this...

Another 12 to 16 hours can be spent and my useless implementation of the Newton-Raphson method
is still not going to work. Can anyone please tell me how to proceed? My complete program is written below.
The reason for the excessive use of pointers is due to an assignment requirement to utilize pointers as much as possible.

For those unfamiliar, the Newton-Raphson method simply squares a number with the degree of accuracy specified by a user, so if the number 100 is entered, the output should be 10 etc.

I sincerely appreciate any help and best regards to anyone involved with this site...

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX 3

/* Function Prototypes */
void ObtainUserInput (int *userIn, float *accuracy);
void DivideUserInput (int userIn, float *divideIt);
void NewtonRaphson (float accuracy, float *divideIt, float *answer);
void PrintData (int userIn, float *divideIt, float *answer);

int main (void)
{
/* Variable defs */
int userIn = 0;
float accuracy = 0;
float divideIt [MAX] = {0};

/* Statements */
ObtainUserInput (&userIn, &accuracy);
DivideUserInput (userIn, divideIt);

return 0;
}

/* ---------------------------------------------------------------------------------------
ObtainUserInput ();
Pre: nothing.
Post: obtains the formula 'x' and the desired accuracy for computing the Newton sqrt.
--------------------------------------------------------------------------------------- */
void ObtainUserInput (int *userIn, float *accuracy)
{
/* Local var defs */

/* Statements */
do
{
printf ("Please enter any number greater than 4: ");
scanf ("%d", userIn);

if (*userIn < 4)
{
printf ("\n%d doesn't look likes its greater than 4...try again please: ");
scanf ("%d", userIn);
}
} while (*userIn < 4);

printf ("\nSo how accurate do you want it to be (0.0001)?: ");
scanf ("%f", accuracy);

}
/* ---------------------------------------------------------------------------------------
DivideUserInput ();
Pre:
Post:
--------------------------------------------------------------------------------------- */
void DivideUserInput (int userIn, float *divideIt)
{
/* Local defs */
int i = 0;

/* Statements */
*(divideIt + 0) = userIn;
*(divideIt + 1) = userIn / 2;
*(divideIt + 2) = userIn / 4;

}

/* ---------------------------------------------------------------------------------------
NewtonRaphson (); should square each number of the divideIt array.
Pre: userIn and accuracy vars.
Post: does some square root computation. // DOES NOT WORK PROPERLY
--------------------------------------------------------------------------------------- */
void NewtonRaphson (float accuracy, float *divideIt, float *answer)
{
/* Local defs */
float guess = 1.0;
float *divide;
int i = 0;

/* Statements */
divide = divideIt;
for (i = 0; i < MAX; i++)
{
if (fabs ((guess * guess) - *(divide + i)) >= accuracy)
{
guess = ((*(divide + i) / guess) * guess) / 2;

}
}

}

/* ---------------------------------------------------------------------------------------
PrintData (); takes in data manipulated in Newton-Raphson function above.
Pre: Newton-Raphson stuff.
Post: prints it all up with pointers.
--------------------------------------------------------------------------------------- */
void PrintData (int userIn, float *divideIt, float *answer)
{
/* Local defs */
float *pWalker;
float *pLast;
int i = 0;
/* Statements */
puts ("");

for (i = 0; i < 3; i++)
printf ("\ndivide: %f", divideIt [i]);

pLast = divideIt + MAX - 1; //pLast assigned to end of divisionResult array.

printf ("\nNumber entered by user, divided by half and four: ");
for (pWalker = divideIt; pWalker <= pLast; pWalker++) //pWalker assigned to divisionResult [0].
{
printf ("\n%.2f.", *pWalker); //bang out array with pointer.
}

puts ("");

pLast = answer + MAX - 1;

printf ("\nNumbers squared: ");
for (pWalker = answer; pWalker <= pLast; pWalker++)
{
printf ("\n%.2f.", *pWalker);
}
}

/* end of program */

2. I'm not entirely sure what you're trying to achieve, but if you just want to use the Newton-Raphson method to find the square root of a number then this code might help -

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Function Prototypes *
void ObtainUserInput (int *userIn, float *accuracy);
void DivideUserInput (int userIn, float *divideIt);
void NewtonRaphson (float accuracy, int userIn, float *answer);

int main (void)
{
/* Variable defs *
int userIn = 0;
float accuracy = 0.0;

/* Statements *
ObtainUserInput (&userIn, &accuracy);

return 0;
}

/* ---------------------------------------------------------------------------------------
ObtainUserInput ();
Pre: nothing.
Post: obtains the formula 'x' and the desired accuracy for computing the Newton sqrt.
--------------------------------------------------------------------------------------- *
void ObtainUserInput (int *userIn, float *accuracy)
{
/* Local var defs *

/* Statements *
do
{
printf ("Please enter any number greater than 4: ");
scanf ("%d", userIn);

if (*userIn < 4)
{
printf ("\n%d doesn't look likes its greater than 4...try again please: ");
scanf ("%d", userIn);
}
} while (*userIn < 4);

printf ("\nSo how accurate do you want it to be (0.0001)?: ");
scanf ("%f", accuracy);

}

void NewtonRaphson (float accuracy, int userIn, float *answer)
{
/* Local defs *
float guess = 1.0;
float div2 = (float)userIn/2;

while (1)
{
guess = (div2+ userIn/div2)/2;

if((fabs (guess-div2) < accuracy))break;

div2 = guess;
}