first problem:
I am writing a program that lets the user input the power and the corresponding constant coefficients of a polynomial function, then the program should output the users mathematical function.
for example:
[output]
Enter the order of the polynomial: 2
Enter the coefficient of x^2: 1
Enter the coefficient of x^1: -5
Enter the constant term: 2
Your function is f(x) = x^2-5x+2
[/output]
I have my code below but I encountered a small problem. There is still "+" that will come up after the constant term. And if possible, i want to ouput the function as what is shown above.(i,i., if the power is 1, just put 'x' and if the power is 0 just put the constant coefficient.
Your function is f(x) = x^2-5x^1+2x^0+
How would i fix this?
#include <iostream>
using namespace std;
int main()
{
double coeff[1000];
int p;
cout << "Enter the power of the equation: ";
cin >> p;
for(int i = p; i > 0; i--)
{
cout << "Enter the coefficient of x^" << i << ": ";
cin >> coeff[i];
}
cout << "Enter the constant term: ";
cin >> coeff[0];
cout << "Your function is: f(x) = ";
for(int i = p; i >= 0; i--)
{
cout << coeff[i] << "x^" << i;
if(coeff[i-1] > 0) cout << "+";
}
cin.get();
cin.get();
return 0;
}
second problem:
I want to make this whole thing as a function so that i will just call it whenever i need it.
int main()
{
getpolynomial();
}
Can Somebody help me with this small struggle that i am facing right now?