Below is the code that you can modify to solve this problem #include <stdio.h> d
ID: 3727401 • Letter: B
Question
Below is the code that you can modify to solve this problem
#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;
}
Explanation / Answer
Figuring out the easiest and simplest solution.
Modifying the evaluate function with two inputs of arrays and two sizes. i.e:
double evaluate(double p1[], double p2[], double x, int size1, int size2)
{ double total = 0;
int i = 0;
double exp = 1;
while(i < size1) {
total += p1[i]*exp;
exp *= x;
i++;
}
i=0;
exp=1;
while(i < size2) {
total += p2[i]*exp;
exp *= x;
i++;
}
return total;
}
Largest degree of P6 will be size1+ size2-2; and P7 will be 2*size3-2;
Now, in main program , take the input of three arrays and provide them in the given evaluate function for the respective array and sizes.
All the best,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.