My Experiments with Life

December 2, 2009

Polynomial Multiplication

Filed under: Uncategorized — alanabraham @ 5:13 am

Yesterday evening, Irshad came with some polynomial multiplication problem in C .

Q : Get the resultant polynomial after the multiplication of n polynomials

Thought lots of methods like matrix multiplication to implement this

I thought of creating a program by reducing the multiplication and use addition instead and over the condition of which to multiply appropriatly…..

There i got wrong….coz u can do that for a small values like 2 polynomials or 3 polynomials for which require as many loops as the polynomials. For an n polynomial condition, i felt it like the requirement of recursive function…..

Here it got messy….i got stuct in n times recursion in a Utopian space-time frame.

Then the Messiah of this problem appeared before me……Jomon chettan

He teared my logic apart and killed it……

He told me to use the temporary structure where you go on multiply with the next.

That was a good start but i did it like a n00b……got stuck n stuck till the sleep invited me to the warmth of her lap.

Morning. I know what to do. I started recoding not debugging  the first stuff.I know now that i am going the right way….logically right path

the right path…..

it took me to this code

i think its right…..still a code is never always 100% working….u can find more conditions where it cant work properly.


Go to this link to see the program file properly

or see here

NOTE : JUST DONT LOOK INTO COMMENTED CODES….

__________________________________________________________

//polynomial mltiplication


#include<stdio.h>

struct polynomial

{

      int degree;

      float  coeff[100];

}pol[100];

//simply output the argument

void out(struct polynomial a)

{

      int k; 

      for(k=a.degree;k>=0;k--)

      {

             if(k)

                   printf("%f(x^%d)+\t",a.coeff[k],k);

             else

                   printf("%f",a.coeff[k]);

      }

      printf("\n-------------------\n"); 

}

//return type is structure polynomial

struct polynomial multiply(struct polynomial pol1,struct polynomial pol2)

{

      int a,i,j;

      //declare t and initialise to zero;

      struct polynomial t;

      //initialising t

             //---------------

                   t.degree=0;

                   for(a=0;a<100;a++)

                         t.coeff[a]=0;

             //--------------- 

      for(i=0;i<=pol1.degree;i++)

      {

             for(j=0;j<=pol2.degree;j++)

             {

                   //printf("~~~~~~~~~%f~~~~~~~~",(pol1.coeff[i]*pol2.coeff[j]));

                   t.coeff[i+j] = t.coeff[i+j] + (pol1.coeff[i]*pol2.coeff[j]);

                   //printf("@@@@@%f@@@@@-%d-",t.coeff[j],j);

             }

      }

      t.degree=i+j-2;

      return(t);

} 

//input whole structure array

void input(int n)

{

      int i,j;

      for(i=0;i<n;i++)

      {

             printf("enter the degree of polynomial %d : ",i+1);

             scanf("%d",&pol[i].degree);

             for(j=0;j<=pol[i].degree;j++)

             {

                   printf("Enter coeff of x for degree %d : ",j);

                   scanf("%f",&pol[i].coeff[j]);

             }

      } 

} 

//main function

int main()

{

      int k,n;

      //declare resultant

      struct polynomial resultant;

      printf("Enter the number of polynomials to be multiplied : ");

      scanf("%d",&n);

      //input the polynomials

      input(n);

      //

      resultant=pol[0]; 

      for(k=1;k<n;k++)

             resultant=multiply(resultant,pol[k]);  

      //output the ultimate resultant

      out(resultant);  

}


_____________________________________________________

Blog at WordPress.com.