Show Polynomial

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:

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


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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  #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.

1
2
3
4
5
6
int main()
{

getpolynomial();

}


Can Somebody help me with this small struggle that i am facing right now?
Last edited on
Topic archived. No new replies allowed.