[Solved] Not able to figure out what is happening with the universal array


To solve the array issue you just need to make a few small changes.

int stop=0,d,*t; // Declare an uninitialized pointer to int
float f(float x)
{
    int loop,loopa;
    float add=0.0,sum=0.0;
    for(;stop==0;)
    {
        int p ;
        cout << "Enter the degree of the poly. eq. (+ve integer)" << endl;
        cin >> d ;
        t = new int[d+1]; // Remove the int and asterix before t. You want to assign the new array to the pointer, not the value the pointer is pointing to.
        cout << "The eq. will be in the form of ax^"<<d<<"+bx^"<<(d-1)<<" and so on ." ;
        p = 97 + d ;
        for(loop=d;loop>=0;loop--)
        {
            cout << "Enter the value of " << char(p-loop) << endl;
            cin >> t[loop];
            cout << "a="<<t[loop]<<endl;
        }
        stop=1; //ARRAY IS STILL THERE              WHY/////
    }       for(loop=0;loop<=d;loop++)      cout<<"out="<<t[loop]<<endl;
    //ARRAY IS GONE TILL NOW//
    cout<<"d="<<d<<endl;
    for(loopa=d;loopa>=0;loopa--)
    {
        cout<<"loopa="<<loopa<<"value="<<t[loopa]<<endl;
        add = t[loopa] * pow(x,loopa);
        sum=sum+add;
    }
    delete[] t; // All new'ed things need to be deleted to not cause a leak. Delete it here since it is no longer needed.
    return sum;
}

Please note that even if this works, it is not advised to use raw pointers in C++. Better to use an std::array<int> or std::vector<int> so you don’t have to take care of the allocating and deleting of memory.

EDIT: Accidentaly left the int in fron of t. Changed now.

0

solved Not able to figure out what is happening with the universal array