954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newton's Square Root Function

help please, i have to generate a funciton which generates the squareroot of any real number! I don't know what else I can do :(

#include <stdio.h>
#include <math.h>
double squareroot(double number)
 {
  double x[20];
  int count,count2;
  x[0] = 1;
  for (count = 0;count <= 20;count++)
   {
    while ((abs(x[(count2)-1]) - x[(count2)]) > 0.001)
     {
      x[(count2)+1] = (((x+number/x))/2);
      return x[(count2)+1];
     }
   }
 }

main()
{
getch();
}
s88
Newbie Poster
10 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

>> return x[(count2)+1];
the above is on the inside of the loop so the loop will only run once.

>>for (count = 0;count <= 20;count++)
this loop will count one too many times. the array only has 20 elements but the loop will run 21 times.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I arranged the code like this, but there is an error in line 15 invalid operands to binary /

#include <stdio.h>
#include <math.h>
float squareroot(float number)
 {
  float x[20];
  int count,count2;
  x[0] = 1;
  for (count = 0;count <= 19;count++)
   {
    while ((abs(x[(count2)-1]) - x[(count2)]) > 0.001)
     {
      x[(count2)+1] = (((x+(number/x)))/2);
     }
   }
  return x[(count2)+1];
 }

main()
{
float num =0;
printf("Enter a number : ");
scanf("%f",num);
printf("%f",squareroot(num));
getch();
}
s88
Newbie Poster
10 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

>>return x[(count2)+1];
this is wrong. When the loop ends, the value of count2 will be 20. x[21] is referencing too nonexistant elements of the array.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

:/ i'm really lost with this one, made the following arrangements but nothing works:

#include <stdio.h>
#include <math.h>
float squareroot(float number)
 {
  float x[20];
  int count;
  int count2 = 1;
  x[0] = 1;
  x[1] = (x+(number/x))/2;
  for (count = 0;count <= 19;count++)
   {
    while ((abs(x[(count2)-1]) - x[(count2)]) > 0.001)
     {
      x[(count2)+1] = ((x+(number/x)) /2);
      count2++;
     }
   }
  return x[(count2)+1];
 }

main()
{
float num =0;
printf("Enter a number : ");
scanf("%f",num);
printf("%f",squareroot(num));
getch();
}
s88
Newbie Poster
10 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

that error was solved, still got one though, but i'll try to solve it first, thanks for your help!

s88
Newbie Poster
10 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

you can use recursion to ensure within the suitable times you can get the right number

gxm
Newbie Poster
1 post since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You