Link to Question http://www.spoj.com/problems/POLYMUL/
Link to my code: http://ideone.com/g5a7aj
Please provide me a test case where this doesn't work. I got a wrong answer in this, and all of the test cases I can think of are working here. Thank you for your help!
The code:
//multiplication of nth order polynomials
#include <stdio.h>
#include <stdlib.h>
void printpolynomial(int a[], int n)
{
int i;
for(i=n; i>=0; i--)
printf("%d ", a[i]);
printf("\n");
}
void polymult()
{
int i, n;
scanf("%d", &n);
int *a= (int *) malloc((n+1)*sizeof(int));
for(i=n; i>=0; i--)
scanf("%d", &a[i]);
int *b= (int *) malloc((n+1)*sizeof(int));
for(i=n; i>=0; i--)
scanf("%d", &b[i]);
int *answer= (int *) malloc((2*n+1)*sizeof(int));
for(i=0; i<(2*n+1); i++)
answer[i]=0;
int index_a, index_b;
for(index_a=0; index_a<=n; index_a++)
for(index_b=0; index_b<=n; index_b++)
answer[index_a+index_b]+=(a[index_a]*b[index_b]);
printpolynomial(answer, 2*n);
free(a); free(b); free(answer);
}
void main()
{
int t;
scanf("%d", &t);
while(t-->0)
polymult();
}