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

C++ Programming Question: Prompt the user for a value to take the square root of

ID: 671991 • Letter: C

Question

C++ Programming Question:

Prompt the user for a value to take the square root of. If the user enters zero or a negative value, keep prompting and getting a value till valid. Implement both of the following methods:

A) The Bisectional Method (Use a do…while loop)

1) Initialize a variable called low to 0 and a variable called high to the number the user entered, x.

2) Compute mid as (high + low) / 2.0 .

3) Assume mid is the square root of the number the user entered, x, and compute the difference or error of current mid value. The equation to compute the error is given below.

error = x - mid * mid

4) If error is greater than zero then set low equal to mid. If error is less than zero set high equal to mid. Remember to increment an iteration counter for this method here.

5) If the absolute value of error is less than or equal to .00001 then consider the current mid value as close enough to be the square root of the value the user entered, x. Otherwise, loop to step 2.

6) Display the computed square root and the number of iterations it took to reach the solution.

B) Newton’s Method (Use a while loop)

1) Initialize a variable called guess to 1.

2) Compute change as (guess + x / guess) / 2.0 .

3) If the absolute value of (changeguess) is greater than .00001 set guess equal to change and loop to step two. Remember to increment an iteration counter for this method here. Otherwise, guess is close enough for the square root of x.

4) Display the computed square root, the number of iterations it took to reach the solution.

Explanation / Answer

Please find below, the program code as per the requirement.

#include <iostream>
#include <cmath>
using namespace std;

void srootBisectional(float x);
void srootNewton(float x);

int main() {
   float x;
  
   cout << "Enter a value to calculate the square root of: "<<endl;
   cin >> x;
  
   while ( x <= 0){
cout << "Invalid value, please enter a positive number: "<<endl;
cin >> x;
}
srootBisectional(x);
srootNewton(x);
   return 0;
}


void srootBisectional(float x){
   float low, high, error, mid;
   int i=0;
low = 0;
high = x;
  
do{
mid = (high + low)/2.0;
error = x - mid * mid;
  
if (error > 0) low = mid;
else if (error < 0) high = mid;
i++;
//if (abs(error) <= 0.00001) {cout<<"error is "<<error<<endl;}
}while(abs(error) > 0.00001);

cout<<"computed square root of "<< x <<" is "<<mid<<endl;
cout<<"Number of iterations to reach the solution using Bisctional method is "<<i<<endl;
}

void srootNewton(float x){
float guess, change;
int i=0;
guess = 1;
change = (guess + x / guess) / 2.0;
  
while (abs(change - guess) > 0.00001)
{
guess = change;
change = (guess + x / guess) / 2.0;
i++;
}
cout<<"computed square root of "<< x <<" is "<<guess<<endl;
cout<<"Number of iterations to reach the solution using Newton's method is "<<i<<endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote