My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
|
Addition of two polynomials in C
Posted Date:
Total Responses: 0
Posted By: Saheli Member Level: Gold Points/Cash: 4
|
#include #include
struct term { int coeff; int expo; struct term *next; };
typedef struct term TERM; void insertend(TERM**,int,int); void polyadd(TERM*,TERM*,TERM**); void display(TERM*);
void main() { int n,i,c,e; TERM *head1,*head2,*head3; head1=head2=head3=NULL; clrscr();
printf("\nPolynomial 1: \n"); printf("\nEnter the number of terms : "); scanf("%d",&n);
for(i=0;i { printf("\nEnter coefficient of x : "); scanf("%d",&c); printf("\nEnter exponent of x : "); scanf("%d",&e); insertend(&head1,c,e); }
printf("\nPolynomial 2: \n"); printf("Enter the number of terms : "); scanf("%d",&n);
for(i=0;i { printf("\nEnter coefficient of x : "); scanf("%d",&c); printf("\nEnter exponent of x : "); scanf("%d",&e); insertend(&head2,c,e); }
polyadd(head1,head2,&head3); display(head3);
getch(); }
void polyadd(TERM *p1,TERM *p2,TERM **p3) { int c,e;
while(p1!=NULL && p2!=NULL) { if(p1->expo>p2->expo) { c=p1->coeff; e=p1->expo; insertend(p3,c,e); p1=p1->next; }
else if(p2->expo>p1->expo) { c=p2->coeff; e=p2->expo; insertend(p3,c,e); p2=p2->next; }
else if(p1->coeff+p2->coeff==0) { p1=p1->next; p2=p2->next; }
else { c=p1->coeff+p2->coeff; e=p1->expo; insertend(p3,c,e); p1=p1->next; p2=p2->next; } }
while(p1!=NULL) { insertend(p3,p1->coeff,p1->expo); p1=p1->next; }
while(p2!=NULL) { insertend(p3,p2->coeff,p2->expo); p2=p2->next; } }
void insertend(TERM **p,int c,int e) { TERM *n,*cur; n=(TERM*)malloc(sizeof(TERM)); n->coeff=c; n->expo=e; n->next=NULL; cur=*p; if(*p==NULL) *p=n; else { while(cur->next!=NULL) cur=cur->next; cur->next=n; } }
void display(TERM *p) { TERM *cur; cur=p; printf("\nCoeff\t\tExpo\n"); while(cur!=NULL) { printf("\n%d\t\t%d",cur->coeff,cur->expo); cur=cur->next; } }
AttachmentsPollynomial add in C (2154-121139-POLYADD.C)
|
Project Feedbacks
|
No feedbacks found. Be the first to respond and make money from revenue sharing program.
|
|
|
| Post Feedback |
|
|
You must Sign In to post a feedback.
|
|
|
|
|
Advertise Here
|