#include <stdio.h> double evaluate(double p[], double x, int size) { double tota
ID: 3727911 • Letter: #
Question
#include <stdio.h>
double evaluate(double p[], double x, int size) {
double total = 0;
int i = 0;
double exp = 1;
while(i < size) {
total += p[i]*exp;
exp *= x;
i++;
}
return total;
}
int main() {
double arr[18];
int size, i, n;
printf("Enter degree of the polynomial: ");
scanf("%d", &n);
i = 0;
printf("Enter polynomial: ");
while(i < n+1) {
scanf("%lf", arr+i);
i++;
}
size = i;
printf("polynomial is %lf when x = 1 ", evaluate(arr, 1, size));
printf("polynomial is %lf when x = 3 ", evaluate(arr, 3, size));
printf("polynomial is %lf when x = -5 ", evaluate(arr, -5, size));
return 0;
}
Modify your program in 1 above (or write a new C program) to accept three polynomials as in problem 2. Now compute the polynomial P P, +P:+pg. Note that P(x)=-Py (Pi). Submit Similarly, (x) =Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <stdio.h>
double evaluate(double p[], double x, int size) {
double total = 0;
int i = 0;
double exp = 1;
while(i < size) {
total += p[i]*exp;
exp *= x;
i++;
}
return total;
}
//read a polynomial from user and store it in arr and return the degree
int inputPolynomial(double arr[])
{
int n;
printf("Enter degree of the polynomial: ");
scanf("%d", &n);
int i = 0;
printf("Enter polynomial co-efficients (start with constant term till highest degree term): ");
while(i < n+1) {
scanf("%lf", arr+i);
i++;
}
return n;
}
int derivative(double P[], int n, double Pdash[])
{
int i;
//calculate the coefficients of derivative
for(i = 1; i <= n; i++)
Pdash[i-1] = P[i] * i;
return n-1; //derivative is 1 less degree
}
void display(char *msg, double P[], int n)
{
int i ;
printf("%s", msg);
for(i = n; i >= 0; i--)
{
if(P[i] != 0) //display only non-zero coefficient terms
{
if(P[i] < 0)
printf(" - %.2lf", -P[i]);
else
printf(" + %.2lf", P[i]);
if(i != 0)
printf("x^%d", i);
}
}
printf(" ");
}
int main() {
double P1[18], P2[18], P3[18];
double P2dash[18], P3dashdash[18], temp[18];
double sum[18];
int n1, n2, n3;
int n4, n5, n6;
int i;
n1 = inputPolynomial(P1);
n2 = inputPolynomial(P2);
n3 = inputPolynomial(P3);
n4 = derivative(P2, n2, P2dash); //compute P2'
n5 = derivative(P3, n3, temp); //find P3' and store in temp
n5 = derivative(temp, n5, P3dashdash);//find P3" using P3'
//now sum up coefficients
for(i = 0; i <= n1 || i <= n4 || i <= n5; i++)
{
sum[i] = 0;
if(i <= n1)
sum[i] += P1[i];
if(i <= n4)
sum[i] += P2dash[i];
if(i <= n5)
sum[i] += P3dashdash[i];
}
n6 = i - 1; //degree of the sum polynomial
display("P1: ", P1, n1);
display("P2: ", P2, n2);
display("P3: ", P3, n3);
display("P2': ", P2dash, n4);
display("P3": ", P3dashdash, n5);
display("P1 + P2' + P3": ", sum, n6);
return 0;
}
-----output-------
Enter degree of the polynomial: 2
Enter polynomial co-efficients (start with constant term till highest degree term): 1 2 3
Enter degree of the polynomial: 3
Enter polynomial co-efficients (start with constant term till highest degree term): 4 5 6 7
Enter degree of the polynomial: 4
Enter polynomial co-efficients (start with constant term till highest degree term): 1 2 3 4 5
P1: + 3.00x^2 + 2.00x^1 + 1.00
P2: + 7.00x^3 + 6.00x^2 + 5.00x^1 + 4.00
P3: + 5.00x^4 + 4.00x^3 + 3.00x^2 + 2.00x^1 + 1.00
P2': + 21.00x^2 + 12.00x^1 + 5.00
P3": + 60.00x^2 + 24.00x^1 + 6.00
P1 + P2' + P3": + 84.00x^2 + 38.00x^1 + 12.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.