EvilZone
Programming and Scripting => C - C++ => : parad0x March 10, 2013, 09:35:01 AM
-
Here's source code, just wrote it.
#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;
}
-
add some comments for better understanding.
:)
-
add some comments for better understanding.
:)
puneet, you know C??
-
yes, actually I'm learning it too.
:)
-
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.
-
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.
-
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.
-
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.
-
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
#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;
}
-
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!
-
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
#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().
-
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)
-
This brings back bad memories when I took a course in numerical analysis.
-
@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.
-
-- Placeholder --
Xires! I am opening all these new answers to threads just to see a placeholder everwhere. That's annoying.
-
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.