Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8922241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:47:01+00:00 2026-06-15T06:47:01+00:00

#include <iostream> #include <math.h> using namespace std; class polynomial{ int degree; float *coefs; public:

  • 0
#include <iostream>
#include <math.h>
using namespace std;

class polynomial{
    int degree;
    float *coefs;
public:
    polynomial(int d);
    float operator()(float x);
    void operator==(polynomial P);
    polynomial operator^(int k);
    float operator[](float x);
    float *getcoefs();
};

polynomial::polynomial(int d){
    int i;
    degree=d;
    if(d>=0){
        coefs=(float *)malloc((d+1)*sizeof(float));
        cout<<"Create a polynomial of degree "<<d<<"\n";
        for (i=d;i>0;i--){
            cout<<"The coefficient of x^"<<i<<" is : ";
            cin>>coefs[i];
            cout<<"\n";
        }
        cout<<"The constant is : ";
        cin>>coefs[0];
        cout<<"\n";
        cout<<"The polynomial is created\n";
    }
}

This is an overloading of the () operator so that P(x) returns the value of the polynomial:

float polynomial::operator()(float x){
    float sum=0;
    int i,kstart;
    for(i=0;i<=degree;i++){
        if(coefs[i]!=0){
            kstart=i;
            break;            
        }                       
    }
    for(i=kstart;i<=degree;i++){
        sum+=coefs[i]*pow(x,i-kstart);                       
    }
    return sum;
}

This is an overloading of the == that is copying the coefficients of one polynomial to another’s:

void polynomial::operator==(polynomial P){
     coefs=P.coefs;
}

This is an overloading of the ^ so that P^k returns the k-th derivative of P:

polynomial polynomial::operator^(int k){
    int i,j;
    polynomial G(-1);
    G.degree=this->degree;
    G==(*this);
    if(k>G.degree){
        for(i=0;i<=G.degree;i++){
            G.coefs[i]=0;
        }
    }
    else{
        for(i=0;i<k;i++){
            G.coefs[i]=0;
            for(j=i+1;j<=G.degree;j++){
                G.coefs[j]*=(j-i);                    
            }          
        }
    }
    return G;
}

float polynomial::operator[](float x){
    return (x-(*this)(x)/(((*this)^1)(x)));      
}

float *polynomial::getcoefs(){
      return coefs;
}

int main(){
    int i,d,maxiter,found;
    float seed,x1,x2,e;
    float *coefs;
    cout<<"The polynomial's degree is : ";
    cin>>d;
    cout<<"\n";
    polynomial P(d),temp(-1);
    cout<<"-------Solve P(x)=0--------\n";
    cout<<"Maxiterations = ";
    cin>>maxiter;
    cout<<"\n";
    cout<<"Tolerance = ";
    cin>>e;
    cout<<"\n";
    cout<<"Seed = ";
    cin>>seed;
    cout<<"\n";
    found=0;
    x1=seed;
    for(i=0;i<maxiter;i++){
        memcpy((void *)(&temp),(void *)(&P),sizeof(polynomial));
        x2=P[x1];
        if(fabs(x2-x1)<=e){
            found=1;
            break;                         
        }
        coefs=temp.getcoefs();
        cout<<coefs[0]<<"\n";
        cout<<coefs[1]<<"\n";
        cout<<coefs[2]<<"\n";
        x1=x2;
    }
    if(found==1){
        cout<<"A possible solution is x = "<<x2<<"\n";
    }
    else{
        cout<<"No solution found!\n"; 
    }
    system("PAUSE");
    return EXIT_SUCCESS;   
}

The problem can be seen in these testing lines:

cout<<coefs[0]<<"\n";
cout<<coefs[1]<<"\n";
cout<<coefs[2]<<"\n";

The coefficients should stay the same,but now they change

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:47:02+00:00Added an answer on June 15, 2026 at 6:47 am

    I solved my problem in the following way…
    I changed my constructor and made it look like that in the class definition:

    polynomial(int d,int test=0);
    

    and this is its code:

    polynomial::polynomial(int d,int test){
        int i;
        degree=d;
        if(test==0){
            coefs=(float *)malloc((d+1)*sizeof(float));
            cout<<"Create a polynomial of degree "<<d<<"\n";
            for (i=d;i>0;i--){
                cout<<"The coefficient of x^"<<i<<" is : ";
                cin>>coefs[i];
                cout<<"\n";
            }
            cout<<"The constant is : ";
            cin>>coefs[0];
            cout<<"\n";
            cout<<"The polynomial is created\n";
        }
        else{
            coefs=(float *)malloc((d+1)*sizeof(float));
            for (i=d;i>=0;i--){
                coefs[i]=0;
            }
        }
    }
    

    then the function that was causing the problem was made into this:

    void polynomial::operator==(polynomial P){
         int i;
         for(i=0;i<=degree;i++){
             coefs[i]=P.coefs[i];
         }
    }
    

    and with accordingly adapting small parts of my code,I was able to call P[x] without destroying my polynomial.If you have any question,please ask

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <math.h> #include <cstdlib> using namespace std; void circle(int x, int y,
i have following class with overloading operators #include<iostream> #include<algorithm> #include<math.h> using namespace std; class
#include <iostream> #include <math.h> using namespace std; int main() { int arraylength; int lastbig
Here is my work on gradient descend algorithm #include<iostream> #include<math.h> #include<float.h> using namespace std;
#include <iostream> using namespace std; int main() { int a, b, c, max; cout<<a=;
#include <iostream> #include <string.h> using namespace std; int main() { int e=0; int b=0;
#include <iostream> #include <string> using namespace std; string crash() { } int noCrash() {
#include <iostream> #include <iomanip> using namespace std; int main() { char array[10]; for(int i
#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; union type{ int a; char b; int
#include <iostream> using namespace std; // This first class contains a vector and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.