#include <iostream>
#include <conio.h>
using namespace std;

main () {

// This program calculates the value of a polinomial together with its
// derivative at a given point, using the Horner's algorithm.
// Written by Yamac Pehlivan.
 
    cout << "Hello. This program calculates the value of a given polynomial together with" 
    << " its derivative at a given point." 
         << endl
         << endl
         << "This program uses the Horner's algorithm" 
         << endl 
         << endl;
         
    cout << "Please enter the degree of the polynomial" 
         << endl
         << endl;
    int n;
    cout << "n=" ;
    cin >> n;                
     
    cout << endl
         << endl;                                           
                                                                                               
    float a[n]  ;
    
    cout << "Please enter the coefficents of the polynomial starting from the coefficient" 
         << " of the highest degree term." 
         << endl
         << endl;
         
// Take the coefficients of the polynomial.    
   
    int k; 
    for (k=0; k<n+1; k++)
  {
    cout << "a[" << k<< "]=" ;
    cin >> a[k] ;
      } 

// Confirm the polynomial.

    cout << "The polynomial is" 
         << endl
         << endl; 
         
    cout << "P(x)=" ;
    
    for (k=0; k<n; k++)
  { 
   cout << "("<< a[k] << ")x^"<<n-k << "+"; 
   }
   cout << "(" << a[n] << ")";
   
   cout << endl 
         << endl;


// Take the point at which the polynomial is to be calculated.
    
    cout << "Please enter a value for the variable:" 
         << endl
         << endl;
    cout << "x="; 
    float z ;
    cin >> z ;
    
    cout << endl
         <<endl;

// Calculate the polinomial.
// Calculate the b[i]:         
    int i;
    float b[n];
    b[0]=a[0];
  for (i=1; i<n+1; i++)
  {
    b[i]=b[i-1]*z + a[i] ;
  }
  
  
// If the variables b[i] are not to be printed comment this part.  
  for (i=0; i<n+1; i++)
  {
    cout << "b[" << i << "]=" << b[i] << ", ";
  }
  
    cout << endl 
         << endl;

    cout << "Press any key to continue." 
         << endl
         << endl;
    getch();
// End of printing the variables b[i]. 

// Print the value of the polynomial at the given point.

    cout << "P(" << z << ")=" << b[n]  
         << endl
         << endl;



    cout << "Press any key to continue." 
         << endl
         << endl;
    getch();
    
// Calculate the first derivative.  
// Calculate the c[i]:         
    
    float c[n];
    c[0]=b[0];
  for (i=1; i<n; i++)
  {
    c[i]=c[i-1]*z + b[i] ;
  }
  
  
// If the variables c[i] are not to be printed comment this part.  
  for (i=0; i<n; i++)
  {
    cout << "c[" << i << "]=" << c[i] << ", ";
  }
  
    cout << endl 
         << endl;

    cout << "Press any key to continue." 
         << endl
         << endl;
    getch();
// End of printing the variables c[i]. 

// Print the value of the derivative at the given point.

    cout << "P'(" << z << ")=" << c[n-1]  
         << endl
         << endl;


         
// End the prgram.
    cout << "Press any key to exit." 
         << endl
         << endl;
    getch();         
}   
