i have this asignment due soon but i can only get the absolute value part right i have no clue how to get the square root with bisection please help. here is the assignment and what i have done.
Requirements:
− You can only use the iostream library. No other libraries are allowed.
− You must format your code according to the programming standards used in this course. If
the programming standards are not followed, some marks will be deducted even if the code
is working correctly.
− You must submit your source code (lab4.cpp) via webct.
Lab 4
− Write a C++ function called myAbs that accepts a number (double) and returns the absolute
value of the given number.
− Write a C++ function called mySqrt that accepts a positive number (double) and returns the
square root of the given number. This function must implement the bisection algorithm for
computing square roots (discussed in class). The iteration must stop when the error is less
than or equal to 0.00001. This function then returns the last approximation of the square
root. If the mySqrt function is given an invalid argument (e.g., a negative number), return
0xffffffff. This is a convention that indicates the result is not a number.
− Put both myAbs and mySqrt into lab4.cpp and use the following main function to test your
functions. Add required comments (e.g., name, student number, etc.) and other C++
statements (e.g., #include, function declarations, etc.) to make your code work correctly.
Code:
int main()
{
cout << "Please enter a positive number: ";
double number;
cin >> number;
cout << "mySqrt(" << number << "): " << mySqrt(number) << endl;
double num = 5.5;
cout << "myAbs(" << num << "): " << myAbs(num) << endl;
cout << "myAbs(" << -num << "): " << myAbs(-num) << endl;
return 0;
}
This is what i have so far
Code:
#include <iostream>
using namespace std;
double myAbs(double num);
double mySqrt(double num);
int main()
{
cout << "Please enter a positive number: ";
double num;
cin >> num;
double number = num;
cout << "mySqrt(" << number << "): " << mySqrt(number) << endl;
cout << "myAbs(" << num << "): " << myAbs(num) << endl;
cout << "myAbs(" << -num << "): " << myAbs(-num) << endl;
return 0;
}
double myAbs(double num)
{
if(num > 0)
{
num = num;
}
if(num<0)
{ num = -num;
}
return (num);
}
double mySqrt(double number)
{
double x = 1;
double y = x;
do
x=.5*(x-(number/x));
while (-.00001 < x - y < .00001);
return number;
}