fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int main()
  6. {
  7. const double DIVISIOR = 2.0; //this represents how we come to our first "guess" basically the lazy (and wrong) way of finding a square root, taking the num
  8. //and dividing it by 2
  9. double num = 0.0; //number we want the square root of
  10. double guess = 0.0; //stores changing guesses as program runs
  11. double root = 0.0; //stores changing roots as program runs
  12. double finalAnswer = 0.0; //stores final answer to be displayed
  13. double numOfApprox = 1.0; //loop control variable so our loop doesn't continue forever
  14.  
  15.  
  16. //format numbers to display trailing decimal places
  17. cout.setf(ios::fixed); //I don't know what this is yet but the book says it will explain on chapter 8
  18. cout.setf(ios::showpoint); //this simply shows our decimal point
  19. cout.precision(3); //this is used to display a select number of places after the decimal
  20.  
  21. //prompt user for input
  22. cout << "Enter the number you want to find the square root of: \n"; //we first ask the user to enter the number they want the square root of
  23. cout << "\n"; //this is here so everything isn't bunched together and the screen is more readable for the user
  24. cin >> num; //the number the user wants the square root of is stored here and we'll be using this later
  25. cout << "\n"; //this is here so everything isn't bunched together and the screen is more readable for the user
  26.  
  27. //setup initial guess as a starting point
  28. guess = num / DIVISIOR; //we take the users number and divide it by DIVISOR which has a value of 2. This is good enough for an initial guess
  29.  
  30. //start looking for sq root
  31. while((guess <= (guess + guess*1.01)) && (guess >= (guess - guess*1.01)) && (numOfApprox <=10.0)) //continue searching for a more approximate value until either A) the new guess is near 1 percent greater
  32. //than the previous guess
  33. //B) the guess is near 1 percent less than the previous guess
  34. //C) we have guessed ten times, which is pretty approximate if you ask me
  35. {
  36. root = num / guess; //Now we take the number the user entered, divide it by our guess value
  37. //the quotient value is the value of our root, which we will use to aid us in future approximations
  38. guess = (guess + root) / DIVISIOR; //Now we take our previous guess and add that to the value of root we just obtained in the line before
  39. //the sum of guess and root is divided by DIVISIOR and the quotient of this expression is the value of our new guess
  40. numOfApprox = numOfApprox + 1.0; //we increment numOfApprox so our loop doesn't run on forever if our guess isn't near one percent greater or less than the previous guess
  41.  
  42.  
  43. }
  44.  
  45. finalAnswer = guess; //giving our output variable the value obtained from the previous loop so we can output this later
  46.  
  47. cout.precision(5); //this is changed and now outputs 5 decimal places so we display the final result more accurately than the input
  48.  
  49. //output the inputs and outputs here
  50. if(finalAnswer <= 0.0)
  51. {
  52. cout << "The number " << num << " has no real square root."; //if the number is 0 or less then the number doesn't have a REAL root
  53. cout << "\n"; //this is here so everything isn't bunched together and the screen is more readable for the user
  54. } //if
  55. else
  56. {
  57. cout << "The square root of " << num << " is " << finalAnswer << "." << endl; //the number the user entered is displayed along with the approximate square root of that number
  58. cout << "\n"; //this is here so everything isn't bunched together and the screen is more readable for the user
  59. } //else
  60.  
  61.  
  62.  
  63. system("pause"); // this keeps the console window from closing before we can see the output
  64. return 0; // return 0 to indicate successful execution of main()
  65. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:63:5: error: use of undeclared identifier 'system'
    system("pause");    // this keeps the console window from closing before we can see the output
    ^
1 error generated.
stdout
Standard output is empty