## Comentários do leitor

### C Program Code for Addition of Two Polynomials using Arrays

#### "Margie Wroe" (2020-04-17)

id="mod_22093521">
Polynomials come under the section Algebra in Mathematics. A polynomial is an expression of finite length constructed from variables, constants and non-negative integer exponents. The operations addition, subtraction and multiplication determine its entire structure. Polynomials are used in a wide variety of problems where they are called as polynomial equations.

An example of a polynomial is 3x2+2x+7; here, the total number of terms is 3. The coefficients of each term are 3, 2, 7 and degrees 2, 1, 0 respectively.

While adding two polynomials, following cases need to be considered.

When the degrees of corresponding terms of the two polynomials are same:

This is the normal case when corresponding coefficients of each term can be added directly. For example, the sum of the polynomials

5x3+2x2+7

7x3+9x2+12

-------------------

12x3+11x2+19 is a simple addition where all the degrees of the corresponding terms are same.

2. When the degrees of corresponding terms of the polynomials are different:

Here, the term with the larger degree pre-dominates.

9x4+5x3+ +2x

3x4+ +4x2+7x

------------------------

12x4+5x3+4x2+9x

Algorithm

Here, I'm writing the program for polynomial addition in C language using arrays and as printing a polynomial in its form is a little time-consuming, the code also got lengthier.

Let ‘m' and ‘n' be the no. of terms of the two polynomials represented by arrays a[] and b[]. Sum of them is represented by c[] and i, j, k respectively are the subscripts used to denote the various elements of arrays a, b and c. The logic of the program is written in 3 parts considering each possibility in order to obtain the exact sum.

First part

Variables i, j, k are set to zero in the beginning. The condition that ‘m' and white glove furniture installation ‘n' should be greater than zero is what is required for execution of the first part. Then, the next checking condition that a[i] equal to b[j] determines whether the degree of the first term of the first polynomial is equal to the degree of the first term of the second polynomial. If this is true, then the sum of the coefficients a[i+1] and b[j+1] is stored in c[k+1] and the degree a[i] or b[i] (both same) in c[k]. As, summation takes place with one term from each of the polynomials, each term gets reduced by 1. Also, the subscripts ‘i' and ‘j' are moved forward by 2 as each term has a degree and a coefficient.

If the checking condition becomes false, then next check a[i] greater than b[j] finds out whether the degree of the first polynomial is greater than that of the second. If this is true, then the degree of the sum c[k] is set to a[i] and the coefficient c[k+1] to a[i+1]. The no. of terms of first polynomial gets reduced by 1 and the subscript ‘i' also moved forward by 2.If this condition becomes false, then the degree of the sum c[k] becomes b[j] and c[k+1]=b[j+1]. The no. of terms of second polynomial gets reduced by 1 and the subscript ‘j' is moved forward by 2.

After executing just one of these possible cases, control finally reaches the statement k=k+2 that moves forward the subscript of array ‘c' by 2. When either ‘m' or ‘n' becomes zero, control exits the loop and moves to the second part.

Flowchart of first part

First part of polynomial addition | Source

Second part

As there can be some terms remaining in either first polynomial or second after the execution of first part, they need to be added to the polynomial sum. This is done by iterating a set of statements for the condition that the no. of terms is greater than zero taking each independently.

The no. of terms of 1st polynomial is checked first. If it is greater than zero, then the coefficient of polynomial sum where it is standing at present is equated to the current coefficient of the 1st polynomial. Similarly degree also has to be equated correspondingly. Then, no. of terms of 1st polynomial gets reduced by 1 and subscripts ‘i' and ‘k' skip forward by 2. The loop is executed as long as the condition is true.

Flowchart of second part

Second part of polynomial addition | Source

Third part

The second part is repeated here for second polynomial. After all the remaining terms of it gets added to the sum, the control exits the loop and final result is obtained.

Flowchart of third part

Third part of polynomial addition | Source

C program code

#include
#include

main()

int a, b, c,m,n,k,k1,i,j,x;

clrscr();

