0

The following program builds perfectly. However, during execution, no matter what value of degree I provide, the program takes only 2 array elements as input. I suppose there might be a problem with the redeclaration of the arrays f[] and fDash[]. In JAVA, arrays can be easily redeclared using the new keyword. Is that possible in c++ too? If not, what is the alternative? P.S. I am using CodeBlocks 13.12 and compiler settings are standard.

#include <iostream>
#include <cmath>
using namespace std;
class Polynomial
{
    public:
        void input(void);
        void expression(void);
        void derivative(void);
        double value(double var);
        double der(double var);

    private:
        int f[];
        int fDash[];
        int degree;
};

void Polynomial::input()
{
   cout<<"Enter degree of polynomial:\t";
   cin>>degree;
   f[degree+1];
   fDash[degree];
   for(int i=0;i<=degree;i++)
   {
       cout<<"Enter coefficient of x^"<<i<<":\t";
       cin>>f[i];
   }
   for(int i=0;i<degree;i++)
   {
       fDash[i]=f[i+1]*(i+1);
   }
}
void Polynomial::expression()
{
    cout<<f[0];
    for(int i=1;i<=degree;i++)
    {
        cout<<" + "<<f[i]<<"*x^"<<i;
    }
}
void Polynomial::derivative()
{
    cout<<fDash[0];
    for(int i=1;i<degree;i++)
    {
        cout<<" + "<<fDash[i]<<"*x^"<<i;
    }
}
double Polynomial::value(double var)
{
    double val=0.0;
    for(int i=0;i<=degree;i++)
    {
        val+=f[i]*pow(var,i);
    }
    return val;
}
double Polynomial::der(double var)
{
    double val=0.0;
    for(int i=0;i<degree;i++)
    {
        val+=fDash[i]*pow(var,i);
    }
    return val;
}
int main()
{
   double lb,ub,step,var,accum=0.0,rms;
   int counter=0;

   Polynomial p;
   p.input();
   cout<<"\n\n\nPolynomial is:\nf(x) = ";
   p.expression();
   cout<<"\n\n\nDerivative is:\nf'(x) = ";
   p.derivative();
   cout<<"\n\n\nEnter x0,x1,Step:\t";
   cin>>lb;
   cin>>ub;
   cin>>step;
   cout<<"\n\n\n====================================";
   cout<<"\n\nx\t|\tf\t|\tf'\n\n\n";

   var=lb;
   while(var<=ub)
   {
        cout<<var<<"\t|\t"<<p.value(var)<<"\t|\t"<<p.der(var)<<"\n";
        accum+=pow(p.value(var),2.0);
        var+=step;
        counter++;
   }
   cout<<"\n====================================";

   accum/=counter;
   rms=sqrt(accum);
   cout<<"\nRMS energy of f(x) = "<<rms;
   return 0;
}
3
  • 2
    You should use std::vector instead of c-style arrays. Commented Mar 5, 2015 at 11:04
  • The line f[degree+1] does not "redeclare" the array, or what do you mean to achieve with this line? Commented Mar 5, 2015 at 11:07
  • vector is the C++ equivalent of Java arrays. C-style arrays are quite different, don't be misled by the vague similarity in syntax. Commented Mar 5, 2015 at 11:10

2 Answers 2

2

This does not compile on clang, it fails with "error: field has incomplete type 'int []' int f[];" and likewise for fDash.

Let's see how you declared these fields:

int f[];
int fDash[];

In C++, you can declare arrays with statically defined sizes like so:

int f[5];
int fDash[6];

If you want dynamic arrays, which you need in this case, you'd have to declare

int* f;
int* fDash;

and allocate memory for them with

f = new int[5];

You also must release that memory somewhere like so

delete [] f;

But beware - managing your own memory like this is error prone and should be avoided. You should just use std::vector instead, which is the equivalent of java.util.ArrayList:

std::vector<int> f;
std::vector<int> fDash;

And modify your input function like so:

void Polynomial::input()
{
    cout<<"Enter degree of polynomial:\t";
    cin>>degree;

    int input;

    for(int i=0;i<=degree;i++)
    {
        cout<<"Enter coefficient of x^"<<i<<":\t";
        cin>>input;
        f.push_back(input);
    }
    for(int i=0;i<degree;i++)
    {
        fDash.push_back(f[i+1]*(i+1));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

You don't use arrays correctly. You need to allocate memory if you want to use array of variable length. How use arrays in c++ see this and this, or use std::vector

3 Comments

Answers like this made me stay with c-style arrays and ignoring std::vector for long time. If he does not need to, he should not even care about c-style arrays and dynamic memory allocation. In my opinion it is badly misleading to say "Read about c-style arrays and dynamic memory allocation, or use std::vector", but the answer should be "Use std::vector!"
Yes, but giving only information about std::vector he will still don't know why first version of his solution doesn't work. It is clear that he uses Java before and C++ is not Java. So we can't try to simulate Java in C++. C++ have different memory handling and he must to know what differences are, otherwise in future hi will make another error like that or try to write Java style code but expressed in C++.
You maybe right, but your answer is pointing him in the direction of using c-style arrays and handle memory by himself. "You need to allocate memory if you want to use array of variable length." is misleading, because when he is using std::vector he does not have to do that (of course memory allocation is done but behind the scenes, just like in Java). In future he might even start to like c-style arrays and try to write C code in C++ (which works but its not nice). If he wants to use an array of variable length he should use std::vector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.