Your assignment is to write a c++ function that can calculate values for any 5 th order polynomial function. ?(?) = ?0 + ?1? + ?2? 2+?3? 3 + ?4? 5 + ?5? 5
Your function should accept only two parameters, the first parameter should be the value of x at which to calculate y(x).
The second parameter should be an array with 6 elements that contain the coefficients a0 to a5.
The output of your function should be the value y(x) for any value of the input parameter x.
The proper way to use an array as a parameter to a function is illustrated in the following example: double getAverage(int arr[], int size) { int i, sum = 0; double avg; for (i = 0; i < size; ++i) { sum += arr[i]; } avg = double(sum) / size; return avg; }
Once you have written your function write a program that includes your function to accomplish the following:
1. Ask the user to input the numbers for the coefficients of a fifth order polynomial. They should input them one at a time. Make sure the user understands the order in which they need to input the numbers.
2. Use your function within a while loop to calculate the values of y(x) starting at x=1 and ending at x=3, in steps of 0.5. Store these numbers in an array named “poly” of the appropriate size.
3. Use a for loop to output the results of your calculations to the screen (This must be done as a separate loop after all the calculations are complete). Format your output as follows: (output all numbers with 5 decimal places) Sample Output: x = 1.20000, y = -0.61600 repeat this for each value calculated, each on its own line.
The code is given below
#include <iostream>
using namespace std;
double func(double arr[],double x){
double val=arr[0],x1=x;
for(int i=1;i<=5;i++){
val+=arr[i]*x;
x=x*x1;
}
return val;
}
int main() {
cout<<"Enter polynomial coefficient from lower degree to high
degree.\n";
double arr[6];
for(int i=0;i<6;i++)cin>>arr[i];
cout<<fixed;
double x=1;
int i=0;
double poly[5];
while(x<3.5){
poly[i]=func(arr,x);
x+=0.5;i++;
}
x=1;
for(int i=0;i<5;i++){
cout<<"x = "<<x<<", y = "<<poly[i];
cout<<"\n";
x+=0.5;
}
}
The output is
Get Answers For Free
Most questions answered within 1 hours.