C++ Bc. 13 cpp: Porovnání verzí

Z GeoWikiCZ
Skočit na navigaci Skočit na vyhledávání
 
Řádek 1: Řádek 1:
 +
#include <iostream>
 +
#include <vector>
 +
 +
double polynom(double x, const std::vector<double>& a);
 +
 +
int main()
 +
{
 +
  using namespace std;
 +
 +
  vector<double> p;
 +
  p.push_back(9);
 +
  p.push_back(2);
 +
  p.push_back(5);
 +
  p.push_back(2);
 +
  p.push_back(7);
 +
 +
  for (double x=0; x<1.05; x+=0.1)
 +
    {
 +
      cout << x << "\t" << polynom(x, p) << endl;
 +
    }
 +
}
 +
 +
 +
double polynom(double x, const std::vector<double>& a)
 +
{
 +
  int    n = a.size();
 +
  double h = a[--n];
 +
  do
 +
    {
 +
      h *= x;
 +
      h += a[--n];
 +
    } while (n);
 +
 +
  return h;
 +
}
 +
 +
0      9
 +
0.1    9.2527
 +
0.2    9.6272
 +
0.3    10.1607
 +
0.4    10.9072
 +
0.5    11.9375
 +
0.6    13.3392
 +
0.7    15.2167
 +
0.8    17.6912
 +
0.9    20.9007
 +
1      25
 +
 
[ [[C plus plus Bc. 13|Zpět]] ]
 
[ [[C plus plus Bc. 13|Zpět]] ]

Verze z 11. 3. 2006, 23:31

#include <iostream>
#include <vector>

double polynom(double x, const std::vector<double>& a);

int main()
{
  using namespace std;

  vector<double> p;
  p.push_back(9);
  p.push_back(2);
  p.push_back(5);
  p.push_back(2);
  p.push_back(7);

  for (double x=0; x<1.05; x+=0.1)
    {
      cout << x << "\t" << polynom(x, p) << endl;
    }
}


double polynom(double x, const std::vector<double>& a)
{
  int    n = a.size();
  double h = a[--n];
  do 
    {
      h *= x;
      h += a[--n];
    } while (n);

  return h;
}
0      9
0.1    9.2527
0.2    9.6272
0.3    10.1607
0.4    10.9072
0.5    11.9375
0.6    13.3392
0.7    15.2167
0.8    17.6912
0.9    20.9007
1      25

[ Zpět ]