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 8923567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:09:34+00:00 2026-06-15T07:09:34+00:00

For some reason when i am trying to do my derivative it just does

  • 0

For some reason when i am trying to do my derivative it just does a derivative of the one item not the whole polynomial.

struct term{
    double coef;
    unsigned deg;
    struct term * next;
    };

I have a struct and then also a class Polynomial with deep copy constructor and = constructor.
In a private class i have a term* ptr

here is my code for the derivative

void Polynomial::derivative (Polynomial *p){
    term *x;
    if ( ptr == NULL)
        return ;
    term *temp;
    temp = ptr;
    while (temp != NULL){
       if ( ptr == NULL){
            ptr = new term ;
            x = ptr ;
        }
        else{
            x -> next = new term ;
            x = x -> next ;
        }
        x-> coef = temp -> coef * temp -> deg;
        x-> deg = temp -> deg - 1;
        temp = temp -> next;

    }
    ptr=x;
}

so when i try to derivative of 3x^4 + 3x^4 + 6x^7 + 3x^4 + 3x^4 + 6x^7 + 2x^9 i get 18x^8

I was looking over the code and have no idea why does it do that for just the last term, since it is a while loop and should go from beginning till NULL and do the derivative.

  • 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-15T07:09:36+00:00Added an answer on June 15, 2026 at 7:09 am

    You’re getting the last term because of these two lines:

    in your else condition:

    x = x -> next
    

    and your final assignment:

    ptr = x;
    

    Consequently, this also leaks memory, since all those pretty terms you allocated prior are now in the ether. You were leaking the old ones anyway, so this really needs a rethink regardless.

    I strongly recommend that since your Polynomial class supports full copy construction and assignment operation, you create a new derivative polynomial from this one, and return that. if the caller wants this one transformed they can poly = poly.derivative(); themselves.

    Example of derivative generator (as opposed to transformer). And as a bonus, eliminates all constant terms when generating the derivative.

    Polynomial Polynomial::derivative() const
    {
        Polynomial poly;
    
        const term *p = ptr;
        while (p)
        {
            if (p->deg != 0) // skip constant terms 
            {
                // add term to poly here (whatever your method is called)
                poly.addTerm(p->coef * p->deg, p->deg-1);
            }
            p = p->next;
        }
        return poly;
    }
    

    This allows this kind of generation: (note p1 is unchanged by derivative()):

    Polynomial p1;
    ... populate p1...
    Polynomial p2prime = p1.derivative();
    

    And for something really enjoyable:

    Polynomial p1;
    ... populate p1...
    Polynomial p2prime2 = p1.derivative().derivative();
    

    Anyway, I hope that makes sense.

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

Sidebar

Related Questions

Im trying to get this working but for some reason it's just not right.
i'm trying to create some getters via metaprogramming,but for some reason it does not
I'm trying this and for some reason is not working. I'm suspecting it may
hi im trying to using flashVars however for some reason there not getting sent
I'm trying to benchmark a few methods against one another and for some reason
I'm trying to declare a lot of enums however for some reason one of
I'm trying to do some URL rewriting - but for some reason it's not
I am trying to add two numbers but for some reason I am not
I'm trying to write a Counting sort function but for some reason it's getting
I am trying to learn codeigniter and for some reason I can't do the

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.