-1
    #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//

junior
  • 15
  • 1
  • 5
  • 1
    Copy `d[]` to `a[]` and repeat. – Weather Vane Nov 15 '14 at 22:09
  • i need the program to execute all steps till it gets stopped i mean all the differentiation values to be calculated automatically, if i copy d[] to a[] may be first ,second like tat the derivation are obtained,i need it to happen continously for the given polynomial. – junior Nov 17 '14 at 03:51

1 Answers1

2

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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • p->coefficient = malloc((p->order + 1)*sizeof(int)); in this command line am getting error like cannot convert void * to int *.. how to solve tat – junior Nov 17 '14 at 00:41
  • @junior You are using a C++ compiler instead of the C compiler. So need Cast. E.g `p->coefficient = (int*)malloc((p->order + 1)*sizeof(int));` However you do not need the case of the C compiler. – BLUEPIXY Nov 17 '14 at 08:31
  • k sir. i will do myself. :) – junior Nov 17 '14 at 15:26
  • i needed to check time complexity for the differ program. i included and i used time_h start_t,end_t; then i added clock_t = start_t; and clock_t = end_t; in the program and i tried to print the time... but its not printing time... plz help me – junior Nov 18 '14 at 02:13
  • @junior `clock_t start_t, end_t; start_t = clock(); ...program... end_t = clock();double sec = (end_t-strt_t)/CLOCKS_PER_SEC;` – BLUEPIXY Nov 18 '14 at 09:25
  • am not understanding this part 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; } plz help me with a example for the above step – junior Nov 19 '14 at 13:43
  • @junior It has been calculated from the low order. – BLUEPIXY Nov 19 '14 at 13:51