Thread: Square Root C

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    6

    Square Root C

    I'm trying make a program using a newton-raphson method this formula run 10 times:


    yk+1 = 1/2 . (yk + a/yk)
    y1 = 1


    The code below have some error, the output have return 0.

    Help, thank you!



    Code:
    #include <stdio.h>
    #include <math.h>
     
    int main(void)
    {
        int k, j, a, b;
        float y;
     
        scanf("%d", &a);
     
        for(k = 1; k > a; k++)
        {
            y = 1;
            for(j = 1; 10 < j; j++)
            {
                y = (y + a/y)/2;
            }
        }
            printf("%d", y);
     
        return 0;
    }
    Last edited by useredu; 04-12-2014 at 12:44 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quick glance suggest ">" is wrong or k++ is wrong.

    Tim S.

    Code:
    for(k = 1; k > a; k++)
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    continues return 0 if I change > to <

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You wrote:

    printf("%d", y);

    y is a float variable, so use %f.

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    Using %f, now return 0.000000

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I don't think you really understand the Newton-Raphson method. If you can't solve the problem yourself, with paper and pencil, then there is no hope in telling a computer how to do it (i.e. writing a program). You must figure out the math yourself first.

    Some basic hints:

    • The purpose of it is to find the roots of a function, i.e. where that function crosses the x-axis.
    • You seem to be missing the whole derivative part. You must have a function, and have it's derivative.
    • Think about the function y = 1 (if that is what you are using -- though it's not clear). Does it make sense to apply the Newton-Raphson method? Why or why not?
    • Read the "Practical Considerations" section of the above-linked Wikipedia article, and make sure when you work this out, you pick reasonable initial guesses.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The outer loop with k just repeats the same process for the inner loop and is not needed. According to the replies in this thread, this is the code you should have and it's working for me:

    Code:
    #include <stdio.h>
     
    int main(void)
    {
        int j, a;
        float y;
     
        scanf("%d", &a);
    
        y = 1.;
        for(j = 0; j < 10; j++)
        {
            y = (y + a/y)/2;
        }
        printf("%f\n", y);
     
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Nevermind my comments in #6. I missed the title of your thread and couldn't figure out what you were trying to do with the N-R method. Makes sense now. Between whiteflags and rcgldr, you should be good to go.

  9. #9
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    Thank you!!!!

  10. #10
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    Thank you all!!

  11. #11
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    The second loop is for to show a table, example: the number and your root, but now I understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Square Root
    By chillycoke in forum C Programming
    Replies: 26
    Last Post: 06-27-2011, 07:43 AM
  2. square root
    By Noobie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2003, 11:01 AM
  3. Square root
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 07-05-2002, 06:35 AM
  4. Square Root??
    By Dangerous Dave in forum C Programming
    Replies: 5
    Last Post: 10-18-2001, 10:34 PM
  5. square root
    By help in forum C Programming
    Replies: 5
    Last Post: 08-29-2001, 05:46 AM

Tags for this Thread