Question

Your assignment is to write a c++ function that can calculate values for any 5 th...

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
1) Develop a C++ function that determines the average value of an array of type double...
1) Develop a C++ function that determines the average value of an array of type double elements double GetAverage(double array[], int size) The function should accept as input an array of double values The function should accept as input the number of elements in the array of double values The function should return a double value which is the array's average value 2) Develop a C++ function that determines the variance of an array of type double elements double GetVariance(double...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
Instructions Write a Java code Your goal is to take N integer inputs from the user...
Instructions Write a Java code Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as input...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
Write a C++ program which consists of several functions besides the main() function. 1)   The main()...
Write a C++ program which consists of several functions besides the main() function. 1)   The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. 2)   A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. 3)   A value-returning function called Power(int a,...
Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array...
Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array of random numbers in the array. The function dynamically allocates an array, uses the system clock to seed the random number generator, populates the array with random values, and then returns a pointer to the array. Function getRandomNumbers to generate a random array and return a pointer. int* getRandomNumbers(int num); // The parameter indicates the number of numbers requested. The algorithm can be described...
1) Write a function in C that takes in a single integer parameter and returns an...
1) Write a function in C that takes in a single integer parameter and returns an array of factors. The function signature should be : int* get_factors(int number) To make things a little easier, assume that the input (number) will never have more than 100 factors. The returned array should be null-terminated. 2) Write a main() function that asks a user for a number, calls the function above, then prints the results.
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the traveling pointer notation to traverse the array. you have to ask the user for the size of the array and ask the user to input the values in the main. The function has the following prototype. int maximum ( int *p [ ], int n);
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT