cs201- introduction to programming- lecture 44
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 44 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
Introduction to Introduction to ProgrammingProgramming
Lecture 44Lecture 44
class Matrixclass Matrix
{{
private :private :
int numRows , numCols ;int numRows , numCols ;
double ** elements ;double ** elements ;
} ;} ;
Class MatrixClass Matrix
class Matrixclass Matrix
{{
private :private :
int numRows , numCols ;int numRows , numCols ;
double ** elements ;double ** elements ;
public :public :
Matrix ( int = 0 , int = 0 ) ; // Default constructorMatrix ( int = 0 , int = 0 ) ; // Default constructor
Matrix ( const Matrix & ) ; // Copy constructorMatrix ( const Matrix & ) ; // Copy constructor
~ Matrix ( ) ; // Destructor~ Matrix ( ) ; // Destructor
Class MatrixClass Matrix
// Utility functions of Matrix class// Utility functions of Matrix class
int getRows ( void ) const ; int getRows ( void ) const ; int getCols ( void ) const ; int getCols ( void ) const ;
// Input output functions for Matrix class// Input output functions for Matrix class
const Matrix & input ( istream & is = cin ) ; const Matrix & input ( istream & is = cin ) ; const Matrix & input ( ifstream & is ) ; const Matrix & input ( ifstream & is ) ;
void output ( ostream & os = cout ) const ; void output ( ostream & os = cout ) const ; void output ( ofstream & os ) const ; void output ( ofstream & os ) const ;
Class MatrixClass Matrix
// Plus Operator// Plus OperatorMatrix operator + ( Matrix & m ) const ; Matrix operator + ( Matrix & m ) const ; Matrix operator + ( double d ) const ;Matrix operator + ( double d ) const ;
Class MatrixClass Matrix
A + A + d ;d ;
‘A’ is an object of a class Matrix
d is a variable of type double
d + d + A ;A ;
d is a variable of type double
‘a’ is an object of a
class Matrix
// Plus Operator // Plus Operator
Matrix operator + ( Matrix & m ) const ; Matrix operator + ( Matrix & m ) const ; Matrix operator + ( double d ) const ;Matrix operator + ( double d ) const ;friend Matrix operator + ( double d , Matrix & m ) friend Matrix operator + ( double d , Matrix & m )
;;const Matrix & operator += ( Matrix & m ) ;const Matrix & operator += ( Matrix & m ) ;
Class MatrixClass Matrix
i += 3 ;i += 3 ;
i = i + 3 ;i = i + 3 ;
A += B ; // A and B are MatricesA += B ; // A and B are Matrices
A – BA – B
Where A and B Where A and B are both are both matricesmatrices
A – d ;A – d ;‘A’ is an
object of a class Matrix
d is a variable of type double
d – A ;d – A ;
d is a variable of type double
‘a’ is an object of a
class Matrix
// Minus Operator// Minus Operator
Matrix operator - ( Matrix & m ) const ; Matrix operator - ( Matrix & m ) const ;
Matrix operator - ( double d ) const ;Matrix operator - ( double d ) const ;
friend Matrix operator - ( double d , Matrix & m ) ;friend Matrix operator - ( double d , Matrix & m ) ;
Class MatrixClass Matrix
A * B ;A * B ;Where A and B are both matrices
A * d ;A * d ;‘A’ is an
object of a class Matrix
d is a variable of type double
d * A ;d * A ;d is a
variable of type double
‘a’ is an object of a
class Matrix
// Multiplication Operator// Multiplication Operator
Matrix operator * ( const Matrix & m ) ;Matrix operator * ( const Matrix & m ) ;Matrix operator * ( double d ) const ;Matrix operator * ( double d ) const ;friend Matrix operator * ( const double d , const Matrix & friend Matrix operator * ( const double d , const Matrix &
m ) ; m ) ;
Class MatrixClass Matrix
A / A / d ;d ;
‘A’ is an object of a
class Matrix
d is a variable of type double
Class MatrixClass Matrix
// Division Operator// Division Operator
Matrix operator / ( const double d ) Matrix operator / ( const double d ) ;;
// Stream Insertion and Extraction Operator// Stream Insertion and Extraction Operator
cin >> m ;cin >> m ; // Where m is a matrix// Where m is a matrix
Example Example
// Stream Insertion and Extraction Operator// Stream Insertion and Extraction Operator
friend istream & operator >> ( istream & , Matrix & ) ;friend istream & operator >> ( istream & , Matrix & ) ;friend ifstream & operator >> ( ifstream & , Matrix & ) ;friend ifstream & operator >> ( ifstream & , Matrix & ) ;
friend istream & operator << ( istream & , Matrix & ) ;friend istream & operator << ( istream & , Matrix & ) ;friend ifstream & operator << ( ifstream & , Matrix & ) ;friend ifstream & operator << ( ifstream & , Matrix & ) ;
Class MatrixClass Matrix
const Matrix & operator = ( const Matrix & m ) ; const Matrix & operator = ( const Matrix & m ) ;
const Matrix & transpose ( void ) ; const Matrix & transpose ( void ) ;
Class MatrixClass Matrix
Matrix :: Matrix ( int row , int col ) // Default Matrix :: Matrix ( int row , int col ) // Default ConstructorConstructor
{{ numRows = row ;numRows = row ; numCols = col ;numCols = col ; elements = new ( double * ) [ numRows ] ;elements = new ( double * ) [ numRows ] ; for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ )
{{ elements [ i ] = new double [ numCols ] ;elements [ i ] = new double [ numCols ] ; for ( int j = 0 ; j < numCols ; j ++ )for ( int j = 0 ; j < numCols ; j ++ ) elements [ i ] [ j ] = 0.0 ; elements [ i ] [ j ] = 0.0 ; }}}}
Class MatrixClass Matrix
Matrix A ( B ) Matrix A ( B ) ;;
Matrix A = B Matrix A = B ;;
Matrix :: Matrix ( const Matrix & m )Matrix :: Matrix ( const Matrix & m ){{ numRows = m.numRows ;numRows = m.numRows ; numCols = m.numCols ;numCols = m.numCols ; elements = new ( double * ) [ numRows ] ;elements = new ( double * ) [ numRows ] ; for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ )
{{ elements [ i ] = new double [ numCols ] ;elements [ i ] = new double [ numCols ] ; for ( int j = 0 ; j < numCols ; j ++ )for ( int j = 0 ; j < numCols ; j ++ ) elements [ i ] [ j ] = m.elements [ i ] [ j ] ;elements [ i ] [ j ] = m.elements [ i ] [ j ] ; }}}}
Class MatrixClass Matrix
Matrix :: ~ Matrix ( void )Matrix :: ~ Matrix ( void )
{{
delete [ ] elements ;delete [ ] elements ;
}}
Class MatrixClass Matrix
int Matrix :: getRows ( ) constint Matrix :: getRows ( ) const
{{
return numRows ;return numRows ;
}}
int Matrix :: getCols ( ) constint Matrix :: getCols ( ) const
{{
return numCols ;return numCols ;
}}
Class MatrixClass Matrix
void Matrix :: output ( ostream & os ) constvoid Matrix :: output ( ostream & os ) const{{ // Print first row with special characters// Print first row with special characters os.setf ( ios :: showpoint ) ;os.setf ( ios :: showpoint ) ; os.setf ( ios :: fixed , ios :: floatfield ) ;os.setf ( ios :: fixed , ios :: floatfield ) ; os << ( char ) 218 ;os << ( char ) 218 ; for ( int j = 0 ; j < numCols ; j ++ )for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 10 ) << " “ ;os << setw ( 10 ) << " “ ; os << ( char ) 191 << "\n" ;os << ( char ) 191 << "\n" ;
Class MatrixClass Matrix
Class MatrixClass Matrix
// Print remaining rows with vertical bars only// Print remaining rows with vertical bars only for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ ) {{ os << ( char ) 179 ;os << ( char ) 179 ; for ( int j = 0 ; j < numCols ; j ++ )for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 10 ) << setprecision ( 2 ) << elements [ i ] [ j ] ;os << setw ( 10 ) << setprecision ( 2 ) << elements [ i ] [ j ] ; os << ( char ) 179 << "\n" ;os << ( char ) 179 << "\n" ; }}
Class MatrixClass Matrix
// Print last row with special characters// Print last row with special characters
os << ( char ) 192 ;os << ( char ) 192 ;
for ( int j = 0 ; j < numCols ; j ++ )for ( int j = 0 ; j < numCols ; j ++ )
os << setw ( 10 ) << " " ;os << setw ( 10 ) << " " ;
os << ( char ) 217 << "\n" ;os << ( char ) 217 << "\n" ;
}}
void Matrix :: output ( ofstream & os ) constvoid Matrix :: output ( ofstream & os ) const{{
os.setf ( ios :: showpoint ) ;os.setf ( ios :: showpoint ) ; os.setf ( ios :: fixed , ios :: floatfield ) ;os.setf ( ios :: fixed , ios :: floatfield ) ; os << numRows << " " << numCols << "\n" ;os << numRows << " " << numCols << "\n" ; for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ )
{{ for ( int j = 0 ; j < numCols ; j ++ )for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 6 ) << setprecision ( 2 ) << elements [ i ] [ j ] ;os << setw ( 6 ) << setprecision ( 2 ) << elements [ i ] [ j ] ; os << "\n" ;os << "\n" ; }}}}
Class MatrixClass Matrix
const Matrix & Matrix :: input ( istream & is )const Matrix & Matrix :: input ( istream & is ){{ cout << "Input Matrix size: " << numRows << " rows by " << numCols << " columns \n" ;cout << "Input Matrix size: " << numRows << " rows by " << numCols << " columns \n" ; for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ )
{{ cout << "Please enter " << numCols << " values separated by spaces for row no." << i+1 cout << "Please enter " << numCols << " values separated by spaces for row no." << i+1
<< ": " ;<< ": " ; for ( int j = 0 ; j < numCols ; j ++ )for ( int j = 0 ; j < numCols ; j ++ )
{{ cin >> elements [ i ] [ j ] ;cin >> elements [ i ] [ j ] ; }} }} return * this ;return * this ;}}
Class MatrixClass Matrix
const Matrix & Matrix :: input ( ifstream & is )const Matrix & Matrix :: input ( ifstream & is ){{ int Rows , Cols ;int Rows , Cols ; is >> Rows ;is >> Rows ; is >> Cols ;is >> Cols ; if ( Rows > 0 && Cols > 0 )if ( Rows > 0 && Cols > 0 )
{{ Matrix temp ( Rows , Cols ) ;Matrix temp ( Rows , Cols ) ; * this = temp ;* this = temp ; for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ )
{{ for ( int j = 0 ; j < numCols ; j ++ )for ( int j = 0 ; j < numCols ; j ++ )
{{ is >> elements [ i ] [ j ] ;is >> elements [ i ] [ j ] ; }} }} }} return * this ;return * this ;}}
Class MatrixClass Matrix
const Matrix & Matrix :: transpose ( )const Matrix & Matrix :: transpose ( ){{ if ( numRows == numCols )if ( numRows == numCols ) // Square Matrix// Square Matrix { { double temp ;double temp ; for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ )
{{ for ( int j = i + 1 ; j < numCols ; j ++ )for ( int j = i + 1 ; j < numCols ; j ++ )
{{ temp = elements [ i ] [ j ];temp = elements [ i ] [ j ]; elements [ i ] [ j ] = elements [ j ] [ i ] ;elements [ i ] [ j ] = elements [ j ] [ i ] ; elements [ j ] [ i ] = temp ;elements [ j ] [ i ] = temp ; }} }} }}
Class MatrixClass Matrix
Class MatrixClass Matrixelseelse{{
Matrix temp(numCols, numRows);Matrix temp(numCols, numRows); for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ )
{{ for ( int j = 0 ; j < numCols ; j ++ )for ( int j = 0 ; j < numCols ; j ++ )
{{ temp.elements [ j ] [ i ] = elements [ i ] [ j temp.elements [ j ] [ i ] = elements [ i ] [ j
] ;] ; }} }} * this = temp ;* this = temp ; }} return * this ;return * this ;}}