# C program for multiplication of two polynomials required..

Page 1 of 1

## 2 Replies - 28401 Views - Last Post: 18 March 2008 - 05:15 AMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=46581&amp;s=36430ef8226e0eefa840e3f0173530c8&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #4 Neerndg123

Reputation: 2
• Posts: 1
• Joined: 18-March 08

## Re: C program for multiplication of two polynomials required..

Posted 18 March 2008 - 04:53 AM

#include<stdio.h>
#include<malloc.h>
struct polynode
{
float coeff;
int exp;
};

void poly_append(struct polynode **,float,int);
void display_poly(struct polynode *);
void poly_multiply(struct polynode *, struct polynode *, struct polynode **);
void padd(float, int, struct polynode **);

main()
{
struct polynode *first, *second, *mult;
int i,coeff,exp,high;

first = second = mult = NULL;

printf("Enter the highest power of polynomial 1: \n");
scanf("%d",&high);

for(i=high;i>0;i--)
{
printf("Enter value for coeff for X^%d : ",i);
scanf("%d",&coeff);

poly_append(&first, coeff,i);
}

printf("\nEnter the highest power of polynomial 2: \n");
scanf("%d",&high);

for(i=high;i>0;i--)
{
printf("Enter value for coeff for X^%d : ",i);
scanf("%d",&coeff);

poly_append(&second, coeff,i);
}

printf("\n\n");
display_poly(&first);
printf("\n");
display_poly(second);

printf("\n");
for(i=1;i<=79;i++)
printf("-");

poly_multiply(first, second, &mult);

printf("\n");
display_poly(mult);

}

/* adds a term to a polynomial */
poly_append(struct polynode **q, float x, int y)
{
struct polynode *temp;
temp = *q;

/* create a new node if the list is empty */
if(*q == NULL)
{
*q = malloc(sizeof(struct polynode));
temp = *q;
}
else
{
/* traverse the entire linked list */

/* create new nodes at intermediate stages */

temp -> link = malloc ( sizeof ( struct polynode ) ) ;
}

/* assign coefficient and exponent */
temp -> coeff = x;
temp -> exp = y;
}

/* displays the contents of linked list representing a polynomial */
display_poly(struct polynode *q)
{
/* traverse till the end of the linked list */
while(q != NULL)
{
printf("%.1f x^%d : ", q -> coeff, q -> exp);
}

printf("\b\b\b"); /* erases the last colon( */
}

/* multiplies the two polynomials */
poly_multiply(struct polynode *x, struct polynode *y, struct polynode **m)

{
struct polynode *y1;
float coeff1, exp1;

y1 = y; /* point to the starting of the second linked list */

if(x == NULL && y == NULL)
return;

/* if one of the list is empty */
if(x == NULL)
*m = y;
else
{
if(y == NULL)
*m = x;
else /* if both linked lists exist */
{
/* for each term of the first list */
while(x != NULL)
{

/* multiply each term of the second linked list with a
term of the first linked list */
while(y != NULL)
{
coeff1 = x -> coeff * y -> coeff;
exp1 = x -> exp + y -> exp;

/* add the new term to the resultant polynomial */
}

y = y1; /* reposition the pointer to the starting of

x = x -> link; /* go to the next node */
}
}
}
}

/* adds a term to the polynomial in the descending order of the exponent */
padd(float c, int e, struct polynode **s)
{
struct polynode *r, *temp = *s;

/* if list is empty or if the node is to be inserted before the first node */
if(*s == NULL || e > ( *s ) -> exp)
{
*s = r = malloc(sizeof(struct polynode));
( *s ) -> coeff = c;

( *s ) -> exp = e;
( *s ) -> link = temp;
}
else
{
/* traverse the entire linked list to search the position to insert a new node */
while(temp != NULL)
{
if ( temp -> exp == e )
{
temp -> coeff += c;

return;
}

if ( temp -> exp > e && ( temp -> link -> exp < e || temp -> link == NULL ) )
{
r = malloc ( sizeof ( struct polynode ) );
r -> coeff = c;
r -> exp = e;
return;
}

temp = temp -> link; /* go to next node */
}

}
}

### #5 red_4900

• Code T(h)inkers

Reputation: 21
• Posts: 1,120
• Joined: 22-February 08

## Re: C program for multiplication of two polynomials required..

Posted 18 March 2008 - 05:14 AM

welcome to dreamincode.

please .its stated in the background when you type your post. and also please state clearly what problem u have with ur code. help others to help u.

This post has been edited by red_4900: 18 March 2008 - 05:31 AM

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: C program for multiplication of two polynomials required..

Posted 18 March 2008 - 05:15 AM

Can you describe the problem you are encountering?