C++ Bc. 6 cpp

Z GeoWikiCZ
(přesměrováno z C plus plus Bc. 6 cpp)
#include <iostream>
#include <iomanip> // manipulatory (setw, setprecision, etc.)
#include <cmath>

double rada (double x, int& iter, double rel_chyba = 1e-8);

int main ()
{
  using namespace std;

  double x, xstd;
  int pocet_iteraci;
  
  for ( double i = -1.0; i <= 1.0; i += 0.2)
    {
      x    = rada(i, pocet_iteraci);
      xstd = sqrt(1 + i);

      cout.setf(ios_base::fixed, ios_base::floatfield);
      // explicitne se uvadi znamenko '+'
      cout.setf(ios_base::showpos); 

      cout << setprecision(2) << setw ( 3) << i
	   << ' ' 
	   << setprecision(8) << setw (10) << x
	   << ' ' << xstd;

      cout.setf  (ios_base::scientific, ios_base::floatfield);
      cout.unsetf(ios_base::showpos);

      cout << ' ' << std::abs(x - xstd);

      cout.setf (ios_base::fixed);
      cout << ' ' << pocet_iteraci << endl;
    }
  
  return 0;
}

double rada ( double x , int& iter, double rel_chyba)
{
  double soucet = 1.0;
  double dif    = soucet;
  int    n      = 2;
  
  iter = 1;

  while ( std::abs(dif) > rel_chyba*std::abs(soucet) )
    {
      dif    *= -1.0 * ( (n - 3) * x ) / n ;
      n      += 2;
      soucet += dif;
      iter++;
    }

  return soucet;
}
-1.00 +0.00007979 +0.00000000 7.97884551e-05 50000002
-0.80 +0.44721361 +0.44721360 1.47877751e-08 55
-0.60 +0.63245554 +0.63245553 4.84481855e-09 27
-0.40 +0.77459667 +0.77459667 3.05723658e-09 16
-0.20 +0.89442719 +0.89442719 1.14852150e-09 10
-0.00 +1.00000000 +1.00000000 0.00000000e+00 2
+0.20 +1.09544512 +1.09544512 8.09980305e-10 10
+0.40 +1.18321596 +1.18321596 1.42135703e-09 16
+0.60 +1.26491106 +1.26491106 4.16274282e-09 25
+0.80 +1.34164078 +1.34164079 5.01443687e-09 51
+1.00 +1.41421357 +1.41421356 7.07096559e-09 73552

[ Zpět ]