can anyone help me for a simple polynomial addition using list..
no matter how long it is .. but it must be simple ...

Recommended Answers

All 9 Replies

Wait, is it multiplication like your title says, or is it addition? How many polynomial's will their be?
If there are only 2 polynomial's and you want to add: Load monomial's into list 1 (store coefficient and vairable) for polynomial 1. Do the same in another list for polynomial 2. Scan through list 1, and check for dublicate variables. If any vaiable's are found, add them onto matching node's. Do the same for list 2. Scan through list 1 and check for matching variable's in list 2. If they are found, add them into matching node's in list 2. If they are not found, add them to the end of list 2 (in a new node).
Good luck.

hi
can you help me build an activity for a simple program using while statement...thank you

hi
can you help me build an activity for a simple program using while statement...thank you

Please make a new thread. As you can imagine, answering two question's in one thread could get confusing.

polynomial multiplication and addition in the same prog.

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

struct node
{
 signed int coe;
 int pow;
 node *next;
}*start1,*start2,*root,*add_res,*mul_res;

void disp(node *cur)
{
   while(cur!=NULL)
   {
    printf("%dx%d ",cur->coe,cur->pow);
    cur=cur->next;
   }
}

void ins(node **root,int c,int p)
{
  node *temp=new node;
  node *cur=new node;
  cur=*root;
  temp->coe=c;
  temp->pow=p;
  temp->next=NULL;
  int flag=0;
  if(*root==NULL)
  {
   *root=temp;
  }
  else
  {
  //flag=0 for ordinary insertion
  //flag=1 for multiplication
  //without flag, the result will be 2x2 + 3x2
  // and not 5x2(co-eff wont get added for same powers)
   while(cur->next!=NULL && flag==0)
   {
       if(p==cur->pow)
       {
	cur->coe=cur->coe + c;
	flag=1;
       }
	   cur=cur->next;
   }
   if(p==cur->pow && flag==0)   // this is used for multiplication
	cur->coe=cur->coe + c; //addition wont pass into this
   else if(flag==0)                        //condition
     cur->next=temp;
  }

}

void create()
{
 char ch;
 int c,p;
 for(int i=1;i<=2;i++)
 {
  printf("Enter for polynomial %d (decreasing power order)",i);
  ch='c';
  // press s after giving i/p
  while(ch!='s')
  {
   printf("\nCo-eff (space) Power  :");
   scanf("%d %d",&c,&p);
   if(i==1)
    ins(&start1,c,p);
   else if(i==2)
    ins(&start2,c,p);
   scanf("%s",&ch);
  }
  root=NULL;
 }
 printf("Poly 1 \n");
 disp(start1);
 printf("\nPoly 2 \n");
 disp(start2);
}
void mul()
{
 node *cur1=start1,*cur2=start2;
 while(cur2!=NULL)
 {
   while(cur1!=NULL)
   {
    //multiply co-eff and add the powers
    ins(&mul_res,cur1->coe * cur2->coe,cur1->pow + cur2->pow);
    cur1=cur1->next;
   }
  cur2=cur2->next;
  cur1=start1;
 }
printf("\n\nThe MUL res is \n");
disp(mul_res);
}
void add()
{
 node *cur1=start1;
 node *cur2=start2;

 while(cur1!=NULL && cur2!=NULL)
 {
   int k;
   if(cur1->pow == cur2->pow)
   {
    k=cur1->coe + cur2->coe;
    ins(&add_res,k,cur1->pow);
    cur1=cur1->next;
    cur2=cur2->next;
   }
   else if(cur1->pow > cur2->pow)
    {
   ins(&add_res,cur1->coe,cur1->pow);
     cur1=cur1->next;
    }
   else if(cur2->pow > cur1->pow)
    {
     ins(&add_res,cur2->coe,cur2->pow);
     cur2=cur2->next;
    }
 }
// insert the remaining elements of cur2
if(cur1==NULL)
{
 while(cur2!=NULL)
 {
     ins(&add_res,cur2->coe,cur2->pow);
     cur2=cur2->next;
 }
}
// insert the remaining elements of cur1
else
 while(cur1!=NULL)
 {
     ins(&add_res,cur1->coe,cur1->pow);
     cur1=cur1->next;
 }
 printf("\n\nThe ADD res is \n");
 disp(add_res);
}


void main()
{
start1=NULL;
start2=NULL;
add_res=NULL;
mul_res=NULL;
clrscr();
create();
add();
mul();
getch();
}

the output looks like :

Enter for polynomial 1 (decreasing power order)
Co-eff (space) Power :2 3
w

Co-eff (space) Power :4 2
w

Co-eff (space) Power :8 0
s
Enter for polynomial 2 (decreasing power order)
Co-eff (space) Power :3 3
w

Co-eff (space) Power :5 1
w

Co-eff (space) Power :2 0
s
Poly 1
2x3 4x2 8x0
Poly 2
3x3 5x1 2x0

The ADD res is
5x3 4x2 5x1 10x0

The MUL res is
6x6 12x5 48x3 10x4 40x1 8x2 16x0

commented: conio, scanf, void main, clrscr .... LOL, is this joke? -2
commented: C'mon, read the forum rules, don't offer free solutions to one's homework, get a decent compiler, and learn the standard! K? -2

"Boys Make Sh--" ? Is that really your name?

well you certainly did make us some shit, didn't you boy? a real horrible steaming pile of it, and dropped it right on our shiny floor. oh look, you named it with a .CPP extention, too.

I'll bet you $20 it doesn't even work.


.

commented: Marvelous answer :) +8

my paypal account is [email]boysmakeshoffi@gmail.com.[/email] put ur $20 in that...

go and download the editor from this link
http://www.brothersoft.com/turbo-c-182798.html

download my poly.cpp or copy the prog and save it as poly.c or poly.cpp and store that in the appropriate folder and run .

if it doesnt work then i accept that i am sh--..if it works then it must be last login in this site and should not login in daniweb again.
OK?

wait, wait.... you want me to....

download Turbo C and install it on my 21st Century machine, just so I can ... compile some broken-down code that won't work anyhow?

lol! :D


now, seriously. you need to wheel that trash on out of here. Take it home and fix it up. bring us back a C program that can ACTUALLY COMPILE, not some bastardized C/C++ hybrid, and none of these deprecated libraries from 1989.

Then and only then will i bother to compile it and verify it is indeed still broken, anyhow. and then you can go donate $20 to some charity.

otherwise, GTFO.

ok..instead of commenting unnecessarily on my code,post ur own code.

here's a clue, Sherlock: this thread was created in August 2009. The OP hasn't made a single post since. and most likely they never will.

i've got better things to do than write trivial algebraic functions for no audience and no reason.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.