Thread: Newton-Raphson number squaring method/pointers

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    6

    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};
    float answer [MAX] = {0};

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

    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;
    *(answer + i) = guess;

    }
    }

    }

    /* ---------------------------------------------------------------------------------------
    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 */
    printf ("\nYour input: %d", userIn);
    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. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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; 
        float answer=0.0;
    
        /* Statements * 
        ObtainUserInput (&userIn, &accuracy); 
        NewtonRaphson (accuracy, userIn, &answer); 
    
        printf("%f\n",answer);
    
        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;
        }
    
        *answer = guess; 
    
    } 
    
    

  3. #3
    Unregistered
    Guest
    Hello zen,
    Thank you very, very much for spending your time and correcting an admitted idiot's program. Since words are often abused I can only assure you that I appreciate your help with all sincerity...

    Thank you again,
    inakappeh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 10:38 PM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM
  4. Perfect number
    By TheSki in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 04:34 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM