Logic: define a structure "struct poly" for storing terms of polynomial i.e. two integers viz int coeff and int expo
now define poly1 as a array of struct poly.."poly poly1[10]" which will store max 10 terms.
same define poly2[10] and poly3[20]
write 3 seperate function for read poly ,display poly and addpoly
read poly- int readPoly(poly p[10])
write function for scanning each term i.e. coeff and expo and save respective array of struct poly. (use for loop )
read function should return number of terms hence data type is int.
Code for scan poly:
printf("\n Enter the total number of terms in the polynomial:");
scanf("%d",&t1);
printf("\n Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER\n");
for(i=0;i<t1;i++)
{
printf("\n Enter the Coefficient and Exponent (%d): ",i+1);
scanf("%d\t%d",&p[i].coeff,&p[i].expo);
}
Display poly- void displayPoly( poly [],int terms);
Write Display function for display polynomial on outut window.
Run a for loop from 0 to terms-1 and display coefficient and exponent of respective terms of polynomial.
Code for Display poly:
for(l=0;l<term-1;l++)
printf("%dx^%d+",p[l].coeff,p[l].expo);
printf("%dx^%d",p[l].coeff,p[l].expo);
Addition of two polynomial:
use three pointer
i- for first poly
j-for second poly
k for third poly for storing result
use double for loop for i and j or use while loop to access terms of polynomial
compare exponent of poly1 and poly2...
1) if exponent of poly1 and poly2 are equal
if(poly1[i].expo==poly2[j].expo)
then copy exponent to poly3 also add coefficient of poly1 and poly 2 and assign it to coefficient of poly3.
in this case increment i,j,k pointer and move on to next terms of all poly.
poly3[k].coeff=poly1[i].coeff + poly2[j].coeff;
poly3[k].expo=poly1[i].expo;
i++;
j++;
k++;
2)if exponent of poly 1 is greater than poly2
if(poly1[i].expo>poly2[j].expo)
then assign poly3 term with poly 1 term and increment only i and k, as second term not copied to poly 3,dont increment poly 2 pointer.
poly3[k].coeff=poly1[i].coeff;
poly3[k].expo=poly1[i].expo;
i++;
k++;
3) last case will be exponent of poly2 is greater than poly1
if(poly1[i].expo>poly2[j].expo)
hence copy poly2 term into poly3 ,in this case increment j and k pointer .
poly3[k].coeff= poly2[j].coeff;
poly3[k].expo=poly2[j].expo;
j++;
k++;
Final Code for addition of polynomial using structure:
#include<stdio.h>
/* declare structure for polynomial */
typedef struct poly
{
int coeff;
int expo;
}poly;
// declare three arrays p1, p2, p3 of type structure poly.
//each polynomial can have maximum of ten terms
// addition result of p1 and p2 is stored in p3
poly poly1[10],poly2[10],poly3[20];
int m=0; //pointer to show output in each pass
//function prototypes
int readPoly(poly []);
int addPoly(poly [],poly [],int ,int ,poly []);
void displayPoly( poly [],int terms);
int main()
{
int t1,t2,t3;
// read and display first polynomial
t1=readPoly(poly1);
printf(" \n Entered First polynomial is : ");
displayPoly(poly1,t1);
//read and display second polynomial
t2=readPoly(poly2);
printf(" \n Entered Second polynomial is : ");
displayPoly(poly2,t2);
//add two polynomials and display resultant polynomial
t3=addPoly(poly1,poly2,t1,t2,poly3);
printf(" \n\n Resultant polynomial after addition : ");
displayPoly(poly3,t3);
printf("\n");
return 0;
}
int readPoly(poly p[10])
{
int t1,i;
printf("\n Enter the total number of terms in the polynomial:");
scanf("%d",&t1);
printf("\n Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER\n");
for(i=0;i<t1;i++)
{
printf("\n Enter the Coefficient and Exponent (%d): ",i+1);
scanf("%d\t%d",&p[i].coeff,&p[i].expo);
}
return(t1);
}
int addPoly(poly poly1[10],poly poly2[10],int t1,int t2,poly poly3[10])
{
int i,j,k;
i=0;
j=0;
k=0;
while(i<t1 && j<t2)
{m++;
if(poly1[i].expo==poly2[j].expo)
{
poly3[k].coeff=poly1[i].coeff + poly2[j].coeff;
poly3[k].expo=poly1[i].expo;
printf("\n addition of polynomial at %d pass:",m);
displayPoly(poly3,m);
i++;
j++;
k++;
}
else if(poly1[i].expo>poly2[j].expo)
{
poly3[k].coeff=poly1[i].coeff;
poly3[k].expo=poly1[i].expo;
printf("\n addition of polynomial at %d pass:",m);
displayPoly(poly3,m);
i++;
k++;
}
else
{
poly3[k].coeff=poly2[j].coeff;
poly3[k].expo=poly2[j].expo;
printf("\n addition of polynomial at %d pass:",m);
displayPoly(poly3,m);
j++;
k++;
}
}
// for rest over terms of polynomial 1
while(i<t1)
{
m++;
poly3[k].coeff=poly1[i].coeff;
poly3[k].expo=poly1[i].expo;
printf("\n addition of polynomial at %d pass:",m);
displayPoly(poly3,m);
i++;
k++;
}
// for rest over terms of polynomial 2
while(j<t2)
{
m++;
poly3[k].coeff=poly2[j].coeff;
poly3[k].expo=poly2[j].expo;
printf("\n addition of polynomial at %d pass:",m);
displayPoly(poly3,m);
j++;
k++;
}
return(k); // k is number of terms in resultant polynomial
}
void displayPoly(poly p[10],int term)
{
int l;
for(l=0;l<term-1;l++)
printf("%dx^%d+",p[l].coeff,p[l].expo);
printf("%dx^%d",p[l].coeff,p[l].expo);
}
/*
Enter the total number of terms in the polynomial:3
Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER
Enter the Coefficient and Exponent (1): 6
6
Enter the Coefficient and Exponent (2): 4
4
Enter the Coefficient and Exponent (3): 2
2
Entered First polynomial is : 6x^6+4x^4+2x^2
Enter the total number of terms in the polynomial:3
Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER
Enter the Coefficient and Exponent (1): 6
6
Enter the Coefficient and Exponent (2): 4
4
Enter the Coefficient and Exponent (3): 1
1
Entered Second polynomial is : 6x^6+4x^4+1x^1
addition of polynomial at 1 pass:12x^6
addition of polynomial at 2 pass:12x^6+8x^4
addition of polynomial at 3 pass:12x^6+8x^4+2x^2
addition of polynomial at 4 pass:12x^6+8x^4+2x^2+1x^1
Resultant polynomial after addition : 12x^6+8x^4+2x^2+1x^1
--------------------------------
Process exited after 17.5 seconds with return value 0
Press any key to continue . . .
*/
No comments:
Post a Comment