Having trouble with this. Adding, derivation, integration, all work fine, but multiplication doesn't.

My polynomial is in fact an array of pointers. Maybe redundant but that's my choice. I set the max_koef to 5 so that means that the degree of the polynomial is 5 and that the array has 6 items, p^0, p^1,..., p^5.

This is my code.

Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_KOEF 5; typedef struct node { int exp; float koef; struct node *next; } monomial; void MENU() { printf("\n***\tPOLYNOMIAL\t***\n"); printf("\n 1.) Add to p"); printf("\n 2.) Add to q"); printf("\n 3.) Printout both"); printf("\n 4.) Multiply"); printf("\n 0.) EXIT"); printf("\nChoose ---------> "); } void Init(monomial *p[]) { int i; int n = MAX_KOEF; for (i=0; i<=n; i++) p[i] = NULL; } void Printout(monomial *a[], int n) { int i, j, test; for (i=0; i<=n; i++) { if (a[i] != NULL) { if (i == 0) printf("\t%4.2f\t",a[i]->koef); else { for (j=0; j<i; j++) { if (a[j] != NULL) { test = 1; break; } else test = 0; } if (test == 1) printf("\t+\t\t%4.2f x^%d\t",a[i]->koef, a[i]->exp); else printf("\t%4.2f x^%d\t",a[i]->koef, a[i]->exp); } } } printf("\n"); } void Insert(monomial *a[], float koef, int exp) { monomial *new = malloc(sizeof(monomial)); int e = exp; new->koef = koef; new->exp = exp; new->next = NULL; if (a[e] == NULL) a[e] = new; else a[e]->koef += new->koef; } void Multiply(monomial *a[], monomial *b[], monomial *c[]) { int i, j; int n = MAX_KOEF; for (i=0; i<=n; i++) { if (a[i] != NULL) { for (j=0; j<=n; j++) { if (b[j] != NULL) { Insert(c, a[i]->koef * b[j]->koef, a[i]->exp + b[j]->exp); } } } } Printout(c, 2*n + 1); } int main (int argc, const char * argv[]) { int n = MAX_KOEF; int choice, exp, test; float koef; monom *p[n+1],*q[n+1]; monom *multi[2*n+1]; Init(p); Init(q); Init(multi); do { MENU(); scanf("%d",&choice); switch (choice) { case 1: printf("Enter koeficcient -> "); scanf("%f",&koef); do { printf("Enter exponent -> "); scanf("%d",&exp); if (exp < 0 && exp > n) test = 0; else test = 1; } while (test != 1); Insert(p,koef,exp); break; case 2: printf("Enter koeficcient -> "); scanf("%f",&koef); do { printf("Enter exponent -> "); scanf("%d",&exp); if (exp < 0 && exp > n) test = 0; else test = 1; } while (test != 1); Insert(q,koef,exp); break; case 3: printf("\n::::::: polynomial p :> "); Printout(p, n); printf("\n::::::: polynomial q :> "); Printout(q, n); break; case 4: printf("\n::::::: polynomial p * q :> "); Multiply(p,q,multi); break; default: printf("\n WRONG CHOICE"); } } while (choice != 0); return 0; }