- Engineering
- Computer Science
- c programming guide let number be the number to get...
Question: c programming guide let number be the number to get...
Question details
C programming
Guide:
Let number be the number to get the square root of
Start with an estimate of 1.
Recompute estimate = 0.5 * (estimate + number/estimate)
Go back to 3
Stop the loop when one of these occurs:
1) (estimate*estimate) == number
2) The number of iterations gets to 100, the maximum
Copy & Paste the following C babyloniansquare.cfile:
#include <stdlib.h>
#include <stdio.h>
const int TEXT_LEN = 64;
void obtainFloat (float* fPtr
)
{
char text[TEXT_LEN];
// YOUR CODE HERE
}
float squareRoot (float number,
int maxIters,
int* numItersPtr
)
{
float estimate = 1.0;
// YOUR CODE HERE
return(estimate);
}
int main ()
{
float f;
float ans;
int numIters = 0;
obtainFloat(&f);
ans = squareRoot(f,100,&numIters);
printf("squareRoot(%g) approx. equals %g (found in %d iterations).\n",
f,ans,numIters
);
return(EXIT_SUCCESS);
}
Finish obtainFloat():
It should ask the user to enter a number from 0 to 65535, and lets the user enter a number. If user enters a number outside that range, it should ask again. The number is not returned, but number is placed in the address to which fPtr points.
Finish squareRoot():
After setting estimate to 1.0, it should loop and recompute estimate with the expression above. Every time it loops, it should increment the integer to which numItersPtrpoints. The loop should stop when the condition given above is met.
Make sure to show screenshot of results.
Solution by an expert tutor
