Answer to Question #239667 in C for kanha

Question #239667

Write a C code to add two polynomials using an array.


1
Expert's answer
2021-09-20T11:09:18-0400
#include<stdio.h>




int main()
{
int poly_first[10], poly_second[10], poly_sum[10],first_term,second_terms,a,m,n,y,b;
printf("\nAdding Polynomial using arrays\n");
printf("\nNumber of terms for the first polynomial:");
scanf("%d", &first_term);
printf("\nEnter terms and coefficient for the first polynomial:\n");
for (n=0;n<2*first_term;n++)
scanf("%d", &poly_first[n]);
printf("\n\The first polynomial:\n");
m=0;
if(poly_first[m+1]==1)
printf("x^%d", poly_first[m]);
else
printf("%dx^%d", poly_first[m+1],poly_first[m]);
m+=2;
while (m<n)
{
printf("+%dx^%d", poly_first[m+1],poly_first[m]);
m+=2;
}
printf("\nTotal terms for the second polynomial:\n");
scanf("%d", &second_terms);
printf("\nEnter terms and coefficient for the second polynomial:\n");
for(y=0;y<2*second_terms;y++)
scanf("%d", &poly_second[y]);
printf("\nThe Second polynomial:\n");
m=0;
if(poly_second[m+1]==1)
printf("x^%d", poly_second[m]);
else
printf("%dx^%d",poly_second[m+1],poly_second[m]);
m+=2;
while (m<2*second_terms)
{
printf("+%dx^%d", poly_second[m+1],poly_second[m]);
m+=2;
}
n=0;
y=0;
a=0;
while (first_term>0 && second_terms>0)
{
if (poly_first[n]==poly_second[y])
{
poly_sum[a+1]=poly_first[n+1]+poly_second[y+1];
poly_sum[a]=poly_first[n];
first_term--;
second_terms--;
n+=2;
y+=2;
}
else if (poly_first[n]>poly_second[y])
{
poly_sum[a+1]=poly_first[n+1];
poly_sum[a]=poly_first[n];
first_term--;
n+=2;
}
else
{
poly_sum[a+1]=poly_second[y+1];
poly_sum[a]=poly_second[y];
second_terms--;
y+=2;
}
a+=2;
}
while (first_term>0)
{
poly_sum[a+1]=poly_first[n+1];
poly_sum[a]=poly_first[n];
a+=2;
n+=2;
first_term--;
}
while (second_terms>0)
{
poly_sum[a+1]=poly_second[y+1];
poly_sum[a]=poly_second[y];
a+=2;
y+=2;
second_terms--;
}
printf("\nThe sum polynomial is:");
m=0;
if (poly_sum[m+1]==1)
printf("x^%d", poly_sum[m]);
else
printf("%dx^%d", poly_sum[m+1],poly_sum[m]);
m+=2;
while (m<a)
{
if (poly_sum[m+1]==1)
printf("+x^%d", poly_sum[m]);
else
printf("+%dx^%d", poly_sum[m+1], poly_sum[m]);
m+=2;
}
getch();
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