Answer to Question #259584 in C for ffff

Question #259584

write a program in c to add two polynomials with n number of variables


1
Expert's answer
2021-11-01T08:28:06-0400
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct Polynomial
{
 float C;
 int exp;
};
struct Polynomial poly1[50],poly2[50],polyAdd[50],d[50];
int main()
{
 int x;
 int X1,X2; 
 int k=0,l=0,m=0;
 printf("Input the highest degree of polynomial 1: ");
 scanf("%d",&X1);
 for(x=0;x<=X1;x++){
 printf("Input the coeCff of x^%d :",x);
 scanf("%f",&poly1[x].C);
 poly1[k++].exp = x;
 }
 printf("Input the highest degree of polynomial 2: ");
 scanf("%d",&X2);




 for(x=0;x<=X2;x++)
 { 
 printf("Input the coeff of x^%d: ",x);
 scanf("%f",&poly2[x].C);
 poly2[l++].exp = x;
 }
 printf("\nPolynomial 1 = %.1f",poly1[0].C);
 for(x=1;x<=X1;x++)
 {
 printf("+ %.1fx^%d",poly1[x].C,poly1[x].exp);
 } 
 printf("\nPolynomial 2 = %.1f",poly2[0].C);
 for(x=1;x<=X2;x++)
 {
 printf("+ %.1fx^%d",poly2[x].C,poly2[x].exp);
 }
 if(X1>X2)
 {
 for(x=0;x<=X2;x++)
 {
 polyAdd[m].C = poly1[x].C + poly2[x].C;
 polyAdd[m].exp = poly1[x].exp;
 m++;
 }




 for(x=X2+1;x<=X1;x++)
 {
 polyAdd[m].C = poly1[x].C;
 polyAdd[m].exp = poly2[x].exp;
 m++;
 }
 }
 else
 {
 for(x=0;x<=X1;x++)
 {
 polyAdd[m].C = poly1[x].C + poly2[x].C;
 polyAdd[m].exp = poly1[x].exp;
 m++;
 }
 for(x=X1+1;x<=X2;x++)
 {
 polyAdd[m].C = poly2[x].C;
 polyAdd[m].exp = poly2[x].exp;
 m++;
 }
 }
printf("\nThe sum of the two polynomials = %.1f",polyAdd[0].C);
 for(x=1;x<m;x++)
 {
 printf("+ %.1fx^%d",polyAdd[x].C,polyAdd[x].exp);
 } 
 getchar();
 getchar();
 return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS