Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. /**
  2. This program computes the square root of a number to the nearest ten billionths place.
  3. CSC 1253 Project #2
  4. @author Andrew Armbrust
  5. @since
  6. File: SquareRootFinder.cpp
  7. Instructor: Dr. Duncan
  8. */
  9.  
  10.  
  11. #include <iostream>
  12. #include <cmath>
  13. #include <iomanip>
  14. using namespace std;
  15.  
  16. int main()
  17. {
  18.  
  19. double mid, Epsilon,low, high, numb;
  20. double squareRoot;
  21.  
  22. cout<< "Enter a number to find its square root ->";
  23. cin >> numb;
  24. Epsilon= 1E-10;
  25. if( numb < 0 )
  26. {
  27. cout<< " sqrt" <<"(" << numb<< ") Nan ";
  28. }
  29.  
  30. else if (numb <= 1)
  31. {
  32. low = 0.0000000000000;
  33. high = 1.000000000000;
  34. mid = (low + high)/2;
  35. cout << setprecision(8.0) << low << '\n';
  36. cout<<setprecision(8)<<high<<endl;
  37. while (mid *mid - numb > Epsilon){
  38. if ((mid * mid - numb) < numb)
  39. low = mid;
  40. else
  41. {
  42. high = mid;
  43. mid = (low + high)/2;
  44. squareRoot=mid;
  45. }
  46. }
  47. cout<<setprecision(8)<<squareRoot;
  48. }
  49.  
  50. else if (numb > 1)
  51. {
  52. low = 1;
  53. high = numb;
  54. mid = (low + high)/2;
  55. cout<<low;
  56. cout<<high;
  57. while (mid *mid - numb > Epsilon){
  58. if ((mid * mid - numb) < 0)
  59. low = mid;
  60. else
  61. {
  62. high = mid;
  63. mid = (low + high)/2;
  64. squareRoot=mid;
  65. }
  66. }
  67. cout<<squareRoot;
  68. }
  69.  
  70.  
  71.  
  72.  
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement