CS201- Introduction to Programming- Lecture 44

36
Introduction to Introduction to Programming Programming Lecture 44 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 of CS201- Introduction to Programming- Lecture 44

Page 1: CS201- Introduction to Programming- Lecture 44

Introduction to Introduction to ProgrammingProgramming

Lecture 44Lecture 44

Page 2: CS201- Introduction to Programming- Lecture 44

class Matrixclass Matrix

{{

private :private :

int numRows , numCols ;int numRows , numCols ;

double ** elements ;double ** elements ;

} ;} ;

Class MatrixClass Matrix

Page 3: CS201- Introduction to Programming- Lecture 44

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

Page 4: CS201- Introduction to Programming- Lecture 44

// 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

Page 5: CS201- Introduction to Programming- Lecture 44

// 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

Page 6: CS201- Introduction to Programming- Lecture 44

A + A + d ;d ;

‘A’ is an object of a class Matrix

d is a variable of type double

Page 7: CS201- Introduction to Programming- Lecture 44

d + d + A ;A ;

d is a variable of type double

‘a’ is an object of a

class Matrix

Page 8: CS201- Introduction to Programming- Lecture 44

// 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

Page 9: CS201- Introduction to Programming- Lecture 44

i += 3 ;i += 3 ;

i = i + 3 ;i = i + 3 ;

A += B ; // A and B are MatricesA += B ; // A and B are Matrices

Page 10: CS201- Introduction to Programming- Lecture 44

A – BA – B

Where A and B Where A and B are both are both matricesmatrices

Page 11: CS201- Introduction to Programming- Lecture 44

A – d ;A – d ;‘A’ is an

object of a class Matrix

d is a variable of type double

Page 12: CS201- Introduction to Programming- Lecture 44

d – A ;d – A ;

d is a variable of type double

‘a’ is an object of a

class Matrix

Page 13: CS201- Introduction to Programming- Lecture 44

// 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

Page 14: CS201- Introduction to Programming- Lecture 44

A * B ;A * B ;Where A and B are both matrices

Page 15: CS201- Introduction to Programming- Lecture 44

A * d ;A * d ;‘A’ is an

object of a class Matrix

d is a variable of type double

Page 16: CS201- Introduction to Programming- Lecture 44

d * A ;d * A ;d is a

variable of type double

‘a’ is an object of a

class Matrix

Page 17: CS201- Introduction to Programming- Lecture 44

// 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

Page 18: CS201- Introduction to Programming- Lecture 44

A / A / d ;d ;

‘A’ is an object of a

class Matrix

d is a variable of type double

Page 19: CS201- Introduction to Programming- Lecture 44

Class MatrixClass Matrix

// Division Operator// Division Operator

Matrix operator / ( const double d ) Matrix operator / ( const double d ) ;;

Page 20: CS201- Introduction to Programming- Lecture 44

// 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

Page 21: CS201- Introduction to Programming- Lecture 44

// 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

Page 22: CS201- Introduction to Programming- Lecture 44

const Matrix & operator = ( const Matrix & m ) ; const Matrix & operator = ( const Matrix & m ) ;

const Matrix & transpose ( void ) ; const Matrix & transpose ( void ) ;

Class MatrixClass Matrix

Page 23: CS201- Introduction to Programming- Lecture 44

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

Page 24: CS201- Introduction to Programming- Lecture 44

Matrix A ( B ) Matrix A ( B ) ;;

Page 25: CS201- Introduction to Programming- Lecture 44

Matrix A = B Matrix A = B ;;

Page 26: CS201- Introduction to Programming- Lecture 44

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

Page 27: CS201- Introduction to Programming- Lecture 44

Matrix :: ~ Matrix ( void )Matrix :: ~ Matrix ( void )

{{

delete [ ] elements ;delete [ ] elements ;

}}

Class MatrixClass Matrix

Page 28: CS201- Introduction to Programming- Lecture 44

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

Page 29: CS201- Introduction to Programming- Lecture 44

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

Page 30: CS201- Introduction to Programming- Lecture 44

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" ; }}

Page 31: CS201- Introduction to Programming- Lecture 44

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" ;

}}

Page 32: CS201- Introduction to Programming- Lecture 44

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

Page 33: CS201- Introduction to Programming- Lecture 44

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

Page 34: CS201- Introduction to Programming- Lecture 44

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

Page 35: CS201- Introduction to Programming- Lecture 44

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

Page 36: CS201- Introduction to Programming- Lecture 44

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 ;}}