SlideShare une entreprise Scribd logo
1  sur  20
Data Structures
FJWU
Dept.
BS Software Engineering
Sparse Matrix and
Polynomials
Aroosa
Neelum Raffique
Saba Arshad
Group Members
3
Sparse Matrix
•
A matrix is sparse if many of its elements are zero
•
A matrix that is not sparse is dense
•
The boundary is not precisely defined
•
Diagonal and tridiagonal matrices are sparse
•
We classify triangular matrices as dense
•
Two possible representations
•
array
•
linked list
SPARSE MATRIX
Sparse matrix of dimension 7 x 7.
COLUMNS
0 1 2 3 4 5 6
0 0 0 0 -5 0 0 0
1 0 4 0 0 0 0 7
2 0 0 0 0 9 0 0
ROWS 3 0 3 0 2 0 0
0
4 1 0 2 0 0 0 0
5 0 0 0 0 0 0 0
6 0 0 8 0 0 0 0
SOURCE CODE:
/*Program to demonstrate addition and multiplication of Two Sparse Matrix */
#include < iostream.h >
#include < conio.h >
#define x 25
class sparce
{
private:
int a [ x ] [ x ], b [ x ] [ x ], c [ x ] [ x ], m, n, p, q;
public:
void init ( );
void input ( );
void add ( );
void mul ( );
void display ( int [25][25], int, int );
void convert( int [25][25], int, int );
};
void sparce :: init ( )
{
int i, j;
for(i = 0; i < x;i + + )
for( j = 0; j < x; j + +)
c [ i ] [ j ] = 0;
}
cont...
void sparce :: input()
{
int i,j;
cout<<"nEnter order Of First matrix::";
cin>>m>>n;
cout<<"nEnter order Of Second matrix::";
cin>>p>>q;
cout<<"nEnter"<<m*n<<"Elements Into First Matrixn";
for(i=0;i<m;i++)
for( j = 0; j < n; j + + )
cin>> a[ i ] [ j ];
cout<<"nEnter"<<p*q<<"Elements Into Second Matrixn";
for(i = 0; i < p ; i + + )
for ( j = 0; j < q ; j + + )
cin>>b [ i ] [ j ];
}
void sparce :: add ( )
{
int i, j;
if( m = = p && n = = q )
{
for( i = 0 ; i < m ; i + + )
for( j = 0; j < n; j + + )
c[ i ] [ j ] = a [ i ][ j ] + b [ i ] [ j ];
convert( c, m, n);
}
else
cout<<"nAddition Is Not Possible";
}
continue...
void sparce :: mul ( )
{
int i, j, k;
if(n = = p)
{
for( i = 0; i < m; i + +)
for( j = 0; j < q; j + + )
for( k = 0; k < n; k + + )
c[ I ] [ j ] + = a [ I ] [ k ] * b [ k ] [ j ];
convert(c, m, n);
}
else
cout<<"n Multiplecation Is Not Possible";
}
void sparce :: display(int c[25][25], int m, int n)
{
int i,j;
for( i = 0 ;i < m; i + + )
{
for( j = 0 ; j < n ; j + + )
cout<<c [ i ] [ j ]<<"t";
cout<<"n";
}
}
void sparce :: convert(int c[25][25], int m, int n)
{
int i, j, k = 1,t = 0;
int sp[25][25];
for( i = 0 ; i < m ; i + +)
for( j = 0 ; j < n ; j + + )
if(c [ i ] [ j ] ! = 0 )
{
sp [ k ] [ 0 ] = i;
sp [ k ] [ 1 ] = j;
sp [ k ] [ 2 ] = c [ i ] [ j ];
k + + ;
t + + ;
}
sp[ 0 ] [ 0 ] = m;
sp[ 0 ] [ 1 ] = n;
sp[ 0 ] [ 2 ] = t;
display( sp, k, 3);
}
void main ( )
{
sparce ob;
clrscr ( );
ob.init ( );
ob.input ( );
cout<<"nAddition of Two Sparce Matrixn";
ob.add ( );
ob.init ( );
cout<<"nMultiplecation Of Two Sparce Matrixn";
ob.mul ( );
getch ( );
}
OUTPUT:
Polynomials
Polynomial terms have variables which are raised to whole-
number exponents (or else the terms are just plain
numbers); there are no square roots of variables, no
fractional powers, and no variables in the denominator of
any fractions. Here are some examples:
Polynomial:
• How to implement this?
There are different ways of implementing
the polynomial ADT:
• Array (not recommended)
• Linked List (preferred and recommended)
Polynomial:
•Array Implementation:
• p1(x) = 8x3
+ 3x2
+ 2x + 6
• p2(x) = 23x4
+ 18x - 3
6 2 3 8
0 2
Index
represents
exponents
-3 18 0 0 23
0 42
p1(x) p2(x)
•This is why arrays aren’t good to represent
polynomials:
• p3(x) = 16x21
- 3x5
+ 2x + 6
Polynomial:
6 2 0 0 -3 0 0 16…………
WASTE OF SPACE!
• Advantages of using an Array:
• only good for non-sparse polynomials.
• ease of storage and retrieval.
• Disadvantages of using an Array:
• have to allocate array size ahead of
time.
• huge array size required for sparse
polynomials. Waste of space and runtime.
Polynomial:
• Linked list Implementation:
• p1(x) = 23x9
+ 18x7
+ 41x6
+ 163x4
+ 3
• p2(x) = 4x6
+ 10x4
+ 12x + 8
23 9 18 7 41 6 18 7 3 0
4 6 10 4 12 1 8 0
P1
P2
NODE (contains coefficient & exponent)
TAIL (contains pointer)
Polynomial:
• Advantages of using a Linked list:
• save space (don’t have to worry about
sparse polynomials) and easy to maintain
• don’t need to allocate list size and can
declare nodes (terms) only as needed
• Disadvantages of using a Linked list :
• can’t go backwards through the list
• can’t jump to the beginning of the list
from the end.
Polynomial:
SOURCE CODE:
/*Program To Demonstrate Addition And Multiplication Of Two Polynomial Expression */
#include < iostream.h >
#include < conio.h >
#define n 100
class poly
{
private:
int a[n], b[n], add[n], mul[n], p, q, at;
public:
void init ( );
void input ( );
void process ( );
void display ( );
};
void poly :: init ( )
{
int i;
for( i = 0; i < n; i + + )
a[ i ] = b [ i ] = add[ i ] = mul[ i ] = 0;
}
void poly :: input ( )
{
int i;
cout<<"nEnter Degree Of First Polynomial::";
cin>>p;
cout<<"nEnter Degree Of Second
Polynomial::";
cin>>q;
cout<<"nEnter Values First
Polynomialn";
for( i = 0; i <= p; i + + )
{
cout<<"nEnter X^"<<i<<" Th
Coefficient";
cin>>a[ i ];
}
cout<<"nEnter Values First
Polynomialn";
for( i = 0; i <= q; i + + )
{
cout<<"nEnter X^"<<i<<" Th
Coefficient";
cin>>b[ i ];
}
}
void poly :: process ( )
{
int i, j;
if( p > q )
at = p;
else
at = q;
for ( i = 0; i <= at; i + +)
add[ i ] = a[ i ] + b[ i ];
for( i = 0; i <= p; i + + )
for( j = 0; j <= q; j + + )
mul [ i + j ] + = a [ i ] * b [ j ];
}
void poly :: display ( )
{
int i;
cout<<"Addition Of Two Polynomial Expressions Arenn";
for( i = at; i >=0 ; i - -)
cout<<add[i]<<"X^"<<i<<"+";
cout<<"nnMultiplecation Of Two Polynomial Expressions Arenn";
for( i = p + q; i > = 0; i - -)
cout<<mul[i]<<"X^"<< i <<"+";
}
void main()
{
poly ob;
clrscr ( );
ob.init ( );
ob.input ( );
ob.process ( );
ob.display ( );
getch ( );
}
OUTPUT:

Contenu connexe

Tendances

03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf
KkSingh64
 
Hospital Management system Database design
Hospital Management system Database designHospital Management system Database design
Hospital Management system Database design
Elias Dinsa
 
Modelling System Requirements: Events & Things
Modelling System Requirements: Events & ThingsModelling System Requirements: Events & Things
Modelling System Requirements: Events & Things
wmomoni
 

Tendances (20)

Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf
 
Arrays
ArraysArrays
Arrays
 
ERD rumah sakit
ERD rumah sakitERD rumah sakit
ERD rumah sakit
 
Database-Management-System-PPT.pptx
Database-Management-System-PPT.pptxDatabase-Management-System-PPT.pptx
Database-Management-System-PPT.pptx
 
Hospital Management system Database design
Hospital Management system Database designHospital Management system Database design
Hospital Management system Database design
 
Ngo management system.
Ngo management system.Ngo management system.
Ngo management system.
 
6. Stack (Struktur Data)
6. Stack (Struktur Data)6. Stack (Struktur Data)
6. Stack (Struktur Data)
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
Instruksi Aritmatika
Instruksi AritmatikaInstruksi Aritmatika
Instruksi Aritmatika
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
Linked List dalam Struktur Data
Linked List dalam Struktur DataLinked List dalam Struktur Data
Linked List dalam Struktur Data
 
