//=============================================================================

#include <iostream>
#include <iomanip>
using namespace std;

constexpr double epsilon = 0.0001;

//-----------------------------------------------------------------------------

int main()
{
   cout << "Aufg_08_05_Quadratwurzel_1\n\n";

   cout << "Wurzel-Berechnung fuer Zahlen groesser-gleich 1.0\n\n";

   cout << "Zahl: ";
   double v;
   cin >> v;
   if (cin.fail() || v < 1.0)
   {
      cout << "Fehleingabe\n";
      return 0;
   }

   cout << "Berechne Wurzel aus " << v << " mit einem Epsilon von " << epsilon << '\n';

   int count = 0;

   double lower_limit = 1.0;
   double upper_limit = v;
   double average;
   for (;;)
   {
      cout << "- Durchlauf " << setw(2) << ++count << ": "
         << lower_limit << " < sqrt(" << v << ") < " << upper_limit << '\n';
      average = (lower_limit + upper_limit) / 2;
      double av2 = average * average;
      if ((av2 - epsilon < v) && (av2 + epsilon > v)) break;
      if (v < av2)
      {
         upper_limit = average;
         continue;
      }
      lower_limit = average;
   }

   cout << "Wurzel: " << average << '\n';
}
