Question: I need someone to fix my code to solve exactly what the problem asks to. My code does not solve it correctly. Write a function
I need someone to fix my code to solve exactly what the problem asks to. My code does not solve it correctly.
Write a function that computes the square root of a number. The square root of a number x can be approximately computed as follows. First guess that the square root of xis1. Then repeatedly get the next guess from the last guess using the rulenext= 0.5(last+ x/last) where last is the last guess and next is the next guess. Repeat ten times using a loop and the tenth guess will be approximately the square root. The function prototype is double compute_sqrt(double x). The function computes the square root of x and returns the square root.
This is my code.
double compute_sqrt(double x) {
// 2. This function computes the square root of a number.
int i;
double next;
double last=1;
// loop 10 times
for(i=1;i<=10;i++) {
//next guess
next=0.5 *(last + x/last);
//assign the new guess to last
last=next;
}
//return last guess
return last;
}
int main(){
while(n!=1) {
double n;
double k;
//enter an number
printf("Enter a positive integer: ");
scanf("%lf", &n);
//call to the compute_sqrt function
k= compute_sqrt(n);
//diplay
printf("Square root is %lf ", k);
}
printf(" Exiting program. ");
break;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
