| | |
Newton's Square Root Function
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 10
Reputation:
Solved Threads: 0
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 

C Syntax (Toggle Plain Text)
#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(); }
>> 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.
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.
•
•
Join Date: Oct 2006
Posts: 10
Reputation:
Solved Threads: 0
I arranged the code like this, but there is an error in line 15 invalid operands to binary /
C Syntax (Toggle Plain Text)
#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(); }
•
•
Join Date: Oct 2006
Posts: 10
Reputation:
Solved Threads: 0
:/ i'm really lost with this one, made the following arrangements but nothing works:
C Syntax (Toggle Plain Text)
#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(); }
![]() |
Similar Threads
- Square root program without sqrt or pwr (C++)
- simple problem with square root (C)
- Trying To Find The Square Root Of Each Number Of A Array (C++)
Other Threads in the C Forum
- Previous Thread: Array problem...
- Next Thread: Creating an Operating System like DOS
Views: 3772 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