Data structures chapter 1
Data structures chapter  1Data structures chapter  1
Data structures chapter 1
 
Relational database management system (rdbms) i
Relational database management system (rdbms) iRelational database management system (rdbms) i
Relational database management system (rdbms) i
 
Data structures - unit 1
Data structures - unit 1Data structures - unit 1
Data structures - unit 1
 
Elmasri Navathe DBMS Unit-1 ppt
Elmasri Navathe DBMS Unit-1 pptElmasri Navathe DBMS Unit-1 ppt
Elmasri Navathe DBMS Unit-1 ppt
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Data Dictionary
Data DictionaryData Dictionary
Data Dictionary
 
Modelling System Requirements: Events & Things
Modelling System Requirements: Events & ThingsModelling System Requirements: Events & Things
Modelling System Requirements: Events & Things
 

Similaire à Sparse Matrix and Polynomial

Similaire à Sparse Matrix and Polynomial (20)

Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
A scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codeA scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ code
 
PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...
 
Arrays
ArraysArrays
Arrays
 
Los dskn
Los dsknLos dskn
Los dskn
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppt
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
C programs
C programsC programs
C programs
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
Parameters
ParametersParameters
Parameters
 
.net progrmming part2
.net progrmming part2.net progrmming part2
.net progrmming part2
 
Array
ArrayArray
Array
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
C programs
C programsC programs
C programs
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
Frsa
FrsaFrsa
Frsa
 
2D array
2D array2D array
2D array
 
C arrays
C arraysC arrays
C arrays
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical Reviewer
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 

Dernier

Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 

Dernier (20)

Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 

