answersLogoWhite

0

#include <stdio.h>

#include <conio.h>

#define MAX 10

struct term

{

int coeff ;

int exp ;

} ;

struct poly

{

struct term t [10] ;

int noofterms ;

} ;

void initpoly ( struct poly *) ;

void polyappend ( struct poly *, int, int ) ;

struct poly polyadd ( struct poly, struct poly ) ;

struct poly polymul ( struct poly, struct poly ) ;

void display ( struct poly ) ;

void main( )

{

struct poly p1, p2, p3 ;

clrscr( ) ;

initpoly ( &p1 ) ;

initpoly ( &p2 ) ;

initpoly ( &p3 ) ;

polyappend ( &p1, 1, 4 ) ;

polyappend ( &p1, 2, 3 ) ;

polyappend ( &p1, 2, 2 ) ;

polyappend ( &p1, 2, 1 ) ;

polyappend ( &p2, 2, 3 ) ;

polyappend ( &p2, 3, 2 ) ;

polyappend ( &p2, 4, 1 ) ;

p3 = polymul ( p1, p2 ) ;

printf ( "\nFirst polynomial:\n" ) ;

display ( p1 ) ;

printf ( "\n\nSecond polynomial:\n" ) ;

display ( p2 ) ;

printf ( "\n\nResultant polynomial:\n" ) ;

display ( p3 ) ;

getch( ) ;

}

/* initializes elements of struct poly */

void initpoly ( struct poly *p )

{

int i ;

p -> noofterms = 0 ;

for ( i = 0 ; i < MAX ; i++ )

{

p -> t[i].coeff = 0 ;

p -> t[i].exp = 0 ;

}

}

/* adds the term of polynomial to the array t */

void polyappend ( struct poly *p, int c, int e )

{

p -> t[p -> noofterms].coeff = c ;

p -> t[p -> noofterms].exp = e ;

( p -> noofterms ) ++ ;

}

/* displays the polynomial equation */

void display ( struct poly p )

{

int flag = 0, i ;

for ( i = 0 ; i < p.noofterms ; i++ )

{

if ( p.t[i].exp != 0 )

printf ( "%d x^%d + ", p.t[i].coeff, p.t[i].exp ) ;

else

{

printf ( "%d", p.t[i].coeff ) ;

flag = 1 ;

}

}

if ( !flag )

printf ( "\b\b " ) ;

}

/* adds two polynomials p1 and p2 */

struct poly polyadd ( struct poly p1, struct poly p2 )

{

int i, j, c ;

struct poly p3 ;

initpoly ( &p3 ) ;

if ( p1.noofterms > p2.noofterms )

c = p1.noofterms ;

else

c = p2.noofterms ;

for ( i = 0, j = 0 ; i <= c ; p3.noofterms++ )

{

if ( p1.t[i].coeff p2.t[j].exp )

{

p3.t[p3.noofterms].coeff = p1.t[i].coeff + p2.t[j].coeff ;

p3.t[p3.noofterms].exp = p1.t[i].exp ;

i++ ;

j++ ;

}

else

{

p3.t[p3.noofterms].coeff = p1.t[i].coeff ;

p3.t[p3.noofterms].exp = p1.t[i].exp ;

i++ ;

}

}

else

{

p3.t[p3.noofterms].coeff = p2.t[j].coeff ;

p3.t[p3.noofterms].exp = p2.t[j].exp ;

j++ ;

}

}

return p3 ;

}

/* multiplies two polynomials p1 and p2 */

struct poly polymul ( struct poly p1, struct poly p2 )

{

int coeff, exp ;

struct poly temp, p3 ;

initpoly ( &temp ) ;

initpoly ( &p3 ) ;

if ( p1.noofterms != 0 && p2.noofterms != 0 )

{

int i ;

for ( i = 0 ; i < p1.noofterms ; i++ )

{

int j ;

struct poly p ;

initpoly ( &p ) ;

for ( j = 0 ; j < p2.noofterms ; j++ )

{

coeff = p1.t[i].coeff * p2.t[j].coeff ;

exp = p1.t[i].exp + p2.t[j].exp ;

polyappend ( &p, coeff, exp ) ;

}

if ( i != 0 )

{

p3 = polyadd ( temp, p ) ;

temp = p3 ;

}

else

temp = p ;

}

}

return p3 ;

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

If polynomial is not any number then how will you define a polynomial?

An expression made with constants, variables and exponents, which are combined using addition, subtraction and multiplication, ... but not division.


C program for comparison of dates using structures?

C program for comparison of dates using structures


Why monomial is not polynomial?

In mathematics, a polynomial is a finite expression made up of variables and constants, by using the operations of addition, subtraction, multiplication. The other requirement is the the exponents bet non-negative whole number.A polynomial is the sum of two or more monomials. That is why a monomial is not a polynomial.


Is 6xy a polynomial?

Yes, (6xy) is a polynomial. A polynomial is defined as an expression consisting of variables raised to non-negative integer powers and combined using addition, subtraction, and multiplication. In this case, (6xy) is a product of constants and variables, both of which are raised to the first power, fitting the criteria for a polynomial.


Write a c program add two polynomial using link list?

GOUDHMARINI


Is a monomial also a polynomial?

A polynomial is a finite length expression constructed from variables and constants, using the operations of addition, subtraction, multiplication, and constant non-negative whole number exponent. Most people require that a polynomial consist of two or more monomials in which case the answer is NO!


Is A monomial is also a polynomial.?

A polynomial is a finite length expression constructed from variables and constants, using the operations of addition, subtraction, multiplication, and constant non-negative whole number exponent. Most people require that a polynomial consist of two or more monomials in which case the answer is NO!


C program for matrix multiplication using dynamic memory alllocation?

Poor boy


What Must the sum of three polynomials again be a polynomial?

The sum of three polynomials must again be a polynomial because polynomials are defined as expressions consisting of variables raised to non-negative integer powers, combined using addition, subtraction, and multiplication by constants. When you add polynomials, the resulting expression will still adhere to these rules, maintaining the structure of a polynomial. Specifically, the degrees of the resulting polynomial will be determined by the highest degree among the summed polynomials, ensuring it remains a polynomial. Therefore, the sum of any number of polynomials is always a polynomial.


What are the 4 conditions for an algebraic expression to be a polynomial?

A polynomial consists of one or more parts, each of which is called a monomial. The monomials are combined with "+" or "-" operations. Within each monomial, any real number is combined with one or more variables, using only multiplication, and - for the variables - positive integer powers (which is just a shortcut for repeated multiplication). That's about it; you can see how best to split this into exactly "4 conditions" if you like.


Evaluate the polynomial expression using linked list using c?

Evaluating a Polynomial expression using a singly linked list visit : http://myfundatimemachine.blogspot.in/2012/06/polynomial-evaluation-in-c.html


Why cant a polynomial have a negative exponents?

A polynomial is defined as a mathematical expression consisting of variables raised to non-negative integer exponents and combined using addition, subtraction, and multiplication. Negative exponents would imply division by the variable raised to a positive power, which leads to fractional terms that are not permitted in the definition of polynomials. Thus, having negative exponents would disqualify an expression from being classified as a polynomial.