I did everything just like this output, but there is problem with my calculation
ID: 3625756 • Letter: I
Question
I did everything just like this output, but there is problem with my calculation and I do not know where it isMy output should be like this
Please enter a positive number and I will calculate the square root for you.
-140.2
Please enter a positive number and I will calculate the square root for you.
-65.9
Please enter a positive number and I will calculate the square root for you.
140.8
Please enter a small positive number for the tolerance such as 0.0001.
-0.00001
Please enter a small positive number for the tolerance such as 0.0001.
0.00001
26 iterations: The square root of 140.800000 is 11.865918
how I can get the number of iterations..?
this is my code:
#include<stdio.h>
double absValue( double x ) {
if ( x > 0) {
return x;
}
else if ( x < 0 ) {
return (0 - x);
}
}
double sqRoot(double n, double tol) {
double x;
absValue(x);
double tooHigh = n;
double tooLow = 0;
double guess = (n/2);
double times = 0;
x = ((guess * guess) - n);
if ((x) > tol ) {
if (x > 0) {
tooHigh = guess;
guess = ((tooHigh + tooLow)/2);
times = times + 1;
}
else if (x < 0) {
tooLow = guess;
guess = ((tooHigh + tooLow)/2);
times = times + 1;
}
}
else if (x < tol) {
printf("%lf", times);
return guess;
}
}
int main() {
double userInput, mySqRt, tol;
do {
puts("Please enter a positive number and I will calculate the square root for you.");
scanf("%lf", &userInput);
}while(userInput<=0);
do {
puts("Please enter a small positive number for the tolerance such as 0.0001.");
scanf("%lf", &tol);
}while(tol<=0);
mySqRt = sqRoot( userInput, tol ); // your job is to write the sqRoot function
printf("The square root of %f is %f ", userInput, mySqRt);
return 0;
}
Explanation / Answer
// WORKING PERFECTLY NOW ENJOY CHEERS............
#include<stdio.h>
double absValue( double x ) {
if ( x > 0) {
return x;
}
else if ( x < 0 ) {
return (0 - x);
}
}
double sqRoot(double n, double tol) {
double x;
absValue(x);
double tooHigh = n;
double tooLow = 0;
double guess = (n/2);
double times = 0;
x = ((guess * guess) - n);
while (absValue(x) > tol )
{
if (x > 0)
{
tooHigh = guess;
guess = ((tooHigh + tooLow)/2);
times = times + 1;
}
else if (x < 0)
{
tooLow = guess;
guess = ((tooHigh + tooLow)/2);
times = times + 1;
}
// printf("guess is %lf ",guess );
x = ((guess * guess) - n);
// printf("x is %lf ",x );
}
if (x < tol)
{
printf("%d iterations :",(int)times);
}
return guess;
}
int main() {
double userInput, mySqRt, tol;
do {
puts("Please enter a positive number and I will calculate the square root for you.");
scanf("%lf", &userInput);
}while(userInput<=0);
do {
puts("Please enter a small positive number for the tolerance such as 0.0001.");
scanf("%lf", &tol);
}while(tol<=0);
mySqRt = sqRoot( userInput, tol ); // your job is to write the sqRoot function
printf("The square root of %f is %f ", userInput, mySqRt);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.