Newton's Square Root Function

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 10
Reputation: s88 is an unknown quantity at this point 
Solved Threads: 0
s88 s88 is offline Offline
Newbie Poster

Newton's Square Root Function

 
0
  #1
Nov 14th, 2006
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

  1. #include <stdio.h>
  2. #include <math.h>
  3. double squareroot(double number)
  4. {
  5. double x[20];
  6. int count,count2;
  7. x[0] = 1;
  8. for (count = 0;count <= 20;count++)
  9. {
  10. while ((abs(x[(count2)-1]) - x[(count2)]) > 0.001)
  11. {
  12. x[(count2)+1] = (((x+number/x))/2);
  13. return x[(count2)+1];
  14. }
  15. }
  16. }
  17.  
  18. main()
  19. {
  20. getch();
  21. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Newton's Square Root Function

 
0
  #2
Nov 14th, 2006
>> 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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 10
Reputation: s88 is an unknown quantity at this point 
Solved Threads: 0
s88 s88 is offline Offline
Newbie Poster

Re: Newton's Square Root Function

 
0
  #3
Nov 14th, 2006
I arranged the code like this, but there is an error in line 15 invalid operands to binary /

  1. #include <stdio.h>
  2. #include <math.h>
  3. float squareroot(float number)
  4. {
  5. float x[20];
  6. int count,count2;
  7. x[0] = 1;
  8. for (count = 0;count <= 19;count++)
  9. {
  10. while ((abs(x[(count2)-1]) - x[(count2)]) > 0.001)
  11. {
  12. x[(count2)+1] = (((x+(number/x)))/2);
  13. }
  14. }
  15. return x[(count2)+1];
  16. }
  17.  
  18. main()
  19. {
  20. float num =0;
  21. printf("Enter a number : ");
  22. scanf("%f",num);
  23. printf("%f",squareroot(num));
  24. getch();
  25. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Newton's Square Root Function

 
0
  #4
Nov 14th, 2006
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 10
Reputation: s88 is an unknown quantity at this point 
Solved Threads: 0
s88 s88 is offline Offline
Newbie Poster

Re: Newton's Square Root Function

 
0
  #5
Nov 14th, 2006
:/ i'm really lost with this one, made the following arrangements but nothing works:

  1. #include <stdio.h>
  2. #include <math.h>
  3. float squareroot(float number)
  4. {
  5. float x[20];
  6. int count;
  7. int count2 = 1;
  8. x[0] = 1;
  9. x[1] = (x+(number/x))/2;
  10. for (count = 0;count <= 19;count++)
  11. {
  12. while ((abs(x[(count2)-1]) - x[(count2)]) > 0.001)
  13. {
  14. x[(count2)+1] = ((x+(number/x)) /2);
  15. count2++;
  16. }
  17. }
  18. return x[(count2)+1];
  19. }
  20.  
  21. main()
  22. {
  23. float num =0;
  24. printf("Enter a number : ");
  25. scanf("%f",num);
  26. printf("%f",squareroot(num));
  27. getch();
  28. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 10
Reputation: s88 is an unknown quantity at this point 
Solved Threads: 0
s88 s88 is offline Offline
Newbie Poster

Re: Newton's Square Root Function

 
0
  #6
Nov 14th, 2006
that error was solved, still got one though, but i'll try to solve it first, thanks for your help!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: gxm is an unknown quantity at this point 
Solved Threads: 0
gxm gxm is offline Offline
Newbie Poster

Re: Newton's Square Root Function

 
0
  #7
Mar 7th, 2008
you can use recursion to ensure within the suitable times you can get the right number
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3772 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC