Write a C program that includes a function named solve Poly (x) that computes th
ID: 3802860 • Letter: W
Question
Write a C program that includes a function named solve Poly (x) that computes the following polynomial at value x (a double): 3x^5 + 2x^4 - 5x^3 - x^2 + 7x - 6 Write a program that asks the user to enter a value for x, calls your solve Poly function to evaluate the polynomial passing it x, then displays the returned double value. Write a new function called solvePoly2 that is passed a double array of size 6 (that represents up to a 5^th degree polynomial) and a value for x and as before evaluates the polynomial at x. For example, the polynomial shown above would be represented like this: (i.e. the coefficient in the 0^th cell represents the x^0 coefficient, the coefficient in the 1^st cell represents the x^1 coefficient, etc.) Here is a second example: the array p here stores the polynomial 3x^3 - 2x + 4. Calling a function to compute the power of x raised to some value is a great help in writing this program; such a function is defined on page 363 in the notes or, if you prefer, you can use the function provided in the C libraries (via math. h).Explanation / Answer
Answer 1:-
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
double solvePoly(double x)
{
double res;
res=3*pow(x,5)+2*pow(x,4)-5*pow(x,3)-pow(x,2)+7*pow(x,1)-6;
return res;
}
int main()
{
double x;
double result;
printf("Enter the Value of X :");
scanf("%lf",&x);
result=solvePoly(x);
printf("The value of Polynomial Function is: %.2lf ",result);
return 0;
}
--------------
output sample:
Enter the Value of X :3
The value of Polynomial Function is: 762.00
--------------------------------------------------------------------------------------------------
Answer 2:-
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
double solvePoly2(double array[],double x)
{
int i;
for(i=0;i<6;i++)
printf("%.2lf ",array[i]);
double res;
res= (array[5]) *(x*x*x*x*x) + (array[4])*(x*x*x*x) + (array[3])*(x*x*x)+(array[2])*(x*x)+(array[1])*(x)+(array[0]);
return res;
}
int main()
{
double array[6]={4,-2,0,3,0,0};
double x;
int i;
double result=0;
printf("Enter the value of x :-");
scanf("%lf",&x);
printf(" x is %lf ",x);
result=solvePoly2(array,x);
printf(" The value of Polynomial Function is : %.2lf ",result);
}
-----------
output sample:-
Enter the value of x :-2
x is 2.000000
4.00 -2.00 0.00 3.00 0.00 0.00
The value of Polynomial Function is : 24.00
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.