Question: Can someone help me with this c++ program? I am trying to get this program to output a certain answer and it is not working....
Can someone help me with this c++ program? I am trying to get this program to output a certain answer and it is not working.... I would appreciate if you could re-write the program for this output. I am trying to output the 5.34914 and 5.01139 alongside the 12.5 and 7.25.
I am trying to achieve this output, *this is the ideal/goal output

but instead, I am getting this one, *this is the wrong output

This is the program
#include
#include
double genNewGuess(double n, double oldGuess);
int main() {
double n, oldGuess, newGuess;
// Prompt user for input
std::cout
std::cin >> n;
// Validate input
if (n
std::cout
return 0;
}
// Initialize guess to n/2
oldGuess = n / 2.0;
std::cout
// Iterate through algorithm until stopping criterion is met
do {
newGuess = genNewGuess(n, oldGuess);
std::cout
oldGuess = newGuess;
} while (abs(newGuess - oldGuess) / oldGuess > 0.01); // Stopping criterion
// Output results
std::cout
return 0;
}
// Function to generate new guess
double genNewGuess(double n, double oldGuess) {
return (oldGuess + n / oldGuess) / 2.0;
}
Enter the Number to find square root: 25 Guessing... 12.5 7.25 5.34914 5.01139 The Final Guess: 5.00001 Actual Value: 5 Enter the Number to find square root: 2.3 Please enter a valid input(Positive Integer). Enter the Number to find square root: 25 Guessing... 12.5 7.25 Square root of 25:5
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
