Ok, according to one of the TAs, this should be a simple program to type but I'm having trouble finishing it and getting it right.
The problem is to find the approximated square root of a number N by repeated calculations using the formula NG=0.5*(LG + N/LG) where NG=Next Guess and LG=Last Guess
LG starts at 1.0
After inputting a number N into the formula above, I have to get it to check if the difference between NG and LG is 0.005 in which NG is accepted as a square root but if not, the next guess becomes the last guess which I assume means LG=NG. This is when I repeat the loop.
The only problem I have is in what to put in the while brackets. Other than that, it'll give me the correct answer but of course it loops forever.
I have a feeling that I might have to change the coding around but I have no idea what.
Ideas?
Code: Select all
#include "stdio.h"
#include "math.h"
void main()
{float N, NG, LG, x;
LG=1;
printf("Type in a number\n");
scanf("%f", &N);
/*I have no idea of what to put in while() which won't cause the identifier to become without a value*/
while(){
NG=0.5*(LG+N/LG);
if (abs(NG-LG)<= 0.005)
printf("%f is a squareroot of N\n", NG);
else LG=NG;}
x=sqrt(N);
printf("%f is the intrinsic absolute value of the number\n", x);
}