We are given the task to create horner's rule by making three separate functions combined with a structure. The structure looks like this:
struct Polynomial
{
long n;
double* a;
};
Next I created a function that is supposed to take in the polynomial degree and coefficient:
Polynomial createPoly()
{
Polynomial poly;
std::cout << "Enter polynomial degree: ";
std::cin >> poly.n;
poly.a = new double[poly.n];
for (int i = 0; i < poly.n; i++)
{
std::cout << "The polynomial coeff x ^ " << i <<" = " << std::endl;
std::cin >> poly.a[i];
}
return poly;
}
Note that everything except the code written after cin>>poly.n; is mandatory. My question now is, what is my createPoly function exactly? It has the structure definition before and takes no values as function inputs. How would I be able to use poly.n and poly.a in other functions?
For example, I now have to create a function which calculates the polynomial through horners rule but I have no clue on how I would be able to combine it with my createPoly function.
double calcPoly(double x, const Polynom* poly); //calc poly defintion
Thanks in advance.
Your function createPoly
returns a Polynomial
object so you could write something like:
Polynomial p = createPoly();
double result = calcPoly(1.56, &p);
Basically, the createPoly
function returns an object that you can then set into a variable and use in your other functions like calcPoly
or any others.