printf("
\tPolynomial Addition
");
printf("\t===================
");

printf("
\tEnter the no. of terms of the polynomial:");
scanf("%d", &m);

printf("
\tEnter the degrees and coefficients:");
for (i=0;i<2*m;i++)
scanf("%d", &a[i]);

printf("
\tFirst polynomial is:");

k1=0;
if(a[k1+1]==1)
printf("x^%d", a[k1]);
else
printf("%dx^%d", a[k1+1],a[k1]);
k1+=2;

while (k1

printf("+%dx^%d", a[k1+1],a[k1]);
k1+=2;

printf("

\tEnter the no. of terms of 2nd polynomial:");
scanf("%d", &n);

printf("
\tEnter the degrees and co-efficients:");

for(j=0;j<2*n;j++)
scanf("%d", &b[j]);
printf("
\tSecond polynomial is:");

k1=0;
if(b[k1+1]==1)
printf("x^%d", b[k1]);
else
printf("%dx^%d",b[k1+1],b[k1]);
k1+=2;
while (k1<2*n)

printf("+%dx^%d", b[k1+1],b[k1]);
k1+=2;

i=0;
j=0;
k=0;

while (m>0 && n>0)

if (a[i]==b[j])

c[k+1]=a[i+1]+b[j+1];
c[k]=a[i];
m--;
n--;
i+=2;
j+=2;

else if (a[i]>b[j])

c[k+1]=a[i+1];
c[k]=a[i];
m--;
i+=2;

else

c[k+1]=b[j+1];
c[k]=b[j];
n--;
j+=2;

k+=2;

while (m>0)

c[k+1]=a[i+1];
c[k]=a[i];
k+=2;
i+=2;
m--;

while (n>0)

c[k+1]=b[j+1];
c[k]=b[j];
k+=2;
j+=2;
n--;

printf("

\tSum of the two polynomials is:");
k1=0;
if (c[k1+1]==1)
printf("x^%d", c[k1]);
else
printf("%dx^%d", c[k1+1],c[k1]);
k1+=2;

while (k1

if (c[k1+1]==1)
printf("+x^%d", c[k1]);
else
printf("+%dx^%d", c[k1+1], c[k1]);
k1+=2;

getch();
return 0;

Results of polynomial addition

Addition of polynomials when the no. of terms and degrees of corresponding terms are equal | Source

Addition of polynomials when the no. of terms and corresponding degrees differ | Source

Do you find this code to be complex?

Yes

No

See results

© 2013 Radhika Sreekanth

Popular

Computer Programming Tutorials

How-To Develop an iOS AVPlayer Application and AirPlay
by Kevin Languedoc0

Computer Programming Tutorials

Borland C Graphics.h Tutorials | How Computer graphics using C graphics library in Programming
by Akhil Anil21

Computer Programming Tutorials

How to install \ deploy django on Xampp webserver with wsgiscriptalias on windows
by Rocklin2

Comments

Sign in or sign up and post using a HubPages Network account.

0 of 8192 characters used
Post Comment

No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.

sending

sanjay paudel

15 months ago

your idea is really awesome author ..

How can i follow ask other question??

العالمية كلين

3 years ago from Saudi Arabia

hey radhikasree

Great blog.Really looking forward to read more. Cool.

AUTHOR
Radhika Sreekanth

3 years ago from Mumbai,India

Thanks Ryan Dsouza for that golden comment. Your comment is inspiring me to create more such content.

Ryan Dsouza

3 years ago

Dear author,

The explanation you have provided is simply golden.... Amazing clarity, precise explanation and an easy to follow explanation..

Thanks your creating such quality content. :)

sangeetha

3 years ago

Thank you mam.

AUTHOR
Radhika Sreekanth

4 years ago from Mumbai,India

Hi dharam,

Nice to hear that you own a blog about computer programming. I"ll visit it for sure. Thanks for commenting.

AUTHOR
Radhika Sreekanth

4 years ago from Mumbai,India

Hi Garima,

Declare a structure named poly and let a[] and b[] be the data members storing degrees and co-efficients of the polynomials respectively. Then if x and y are two variables of struct 'poly' type, do the addition by accessing the variables as x.a[], x.b[], y.a[] and y.b[]. Hope you got it.

dharam

4 years ago

Thank you very much for this article... I really enjoy reading your articles. I am a software engineer with 7+ years of experience and I author a blog which is for computer programming students. I really got inspiration from your blog. Here is a link to my blog please have a look website

I was trying to explore the possibility of getting few links to my blog from here. Please let me know if you are open to that..

Thanks a tonne.

Garima

4 years ago

how can we do the same using both arrays and structures

AUTHOR
Radhika Sreekanth

4 years ago from Mumbai,India

Hi Priyanka,

Without seeing the program I can't answer your query. They are not keywords and hope they are inserted in '//' to describe the relevant step of the program.

priyanka

4 years ago

hello mam,

here i have a doubt

why we are using match,proceed words in add of poly program

and what these words represents

plz give reply to my quest

AUTHOR
Radhika Sreekanth

5 years ago from Mumbai,India

Hi Sourath,

Appreciate your question.

A program is one that is crafted according to some particular requirements and it works best for that criteria only. So if the program asks you to input degree in decreasing order, you should feed it in that manner itself to get results. Else the program may crash as it is not programmed according to your likes.

If you wish, a validation can be included that says, "Input data don't match the program format".

Sourath

5 years ago

What happens if someone doesn't inputs the polynomial degree in decreasing order.I mean if someone enters the polynomial as both increasing and decreasing order of degree

1st polynomial as : 3x^6+5x^4+2x^2+1

2nd polynomial as :2+7x^2+3x^4+2x^6

mammootty

6 years ago

thank u polynomial addition is very simple written than me.any ways thanks a lot.

AUTHOR
Radhika Sreekanth

6 years ago from Mumbai,India

Thanks jeni!

jeni...

6 years ago

this program is very easily to study.....

AUTHOR
Radhika Sreekanth

6 years ago from Mumbai,India

@Himanshu

Algorithm for multiplication would take time to develop as the entire logic is different. Give me some time, then I'll post it.

Himanshu

6 years ago

HOW to multiply of polynomial equation , plz include c code with algo...

AUTHOR
Radhika Sreekanth

7 years ago from Mumbai,India

Thanks atein for reading and voting. Yes, we can do it using structures simply or even by using linked lists. That may shorten the code length too.

atein

7 years ago

Good hub . Voted up . Nice Flowchart/ Algo . We could have done the same using structures . I mean we can also use structures in C to add polynomials . Right ?

AUTHOR
Radhika Sreekanth

7 years ago from Mumbai,India

Hi Ruby,

Yes, this is going to be useful for students of Computer programming. I've worked as a Faculty with Aptech Computer Education Centre in the past. Thanks for reading and trying it. Take care.

Ruby Jean Richert

7 years ago from Southern Illinois

I am sure this is useful to many who study this type of mathematics, but for me, i am lost, but i have a hard time with any kind of math. I was able to work out one part after you explained the technique. Thank's for sharing. Are you a teacher? Cheers.