Sparse Matrix and Polynomial

  • 1. Data Structures FJWU Dept. BS Software Engineering Sparse Matrix and Polynomials
  • 3. 3 Sparse Matrix • A matrix is sparse if many of its elements are zero • A matrix that is not sparse is dense • The boundary is not precisely defined • Diagonal and tridiagonal matrices are sparse • We classify triangular matrices as dense • Two possible representations • array • linked list
  • 4. SPARSE MATRIX Sparse matrix of dimension 7 x 7. COLUMNS 0 1 2 3 4 5 6 0 0 0 0 -5 0 0 0 1 0 4 0 0 0 0 7 2 0 0 0 0 9 0 0 ROWS 3 0 3 0 2 0 0 0 4 1 0 2 0 0 0 0 5 0 0 0 0 0 0 0 6 0 0 8 0 0 0 0
  • 5. SOURCE CODE: /*Program to demonstrate addition and multiplication of Two Sparse Matrix */ #include < iostream.h > #include < conio.h > #define x 25 class sparce { private: int a [ x ] [ x ], b [ x ] [ x ], c [ x ] [ x ], m, n, p, q; public: void init ( ); void input ( ); void add ( ); void mul ( ); void display ( int [25][25], int, int ); void convert( int [25][25], int, int ); }; void sparce :: init ( ) { int i, j; for(i = 0; i < x;i + + ) for( j = 0; j < x; j + +) c [ i ] [ j ] = 0; }
  • 6. cont... void sparce :: input() { int i,j; cout<<"nEnter order Of First matrix::"; cin>>m>>n; cout<<"nEnter order Of Second matrix::"; cin>>p>>q; cout<<"nEnter"<<m*n<<"Elements Into First Matrixn"; for(i=0;i<m;i++) for( j = 0; j < n; j + + ) cin>> a[ i ] [ j ]; cout<<"nEnter"<<p*q<<"Elements Into Second Matrixn"; for(i = 0; i < p ; i + + ) for ( j = 0; j < q ; j + + ) cin>>b [ i ] [ j ]; } void sparce :: add ( ) { int i, j; if( m = = p && n = = q ) { for( i = 0 ; i < m ; i + + ) for( j = 0; j < n; j + + ) c[ i ] [ j ] = a [ i ][ j ] + b [ i ] [ j ]; convert( c, m, n); } else cout<<"nAddition Is Not Possible"; }
  • 7. continue... void sparce :: mul ( ) { int i, j, k; if(n = = p) { for( i = 0; i < m; i + +) for( j = 0; j < q; j + + ) for( k = 0; k < n; k + + ) c[ I ] [ j ] + = a [ I ] [ k ] * b [ k ] [ j ]; convert(c, m, n); } else cout<<"n Multiplecation Is Not Possible"; } void sparce :: display(int c[25][25], int m, int n) { int i,j; for( i = 0 ;i < m; i + + ) { for( j = 0 ; j < n ; j + + ) cout<<c [ i ] [ j ]<<"t"; cout<<"n"; } }
  • 8. void sparce :: convert(int c[25][25], int m, int n) { int i, j, k = 1,t = 0; int sp[25][25]; for( i = 0 ; i < m ; i + +) for( j = 0 ; j < n ; j + + ) if(c [ i ] [ j ] ! = 0 ) { sp [ k ] [ 0 ] = i; sp [ k ] [ 1 ] = j; sp [ k ] [ 2 ] = c [ i ] [ j ]; k + + ; t + + ; } sp[ 0 ] [ 0 ] = m; sp[ 0 ] [ 1 ] = n; sp[ 0 ] [ 2 ] = t; display( sp, k, 3); } void main ( ) { sparce ob; clrscr ( ); ob.init ( ); ob.input ( ); cout<<"nAddition of Two Sparce Matrixn"; ob.add ( ); ob.init ( ); cout<<"nMultiplecation Of Two Sparce Matrixn"; ob.mul ( ); getch ( ); }
  • 10. Polynomials Polynomial terms have variables which are raised to whole- number exponents (or else the terms are just plain numbers); there are no square roots of variables, no fractional powers, and no variables in the denominator of any fractions. Here are some examples:
  • 11. Polynomial: • How to implement this? There are different ways of implementing the polynomial ADT: • Array (not recommended) • Linked List (preferred and recommended)
  • 12. Polynomial: •Array Implementation: • p1(x) = 8x3 + 3x2 + 2x + 6 • p2(x) = 23x4 + 18x - 3 6 2 3 8 0 2 Index represents exponents -3 18 0 0 23 0 42 p1(x) p2(x)
  • 13. •This is why arrays aren’t good to represent polynomials: • p3(x) = 16x21 - 3x5 + 2x + 6 Polynomial: 6 2 0 0 -3 0 0 16………… WASTE OF SPACE!
  • 14. • Advantages of using an Array: • only good for non-sparse polynomials. • ease of storage and retrieval. • Disadvantages of using an Array: • have to allocate array size ahead of time. • huge array size required for sparse polynomials. Waste of space and runtime. Polynomial:
  • 15. • Linked list Implementation: • p1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3 • p2(x) = 4x6 + 10x4 + 12x + 8 23 9 18 7 41 6 18 7 3 0 4 6 10 4 12 1 8 0 P1 P2 NODE (contains coefficient & exponent) TAIL (contains pointer) Polynomial:
  • 16. • Advantages of using a Linked list: • save space (don’t have to worry about sparse polynomials) and easy to maintain • don’t need to allocate list size and can declare nodes (terms) only as needed • Disadvantages of using a Linked list : • can’t go backwards through the list • can’t jump to the beginning of the list from the end. Polynomial:
  • 17. SOURCE CODE: /*Program To Demonstrate Addition And Multiplication Of Two Polynomial Expression */ #include < iostream.h > #include < conio.h > #define n 100 class poly { private: int a[n], b[n], add[n], mul[n], p, q, at; public: void init ( ); void input ( ); void process ( ); void display ( ); }; void poly :: init ( ) { int i; for( i = 0; i < n; i + + ) a[ i ] = b [ i ] = add[ i ] = mul[ i ] = 0; }
  • 18. void poly :: input ( ) { int i; cout<<"nEnter Degree Of First Polynomial::"; cin>>p; cout<<"nEnter Degree Of Second Polynomial::"; cin>>q; cout<<"nEnter Values First Polynomialn"; for( i = 0; i <= p; i + + ) { cout<<"nEnter X^"<<i<<" Th Coefficient"; cin>>a[ i ]; } cout<<"nEnter Values First Polynomialn"; for( i = 0; i <= q; i + + ) { cout<<"nEnter X^"<<i<<" Th Coefficient"; cin>>b[ i ]; } }
  • 19. void poly :: process ( ) { int i, j; if( p > q ) at = p; else at = q; for ( i = 0; i <= at; i + +) add[ i ] = a[ i ] + b[ i ]; for( i = 0; i <= p; i + + ) for( j = 0; j <= q; j + + ) mul [ i + j ] + = a [ i ] * b [ j ]; } void poly :: display ( ) { int i; cout<<"Addition Of Two Polynomial Expressions Arenn"; for( i = at; i >=0 ; i - -) cout<<add[i]<<"X^"<<i<<"+"; cout<<"nnMultiplecation Of Two Polynomial Expressions Arenn"; for( i = p + q; i > = 0; i - -) cout<<mul[i]<<"X^"<< i <<"+"; }
  • 20. void main() { poly ob; clrscr ( ); ob.init ( ); ob.input ( ); ob.process ( ); ob.display ( ); getch ( ); } OUTPUT: