[Date Prev][Date Next][Date Index]
[intv] Polynomial evaluation point using horner's rule.
//---------------main.cpp-----------
//Problem: Polynomial evaluation.
//Save as: main.cpp, compile as: g++ -o main main.cpp
#include <iostream>
using namespace std;
int horner(int *coeff, int size, int x){
int res = coeff[0];
for (int i = 1; i < size; i++){
res = res*x + coeff[i];
}
return res;
}
int main (){
//polynomial 3x^2 + 2x + 1
int a[3] = {3, 2, 1};
int x = 2; //evaluation point.
cout << "Results = " << horner(a,3,x) << endl;
int b[5] = {2, -1, 3, 1, -5};
x = 3;
cout << "Results = " << horner(b,5,x) << endl;
}
//---------------main.cpp-----------
Comments and corrections are appreciated and can be sent to
papers@mia.ece.uic.edu.
Click here for ©opyright information.
|