Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


Presentation on theme: "Introduction to Programming"— Presentation transcript:

1 Introduction to Programming
Lecture 44

2 Class Matrix class Matrix { private : int numRows , numCols ;
double ** elements ; } ;

3 Class Matrix class Matrix { private : int numRows , numCols ;
double ** elements ; public : Matrix ( int = 0 , int = 0 ) ; // Default constructor Matrix ( const Matrix & ) ; // Copy constructor ~ Matrix ( ) ; // Destructor

4 Class Matrix // Utility functions of Matrix class
int getRows ( void ) const ; int getCols ( void ) const ; // Input output functions for Matrix class const Matrix & input ( istream & is = cin ) ; const Matrix & input ( ifstream & is ) ; void output ( ostream & os = cout ) const ; void output ( ofstream & os ) const ;

5 Class Matrix // Plus Operator Matrix operator + ( Matrix & m ) const ;
Matrix operator + ( double d ) const ;

6 d is a variable of type double ‘A’ is an object of a class Matrix
A + d ;

7 d is a variable of type double ‘a’ is an object of a class Matrix
d + A ;

8 Class Matrix // Plus Operator Matrix operator + ( Matrix & m ) const ;
Matrix operator + ( double d ) const ; friend Matrix operator + ( double d , Matrix & m ) ; const Matrix & operator += ( Matrix & m ) ;

9 i += 3 ; i = i + 3 ; A += B ; // A and B are Matrices

10 Where A and B are both matrices
A – B Where A and B are both matrices

11 d is a variable of type double ‘A’ is an object of a class Matrix
A – d ;

12 d is a variable of type double ‘a’ is an object of a class Matrix
d – A ;

13 Class Matrix // Minus Operator
Matrix operator - ( Matrix & m ) const ; Matrix operator - ( double d ) const ; friend Matrix operator - ( double d , Matrix & m ) ;

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

15 d is a variable of type double ‘A’ is an object of a class Matrix
A * d ;

16 d is a variable of type double ‘a’ is an object of a class Matrix
d * A ;

17 Class Matrix // Multiplication Operator
Matrix operator * ( const Matrix & m ) ; Matrix operator * ( double d ) const ; friend Matrix operator * ( const double d , const Matrix & m ) ;

18 ‘A’ is an object of a class Matrix d is a variable of type double
A / d ;

19 Class Matrix // Division Operator
Matrix operator / ( const double d ) ;

20 Example // Where m is a matrix
// Stream Insertion and Extraction Operator cin >> m ; // Where m is a matrix

21 Class Matrix // Stream Insertion and Extraction Operator
friend istream & operator >> ( istream & , Matrix & ) ; friend ifstream & operator >> ( ifstream & , Matrix & ) ; friend istream & operator << ( istream & , Matrix & ) ; friend ifstream & operator << ( ifstream & , Matrix & ) ;

22 Class Matrix const Matrix & operator = ( const Matrix & m ) ;
const Matrix & transpose ( void ) ;

23 Class Matrix Matrix :: Matrix ( int row , int col ) // Default Constructor { numRows = row ; numCols = col ; elements = new ( double * ) [ numRows ] ; for ( int i = 0 ; i < numRows ; i ++ ) elements [ i ] = new double [ numCols ] ; for ( int j = 0 ; j < numCols ; j ++ ) elements [ i ] [ j ] = 0.0 ; }

24 Matrix A ( B ) ;

25 Matrix A = B ;

26 Class Matrix Matrix :: Matrix ( const Matrix & m ) {
numRows = m.numRows ; numCols = m.numCols ; elements = new ( double * ) [ numRows ] ; for ( int i = 0 ; i < numRows ; i ++ ) elements [ i ] = new double [ numCols ] ; for ( int j = 0 ; j < numCols ; j ++ ) elements [ i ] [ j ] = m.elements [ i ] [ j ] ; }

27 Class Matrix Matrix :: ~ Matrix ( void ) { delete [ ] elements ; }

28 Class Matrix int Matrix :: getRows ( ) const { return numRows ; }
int Matrix :: getCols ( ) const return numCols ;

29 Class Matrix void Matrix :: output ( ostream & os ) const {
// Print first row with special characters os.setf ( ios :: showpoint ) ; os.setf ( ios :: fixed , ios :: floatfield ) ; os << ( char ) 218 ; for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 10 ) << " “ ; os << ( char ) 191 << "\n" ;

30 Class Matrix // Print remaining rows with vertical bars only
for ( int i = 0 ; i < numRows ; i ++ ) { os << ( char ) 179 ; for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 10 ) << setprecision ( 2 ) << elements [ i ] [ j ] ; os << ( char ) 179 << "\n" ; }

31 Class Matrix // Print last row with special characters
os << ( char ) 192 ; for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 10 ) << " " ; os << ( char ) 217 << "\n" ; }

32 Class Matrix void Matrix :: output ( ofstream & os ) const {
os.setf ( ios :: showpoint ) ; os.setf ( ios :: fixed , ios :: floatfield ) ; os << numRows << " " << numCols << "\n" ; for ( int i = 0 ; i < numRows ; i ++ ) for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 6 ) << setprecision ( 2 ) << elements [ i ] [ j ] ; os << "\n" ; }

33 Class Matrix const Matrix & Matrix :: input ( istream & is ) {
cout << "Input Matrix size: " << numRows << " rows by " << numCols << " columns \n" ; for ( int i = 0 ; i < numRows ; i ++ ) cout << "Please enter " << numCols << " values separated by spaces for row no." << i+1 << ": " ; for ( int j = 0 ; j < numCols ; j ++ ) cin >> elements [ i ] [ j ] ; } return * this ;

34 Class Matrix const Matrix & Matrix :: input ( ifstream & is ) {
int Rows , Cols ; is >> Rows ; is >> Cols ; if ( Rows > 0 && Cols > 0 ) Matrix temp ( Rows , Cols ) ; * this = temp ; for ( int i = 0 ; i < numRows ; i ++ ) for ( int j = 0 ; j < numCols ; j ++ ) is >> elements [ i ] [ j ] ; } return * this ;

35 Class Matrix const Matrix & Matrix :: transpose ( ) {
if ( numRows == numCols ) // Square Matrix double temp ; for ( int i = 0 ; i < numRows ; i ++ ) for ( int j = i + 1 ; j < numCols ; j ++ ) temp = elements [ i ] [ j ]; elements [ i ] [ j ] = elements [ j ] [ i ] ; elements [ j ] [ i ] = temp ; }

36 Class Matrix else { Matrix temp(numCols, numRows);
for ( int i = 0 ; i < numRows ; i ++ ) for ( int j = 0 ; j < numCols ; j ++ ) temp.elements [ j ] [ i ] = elements [ i ] [ j ] ; } * this = temp ; return * this ;


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google