Lab 5

Description:

This week, we are Creating a C++ program the will approximate the square root of a number using the Babylonian Algorithm explained below. Display partial results as you approximate the square root. After approximating the square root up to the tolerance, compare it with the result obtained by using the sqrt( ) function in the math.h library.


#include 

using namespace std;

float tolerance = 0.00001f;

float mySqrt(float x)
{
	float l = x / 2;
	float c = x / 2;

	do
	{
		l = c;
		c = (l + x / l) * 0.5f;
	}while(l-c > tolerance);

	return c; 
}

int main(int argc, char** argv)
{
	float x;
	bool exit = false;
	while(!exit)
	{
		cout <> x;
		cout << "Result:" << endl;
		cout << "Babylonian Algorithm: " << mySqrt(x) << endl;
		cout << "C Lib Sqrt: " << sqrtf(x) << endl;
	}

	return 0;
}


hi

Leave a Reply

Your email address will not be published. Required fields are marked *