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

Definea new function called mySqrt with one formal parameter defined in the para

ID: 3751656 • Letter: D

Question

Definea new function called mySqrt with one formal parameter defined in the parameter list. The parameter represents the number of which you will find the square root, you can call it n if you want.Include the input, output, and process below as docstrings in your function. You can use the parameter names provided above or pick your own. I listed them so that it would be easier to reference them in the text below. Newton’s approach is an iterative guessing algorithm where the initial guess is n/2 and each subsequent guess is computed using the formula: newguess = (1/2) * (oldguess + (n/oldguess)).

1. Write the code to calculatethe initial guess andassignit to a variable called square_root.

2. Setup your for-loop header. As the number of times you iterate increases, the closer your approximation. I iterated 100 times in my solution. 2(a). Calculate the subsequent guesses using the formula: (1/2) * oldguess + (n/ oldguess). Use the update pattern with the square_root variable to store the values.

3. Return the square root.

Explanation / Answer

Program in C language

#include <stdio.h>
#include <stdlib.h>

//function to calculate square root
//input: float n
//output: float square_root
float mySqrt(float n){
float square_root;
float initial_guess = n/2; //initial guess
square_root = initial_guess;
  
int iterations = 100;
for(int i=0;i<iterations;i++){
float new_guess = 0.5*(square_root + (n/square_root));//square_root is old_guess
square_root = new_guess;
}
return square_root;
}

int main() {
//input
float n;
printf("Enter a number: ");
scanf("%f",&n);
  
//output
float sqrt = mySqrt(n);
printf(" The square root of a number %f is: %f",n,sqrt);
return 0;
}

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