Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

modify the program so that the value of (guess) is printed each time though the

ID: 3600182 • Letter: M

Question

modify the program so that the value of (guess) is printed each time though the while loop.

#include 5 float absolutevalue (float x) if (x 0) return (x); 2 float squareroot (float x) const float epsilon = 0.00001; float guess = 1.0; while (absolutevalue ( guess * guess -x)>= epsilon) guess = (x/ guess + guess)/ 2.0; return guess; 2 int main (void) printf("squareroot printf("squareroot printf("squareroot (2.0) %f ", squareroot (2.0)); (144.0) = %f ", squareroot (144.0)); (17.5) -%f ", squareroot (17.5)); return 0

Explanation / Answer

#include<stdio.h>

float absolutevalue(float x){
   if(x<0)
       x = -x;
   return(x);
}

float squareroot(float x){
   const float epsilon = 0.00001;
   float guess = 1.0;
   while(absolutevalue(guess*guess-x)>=epsilon){
       guess = (x/guess + guess)/2.0;
       //printing guess value
       printf("%f ",guess);
   }
   return guess;
}

int main(void){
   printf("squareroot (2.0) = %f ",squareroot(2.0));
  
   printf("squareroot (144.0) = %f ",squareroot(144.0));
   printf("squareroot (17.5) = %f ",squareroot(17.5));  
   return 0;
}

/*
sample output
1.500000
1.416667
1.414216
squareroot (2.0) = 1.414216
72.500000
37.243103
20.554796
13.780231
12.114992
12.000546
12.000000
squareroot (144.0) = 12.000000
9.250000
5.570946
4.356122
4.186728
4.183301
4.183300
squareroot (17.5) = 4.183300
*/