Add two Polynomial

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct poly
{
	int co;
   int pow;
};
typedef struct poly POLY;
int main(void)
{
	POLY *p,*q;
   int m,n,i,j;
   printf("Enter the highest power of both poly[m>n]: ");
   scanf("%d%d",&m,&n);
	p=(POLY*)malloc((m+1)*(sizeof(POLY)));
	printf("Enter the coefficient and power of the 1st poly: \n");
   for(i=0;i<=m;i++)
	scanf("%d %d",&p[i].co,&p[i].pow);

   q=(POLY*)malloc((n+1)*(sizeof(POLY)));
   printf("Enter the coefficient and power of the 2nd poly: \n");
   for(i=0;i<=n;i++)
	scanf("%d %d",&q[i].co,&q[i].pow);
   printf("The polynomial is: \n");
   for(i=0;i<=m;i++)
   {
      for(j=0;j<=n;j++)
      {
			if(*(&p[i].pow)==*(&q[j].pow))
         {
				*(&p[i].co)=*(&p[i].co)+*(&q[j].co) ;
            break;
         }
			else
				*(&p[i].co)=*(&p[i].co) ;
         }
	 		printf("%dX^%d  ",*(&p[i].co),*(&p[i].pow));
	 		if(i!=m)
				printf("+ ");
   }
   free(p);
   free(q);
   getch();
   return 0;
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *