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
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)
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:
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 <<"+";
}