EvilZone

Programming and Scripting => C - C++ => : parad0x March 10, 2013, 09:35:01 AM

: Finding square root using Newton-Raphson Method
: parad0x March 10, 2013, 09:35:01 AM
Here's source code, just wrote it.
: (C)
#include <stdio.h>
// Function to get absolute value of the number given by user.
float absolute(float num)
{
if(num < 0){
 num = -num;
}
return num;
}
// Function to calculate square root of the number using Newton-Raphson method
float square_root(int x)
{
const float    difference = 0.00001;
float          guess = 1.0;
while(absolute(guess * guess - x) >= difference){
 guess = (x/guess + guess)/2.0;
}

return guess;
}

int main()
{
int     number;
float  root;
printf("Enter a number: ");
scanf("%i", &number);
root = square_root(number);
printf("The square root of %i is: %f", number, root);
return 0;
}
: Re: Finding square root using Newton-Raphson Method
: pune3t March 10, 2013, 10:11:02 AM
add some comments for better understanding.
:)
: Re: Finding square root using Newton-Raphson Method
: parad0x March 10, 2013, 10:22:08 AM
add some comments for better understanding.
:)
puneet, you know C??
: Re: Finding square root using Newton-Raphson Method
: pune3t March 10, 2013, 10:25:38 AM
yes, actually I'm learning it too.
:)
: Re: Finding square root using Newton-Raphson Method
: DaNePaLI March 10, 2013, 02:24:25 PM
Hi parad0x, you made a small flaw at line 15.

Replace
:
while(absolute(guess * guess - x*x) >= difference){with
:
while(absolute(guess * guess - x) >= difference){
Since x is the number whose square root you need to find, guess * guess has to be subtracted from x not from x * x.

Then your program will run fine.
: Re: Finding square root using Newton-Raphson Method
: pune3t March 10, 2013, 03:59:23 PM
and please for god sake use #include<conio.h> its easy to understand with the help of this header file, & the main reason behind using this is it uses a [ getch() function ] which holds the screen, after your program has been executed.
e.g: see the uploaded file.
: Re: Finding square root using Newton-Raphson Method
: parad0x March 10, 2013, 04:27:49 PM
and please for god sake use #include<conio.h> its easy to understand with the help of this header file, & the main reason behind using this is it uses a [ getch() function ] which holds the screen, after your program has been executed.
e.g: see the uploaded file.
conio.h header file slows down the program and for holding the screen, I am sure you are on windows. To hold the screen, use getchar() which is present in stdio.h.
: Re: Finding square root using Newton-Raphson Method
: p_2001 March 10, 2013, 06:00:45 PM
Speed up the code... The square root of a number is always less than half of the number.


the code is slow because of the number of decimals and the divisions done.
: Re: Finding square root using Newton-Raphson Method
: Super_mario666 March 10, 2013, 06:24:06 PM
well to be blunt your code is a complete mess but that just fine since your new. allow me to tidy it up a bit


: (c)

#include <stdio.h>
 
float absolute(float num); //Prototype or function declaration.
float square_root(int x);  // definition is below main
 
int main()
{
    int number;
    float root;


    printf("Enter a number: ");
    scanf("%i", &number);


    root = square_root(number);


    printf("The square root of %i is: %f", number, root);
    return 0;   
}


float absolute(float num) // Function to get absolute value of the number given by user.
{
     if(num < 0)
       num = -num;
   
     return num;
}
 


float square_root(int x)  // Function to calculate square root of the number using Newton-Raphson method
{
     const float difference = 0.00001;
     float guess = 1.0;
     while(absolute((guess*guess) - x) >= difference) //DaNePaLI's addition
     {
         guess = ((x/guess) + guess)/2.0;
     }
 
     return guess;
}
: Re: Finding square root using Newton-Raphson Method
: flowjob March 10, 2013, 06:25:21 PM
Speed up the code... The square root of a number is always less than half of the number.


the code is slow because of the number of decimals and the divisions done.

Less than or equal to half of the number: sqrt(4) = 2 = 4/2
except for 1: sqrt(1) = 1 = 1/1

It's important to not forget about these two cases too!
: Re: Finding square root using Newton-Raphson Method
: parad0x March 17, 2013, 07:13:04 AM
well to be blunt your code is a complete mess but that just fine since your new. allow me to tidy it up a bit


: (c)

#include <stdio.h>
 
float absolute(float num); //Prototype or function declaration.
float square_root(int x);  // definition is below main
 
int main()
{
    int number;
    float root;


    printf("Enter a number: ");
    scanf("%i", &number);


    root = square_root(number);


    printf("The square root of %i is: %f", number, root);
    return 0;   
}


float absolute(float num) // Function to get absolute value of the number given by user.
{
     if(num < 0)
       num = -num;
   
     return num;
}
 


float square_root(int x)  // Function to calculate square root of the number using Newton-Raphson method
{
     const float difference = 0.00001;
     float guess = 1.0;
     while(absolute((guess*guess) - x) >= difference) //DaNePaLI's addition
     {
         guess = ((x/guess) + guess)/2.0;
     }
 
     return guess;
}
mario, what you are saying are standard C++ while c99 standards say that you should write the function before it is used in main().
: Re: Finding square root using Newton-Raphson Method
: Super_mario666 March 19, 2013, 06:15:33 PM
mario, what you are saying are standard C++ while c99 standards say that you should write the function before it is used in main().

you don't have to include a function prototype just so long as the function is declared before the function is called. i personally included it because i find it easier to read and understand. but the main thing i was trying to fix was was indentation. all of your code was hugging the left side of the page which is a total pain to go though.


(http://i.qkme.me/3tfmc7.jpg)
: Re: Finding square root using Newton-Raphson Method
: Axon March 22, 2013, 12:42:14 AM
This brings back bad memories when I took a course in numerical analysis.
: Re: Finding square root using Newton-Raphson Method
: Xires March 25, 2013, 06:49:33 PM
@parad0x; prototypes are just forward-declarations of functions.  Indeed, the ANSI standard indicates their preferred use because all the information for function calls is then available before any function is actually run.  Using 'prototypes' avoids issues with inter-dependent functions and having to choose which function comes first and finally redesigning code entirely to avoid interdependence(sometimes it's not avoidable).  This is the best method and C99 has not changed that.  Use of prototypes is preferred...always.

@Deque; yes, I'm sorry.  I had like 15 minutes to do something of my own choice so I came here to check out what havoc I could wreak.  Not having time to actually respond to everything, I left placeholders to help me remember what was awaiting my comment(I do this somewhat often whilst working).  Unfortunately, work took my attention away for multiple days when I thought I could respond within a sane period of time.
: Re: Finding square root using Newton-Raphson Method
: Deque March 25, 2013, 08:45:04 PM
-- Placeholder --

Xires! I am opening all these new answers to threads just to see a placeholder everwhere. That's annoying.
: Re: Finding square root using Newton-Raphson Method
: rasenove March 26, 2013, 05:53:22 AM
:  parad0x
mario, what you are saying are standard C++
while c99 standards say that you should write
the function before it is used in main().

you can use forward  declariations to put the main() on top of other functions.