Given an array A a0 a1 a2 an1 of integers let pA be the fol

Given an array A = [a_0, a_1, a_2, ...a_n-1] of integers, let p_A be the following polynomial: a_n-1 x^1 - 1 + a_n-2x^n-2 + ... + a_2 x^2 + a_1 x + a_0 Give an algorithm that gets an array A and an integer t as inputs and outputs PA(t) (value of the polynomial at t). Derive the worst-case run-time of your algorithm (as a function of the size of the input array). Your grade will be inversionally proportional to the run time of your algorithm

Solution

#include <bits/stdc++.h>
#define max 10
using namespace std;

int main()
{
int ar[max];
int i, num, power;
float x, result;

cout<<\"Enter the order of the polynomial \ \";
cin>>num;
cout<<\"Enter the value of x \ \";
cin>> x;
cout<<\"Enter \" <<num + 1<< \"coefficients \ \";
for (i = 0; i <= num; i++)
{
cin>>ar[i];
}
result = ar[0];
for (i = 1; i <= num; i++)
{
result = result * x + ar[i];
}
power = num;

cout<<\"Polynomial is: \ \";
for (i = 0; i <= num; i++)
{
if (power < 0)
{
break;
}

if (ar[i] > 0)
cout<<\" + \";
else if (ar[i] < 0)
cout<<\" - \";
else
cout<<\" \";
cout<<abs(ar[i])<<\"x^\" <<(power--);
}
cout<<\"\ Value of the polynomial = \"<<result;
}

============================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ g++ polynomial.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter the order of the polynomial
2
Enter the value of x
2
Enter 3coefficients
3
2
6
Polynomial is:
+ 3x^2 + 2x^1 + 6x^0
Value of the polynomial = 22akshay@akshay-Inspiron-3537:~/Chegg$ subl polynomial.cpp

 Given an array A = [a_0, a_1, a_2, ...a_n-1] of integers, let p_A be the following polynomial: a_n-1 x^1 - 1 + a_n-2x^n-2 + ... + a_2 x^2 + a_1 x + a_0 Give an
 Given an array A = [a_0, a_1, a_2, ...a_n-1] of integers, let p_A be the following polynomial: a_n-1 x^1 - 1 + a_n-2x^n-2 + ... + a_2 x^2 + a_1 x + a_0 Give an

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site