I'm looking for a few suggestions on different ways to input a polynomial into an array (coef[]) using a degree (double degree). I have one way that works, but my most recent project needs a new way to do the same thing (not sure why but it can't hurt to explore parts of the language that I don't know yet). Here's my current solution:

double coef[11];
int degree;

cout<<"Enter degree of the polynomial : ";
cin>>degree;
cout<<"Enter space separated coefficients starting from highest degree\n";
    for(int i=degree;i>=0;i--)
    cin>>coef[i];

What was the old way that you were using? I don't think there's anything wrong with the way you are reading input in this case. You could make the coef be a dynamically allocated array so it doesn't have to be set at 11.

I'm currently using the "for" statement to input each coefficient. I just found a website that showed me how to dynamically allocate the array, so I can do that.

Overall however, the guidelines state "code cannot be reused from projects 3 and 4", which is what I have here. Having said this, my current project is the introductory OOP project, so they may be saying that I should include the input of the coef into a member function rather than in main. Just a thought if there's no other efficient way of accomplishing this input.

Maybe you should have a class/struct that has the coefficient and the power as their properties (that way you can store just the expressions instead of having a 0 coefficient). You can then use the 'vector' array (search google for "vector c++"), which is a dynamic array, to store all the expressions in the polynomial. Eg.

struct Exp{ float Coefficient; int Power; };

class Polynomial{ vector <Exp> Exps; public: //your member functions... };[code = c++]
struct Exp{
float Coefficient;
int Power;
};

class Polynomial{
vector <Exp> Exps;
public:
//your member functions...
};

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.