EvilZone

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

Title: Finding square root using Newton-Raphson Method
Post by: parad0x on March 10, 2013, 09:35:01 AM
Here's source code, just wrote it.
Code: (C) [Select]
#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;
}
Title: Re: Finding square root using Newton-Raphson Method
Post by: pune3t on March 10, 2013, 10:11:02 AM
add some comments for better understanding.
:)
Title: Re: Finding square root using Newton-Raphson Method
Post by: parad0x on March 10, 2013, 10:22:08 AM
add some comments for better understanding.
:)
puneet, you know C??
Title: Re: Finding square root using Newton-Raphson Method
Post by: pune3t on March 10, 2013, 10:25:38 AM
yes, actually I'm learning it too.
:)
Title: Re: Finding square root using Newton-Raphson Method
Post by: DaNePaLI on March 10, 2013, 02:24:25 PM
Hi parad0x, you made a small flaw at line 15.

Replace
Code: [Select]
while(absolute(guess * guess - x*x) >= difference){with
Code: [Select]
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.
Title: Re: Finding square root using Newton-Raphson Method
Post by: pune3t on 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.
Title: Re: Finding square root using Newton-Raphson Method
Post by: parad0x on 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.
Title: Re: Finding square root using Newton-Raphson Method
Post by: p_2001 on 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.
Title: Re: Finding square root using Newton-Raphson Method
Post by: Super_mario666 on 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


Code: (c) [Select]

#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;
}
Title: Re: Finding square root using Newton-Raphson Method
Post by: flowjob on 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!
Title: Re: Finding square root using Newton-Raphson Method
Post by: parad0x on 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


Code: (c) [Select]

#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().
Title: Re: Finding square root using Newton-Raphson Method
Post by: Super_mario666 on 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)
Title: Re: Finding square root using Newton-Raphson Method
Post by: Axon on March 22, 2013, 12:42:14 AM
This brings back bad memories when I took a course in numerical analysis.
Title: Re: Finding square root using Newton-Raphson Method
Post by: Xires on 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.
Title: Re: Finding square root using Newton-Raphson Method
Post by: Deque on 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.
Title: Re: Finding square root using Newton-Raphson Method
Post by: rasenove on March 26, 2013, 05:53:22 AM
Quote from:  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.