answersLogoWhite

0


Best Answer

#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 ) ;

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 ;

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

16y ago

The question is about writing a programme that will accept two polynomial equasions and add them. As an example:

3x^2 + 2x + 1

2x^2 - x +1

to yield:

3x^2 + x + 2

Further, the programme should represent the equasions as a linked list.

If you are still scratching your crust over this, then have a butcher's at the following code which reads two equasions from the command line and adds them together. A sample run is at the bottom. Best of luck.

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

struct ele {

struct ele *next;

int coef;

int power;

};

/* read an equasion and build a linked list

equasion must be of the form nxp {+\-} nxp...

where n is the coefficent of x and p is the power (3x2 meaning

3-x-squared). The equasion must be oredered in decending power

and things like 0x3 can be omitted, and the last (constant)

does not need x0. 1x2 and x2 are the same.

*/

struct ele *build( char *buf )

{

struct ele *expr = NULL;

struct ele *tail = NULL;

struct ele *cep = NULL;

char *tok;

char *tp;

int sign = 1;

tok = strtok( buf, " \t" );

while( tok )

{

if( !isdigit( *tok ) && !*(tok+1) )

{

switch( *tok )

{

case '+': sign = 1; break;

case '-': sign = -1; break;

}

}

else

{

cep = (struct ele *) malloc( sizeof( struct ele ) );

memset( cep, 0, sizeof( *cep ) );

if( !tail )

expr = tail = cep;

else

{

tail->next = cep;

tail = cep;

}

if( *tok NULL e1->power > e2->power) )

{

cep->power = e1->power;

cep->coef = e1->coef;

e1 = e1->next;

}

else

{

cep->power = e2->power;

cep->coef = e2->coef;

e2 = e2->next;

}

}

}

return result;

}

void print( struct ele *ep )

{

while( ep )

{

if( ep->coef != 0 )

switch( ep->power )

{

default:

printf( "%+dx^%d ", ep->coef, ep->power );

break;

case 1:

printf( "%+dx ", ep->coef );

break;

case 0:

printf( "%+d", ep->coef );

break;

}

ep = ep->next;

}

printf( "\n" );

}

int main( int argc, char **argv )

{

struct ele *expr1 = NULL;

struct ele *expr2 = NULL;

struct ele *sum = NULL;

expr1 = build( argv[1] );

expr2 = build( argv[2] );

sum = add( expr1, expr2 );

print( expr1 );

print( expr2 );

print( sum );

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Here's the program. Took me some time to figure it out, but I did manage to do it, and I am satisfied with my effort.

#include

main()

{

int i,j,k=0,m,l,n;

int a[80],b[80],c[80];

printf("Enter the degree of the first polynomial: ");

scanf("%d",&m);

printf("\nEnter its coefficients: ");

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

scanf("%d",&a[i]);

printf("\nEnter the degree of the second polynomial: ");

scanf("%d",&n);

printf("\nEnter its coefficients: ");

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

scanf("%d",&b[i]);

for (i=0;i<=(m+n);++i)

c[i]=0;

for (l=(m+n);l>=0;--l)

{

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

{

for (j=0;j<=n;++j)

if (((i+j)==l) && (i>=0) && (i<=m) && (j>=0) && (j<=n))

c[k]+= (a[m-i])*(b[n-j]);

}

++k;

}

printf("\nThe desired poynomial is: \n\n");

printf("%dx^%d",c[0],(m+n));

for (i=(m+n-1);i>=1;--i)

if (c[m+n-i]>=0)

printf("+%dx^%d",c[m+n-i],i);

else

printf("%dx^%d",c[m+n-i],i);

if (c[m+n]>=0)

printf("+%d",c[m+n]);

else

printf("%d",c[m+n]);

printf("\n\n");

}

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Arrays may represent the coefficients, eg:

a(x) = 2+x+x3, b(x)=x+6x4, c(x)=a(x)+b(x)

a[0]= 2; a[1]=1; a[2]= 0; a[3]=1

b[0]= 1; b[1]=0; b[2]= 0; b[3]=0; b[4]=6 for (i=0; i<=4; ++i) c[i] = a[i]+b[i];

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Write a algorithm to add two polynomials using aaray?

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to write a program in C programming language to multiply two polynomials?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is there a program that can help you make a programming language but you don't need to know a programming language?

No. In order to make or use a program or a programming language, you need to know a programming language.


Is there a c program to multiply two polynomials using linked lists?

yes


What is the work of compiler?

A compiler converts a program in one programming language into a program in another programming language. Often the conversion is into a language that can be understood directly by the hardware.


A common program language?

Yes, C is a common programming language.


Which program uses actionscript as its programming language?

Adobe Flash uses actionscript. It is the main programming language for flash.


Why do you have program language?

Because if we did not then it would be D programming


Is COBOL an application program?

No, COBOL is a programming language.


Why program in c language?

It is the most common programming language, but of course there are many others. It is one of the easiest computer programming.


What program does prolog belong to?

Prolog does not belong to any program, it is a programming language.


What has the author Carl Feingold written?

Carl Feingold has written: 'Fundamentals of COBOL programming' -- subject(s): COBOL (Computer program language) 'RPG II programming' -- subject(s): RPG (Computer program language) 'Fundamentals of structured COBOL programming' -- subject(s): COBOL (Computer program language), Structured programming


Technology and programming language?

program means set of statements.and coming to programming languages , the program which is developed in any language i.e c,c++,java etc


What has the author Bryan Meyers written?

Bryan Meyers has written: 'Desktop guide to CL programming' -- subject(s): Job Control Language (Computer program language), Programming, IBM computers 'RPG IV jump start' -- subject(s): RPG IV (Computer program language) 'Control language programming for the AS/400' -- subject(s): IBM AS/400 (Computer), Programming, Job Control Language (Computer program language)