Erroneous answer in reciprocal number algorithm

This is a discussion on Erroneous answer in reciprocal number algorithm within the C Programming forums, part of the General Programming Boards category; Hi all, I have written a program to find the reciprocal of an input number by using newton rhapson iterative ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Erroneous answer in reciprocal number algorithm

    Hi all,

    I have written a program to find the reciprocal of an input number by using newton rhapson iterative method. Below is the algorithm implementation in C:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
    double guess,newguess,input,error=1.0;                                       
    printf("Enter the input number");
    scanf("%f",&input);
    printf("Enter the guess for the reciprocal of the input number between -1.0 and 1.0");
    scanf("%f",&guess);
    while(error>0.0001)
    {
             newguess=guess*(2-(input*guess));
             error=newguess-guess;
             guess=newguess;
    }
    printf("The reciprocal of the input number is %f",newguess);
    system("pause");
    return 0;
    }
    When I enter 3 as my input number, i get an absurd answer. On using hand-calculations on paper, I am converging to the right value of the reciprocal which is .33. Kindly point out the errors. Thank you.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    We've probably mentioned this already,

    Code:
    bar.c: In function ‘main’:
    bar.c:8: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’
    bar.c:10: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’
    You really need to watch your scanf calls.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thank you for your prompt reply. Solves my problem, apologize for the trivial question, my IDE dev c was running fine with %f so that is why I couldn't detect the problem earlier.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    What is "dev c"?

    Consider using Code::Blocks, which uses GCC as the compiler suite.
    Then you can configure such options as "-W -Wall -ansi -pedantic" and get lots of good information about mistakes which are more than just syntax errors.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Erroneous answer in algorithm problem
    By abhishekcoder in forum C Programming
    Replies: 13
    Last Post: 05-05-2012, 02:09 PM
  2. Right-prime number algorithm problem
    By shufu7-11 in forum C Programming
    Replies: 5
    Last Post: 11-07-2011, 05:41 PM
  3. Prime number algorithm
    By Akkernight in forum C Programming
    Replies: 9
    Last Post: 04-19-2009, 01:50 PM
  4. Number-to-Word Algorithm
    By byte in forum C Programming
    Replies: 4
    Last Post: 09-23-2005, 12:59 PM
  5. prime number algorithm
    By Unregistered in forum C++ Programming
    Replies: 12
    Last Post: 02-22-2002, 10:30 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21