D
D
Daniil Vershinin2019-01-14 13:30:05
C++ / C#
Daniil Vershinin, 2019-01-14 13:30:05

Implementing "polynomial" class with dynamic array?

The coefficients are not written to the array k, and this is the problem. (Parallel question - since I messed up - is it possible at all? That is, right?)

class polynom {
    int polynomsize;
    float *k;
    //float k[100];

    public:
    // Конструктор с параметрами, инициализирующий степень полинома и выделяющий память под массив.
    polynom(int S,float K[]) {
        polynomsize = S;
        float *k = new float[polynomsize];
        for(int i=0; i<polynomsize; ++i)
            k[i]=K[i];
    }
    string get() {
        string s="[Poly] ";
        for (int i=0; i<polynomsize; i++) {
            if (k[i]!=0)
                if ((polynomsize-i-1)!=0)
                    s+=to_string(k[i])+"x^"+to_string(polynomsize-i-1)+" + ";
                else s+=to_string(k[i]);
        }
        return s;
    }

    void div(float n) {
        for (int i=0; i<polynomsize; i++) {
            k[i]=k[i]/n;
        }
    }
    ~ polynom(){
        delete[] k;
        cout << "destructor worked";
    };
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Movchan, 2019-01-14
@Titamik

float *k = new float[polynomsize];

You've overridden k and are assigning to a local pointer, not a field.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question