944,117 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5357
  • C RSS
Nov 14th, 2006
0

Newton's Square Root Function

Expand Post »
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. }
Similar Threads
s88
Reputation Points: 10
Solved Threads: 0
Newbie Poster
s88 is offline Offline
10 posts
since Oct 2006
Nov 14th, 2006
0

Re: Newton's Square Root Function

>> 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 14th, 2006
0

Re: Newton's Square Root Function

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. }
s88
Reputation Points: 10
Solved Threads: 0
Newbie Poster
s88 is offline Offline
10 posts
since Oct 2006
Nov 14th, 2006
0

Re: Newton's Square Root Function

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 14th, 2006
0

Re: Newton's Square Root Function

:/ 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. }
s88
Reputation Points: 10
Solved Threads: 0
Newbie Poster
s88 is offline Offline
10 posts
since Oct 2006
Nov 14th, 2006
0

Re: Newton's Square Root Function

that error was solved, still got one though, but i'll try to solve it first, thanks for your help!
s88
Reputation Points: 10
Solved Threads: 0
Newbie Poster
s88 is offline Offline
10 posts
since Oct 2006
Mar 7th, 2008
0

Re: Newton's Square Root Function

you can use recursion to ensure within the suitable times you can get the right number
gxm
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gxm is offline Offline
1 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Array problem...
Next Thread in C Forum Timeline: Using fgets() properly





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC