Recursive C program to differentiation a polynomial until it stops

9.7k views Asked by At
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    void main()
    {
        int a[10],i,n,d[10],power;
        float in[10];
        clrscr();
        printf("Enter the order ofthe polynomial\n");
        scanf("%d",&n);
        for(i=n;i>=0;i--)
        {
            printf("Enter the co-efficient of x^%d:",i);
            scanf("%d",&a[i]);

        }
        printf("Given polynomial is\n");
        for(i=n;i>=0;i--)
        {
            if(power<0)
            {
                break;
            }
            if(a[i]>0)
            {
            if(i!=n)
            printf(" + ");
            }
            else if(a[i]<0)
                printf(" - ");
                else
                printf(" ");
            printf("%dx^%d",abs(a[i]),i);
        }
        //To find derivative
        for(i=n;i>=0;i--)
        d[i]=a[i]*i;
        printf("\n Derivative of the given polynomial is\n");
        for(i=n;i>=1;i--)
        {
            if(power<0)
            {
                break;
            }

            if(d[i]>0)
            printf(" + ");
            else if(d[i]<0)
                printf(" - ");
                else
                printf(" ");
            printf("%dx^%d",d[i],i-1);
        }
    getch();
    }

the above program only calculate the first derivative, but i need a program which calculates all the derivatives ex 2x^3+2x^2+3x+1 f1= 6x^2+4x+3 f2=12x+4 f3=12

like this i need to modify the program,, but am not getting how to do that,,please help me//

1

There are 1 answers

7
BLUEPIXY On BEST ANSWER

try this

#include <stdio.h>
#include <stdlib.h>

typedef struct polynomial {
    int order;
    int *coefficient;
} Polynomial;

void init_poly(Polynomial *p){
    int i;
    printf("Enter the order of the polynomial\n");
    scanf("%d", &p->order);
    p->coefficient = malloc((p->order + 1)*sizeof(int));
    for(i = p->order; i >= 0; --i){
        printf("Enter the co-efficient of x^%d:", i);
        scanf("%d", &p->coefficient[i]);
    }
}
void drop_poly(Polynomial *p){
    free(p->coefficient);
}

void print_poly(Polynomial *p){
    int i;
    for(i = p->order; i >= 0; --i){
        if(p->coefficient[i]){
            if(p->order != i){
                printf(" %c ", p->coefficient[i] < 0 ? '-' : '+');
                printf("%d", abs(p->coefficient[i]));
            } else
                printf("%d", p->coefficient[i]);
            if(i)
                printf("x^%d", i);
        }
    }
    printf("\n");
}

void differential_poly(Polynomial *p){
    int i;
    for(i = 0; i < p->order; ++i){
        p->coefficient[i] = p->coefficient[i+1] * (i+1);
    }
    p->coefficient[p->order--] = 0;
}

void r_print_differential_poly(Polynomial *p){
    int i=1;
    while(p->order){
        differential_poly(p);
        printf("f%d = ", i++);
        print_poly(p);
    }
}

int main(void){
    Polynomial poly;

    init_poly(&poly);
    printf("Given polynomial is\n");
    print_poly(&poly);

    r_print_differential_poly(&poly);

    drop_poly(&poly);
    return 0;
}