Polynomial
Representation:
The polynomial can
be represented using following methods:
1. Using structure
of arrays
2.
Using array of structures
1.
Using structure of arrays:
syntax:
struct polynomial{
int degree;
float coeff[100];
} ;
typedef struct
polynomial POLY;
POLY a,b;
a(x)=25x4
+12x2 +2x +7
a.degree=4;
a.coeff[0]=7
a.coeff[1]=2;
a.coeff[2]=12;
a.coeff[3]=0;
a.coeff[4]=25;
b(x)=28x5
+6
b.degree=5;
b.coeff[0]=6
b.coeff[1]=0;
b.coeff[2]=0;
b.coeff[3]=0;
b.coeff[4]=28;
Advantage:
1.This
representation is simple
2.Using this we can
write easy algorithms.
3.If few terms with
zero coefficients are present, this uses less space.
Disadvantage:
If more terms with
zero coefficients are present, this uses more space.
2.Using Array of
structures
In this we use an
array of structures. Instead of using one array for each polynomial
as in the previous method, here we use one array to store all the
polynomials.
Syntax:
typedef struct{
float coeff;
int expon;
}POLY;
POLY p[100];
example:
Concider two
polynomials
a(x)=25x4
+12x2 +2x +7
and b(x)=28x5
+6
advantage:
If more terms with
zero coefficients are present, this uses less space.
Disadvantage:
If more terms with
non-zero coefficients are present, this uses more space(usually twice
the memorey used by previous method).
ARRAY OF STRUCTURES PROGRAM
Write a program to
add two polynomials
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 100
#define
COMPARE(x,y)(((x)==(y))?0:((x)>(y))?1:-1)
int avail;
typedef struct{
float coeff;
int exp;
}POLY;
void attach(POLY *p,
float coeffei, int expei){
if(avail==MAX_SIZE){
printf("Too
many items");
exit(0);
}
p[avail].coeff=coeffei;
p[avail].exp=expei;
avail++;
}
void addpoly(POLY
*p,int startA,int startB,int *startC,int endA,int endB,int *endC){
*startC=avail;
int expe;
float coeffe;
while(startA<=endA&&startB<=endB)
{
switch(COMPARE(p[startA].exp,p[startB].exp))
{
case 0:
coeffe=p[startA].coeff+p[startB].coeff;
if(coeffe!=0)attach(p,coeffe,p[startA].exp);
startA++;
startB++;
break;
case 1:
attach(p,p[startA].coeff,p[startA].exp);
startA++;
break;
default:attach(p,p[startB].coeff,p[startB].exp);
startB++;
}
}
*endC=avail-1;
}
readpoly(POLY *p,int
*start,int *end)
{
int expe;
float coeffe;
int i=0;
*start=avail;
for(;;){
printf("Term
number %d \n",++i);
printf("coeff
"); scanf("%f",&coeffe);
printf("exponent
"); scanf("%d",&expe);
attach(p,coeffe,expe);
if(expe==0) break;
}
*end=avail-1;
}
void printpoly(POLY
*p,int start,int end){
while(start<=end)
{
if(p[start].coeff<0){
printf(" - ");}
else {printf("
+ ");}
printf("%fx^%d
",p[start].coeff,p[start].exp);
start++;
}
}
void main()
{
POLY p[MAX_SIZE];
int
startA,startB,startC;
int endA,endB,endC;
printf("ENTER
THE FIRST POPPLPPYPOMIAL \n");
readpoly(p,&startA,&endA);
printf("ENTER
THE SEC POPPLPPYPOMIAL \n");
readpoly(p,&startB,&endB);
addpoly(p,startA,startB,&startC,endA,endB,&endC);
printf("Sum of
two polynomials \n");
printpoly(p,startC,endC);
}
---------------------------------------------------------
Cobra T-Shirt - Titsanium Art
ReplyDeleteCobra babyliss nano titanium T-Shirt. £10.99. Quantity. Add titanium auto sales to Cart. Select one for Delivery or Pickup Near Me. titanium powder Delivery available sugarboo extra long digital titanium styler on selected orders. black titanium