Assignment Operator and Matrix Operators in C++: Lecture 45, Exercises of Computer Programming

The code and explanations for various assignment and matrix operators in c++. Topics include self-assignment, matrix assignment, addition, and multiplication operators. The document also covers the use of pointers and arrays, as well as rules for programming and an introduction to classes and objects.

Typology: Exercises

2011/2012

Uploaded on 11/06/2012

somo
somo 🇮🇳

4.8

(4)

70 documents

1 / 38

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Programming
Lecture 45
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Assignment Operator and Matrix Operators in C++: Lecture 45 and more Exercises Computer Programming in PDF only on Docsity!

Introduction to Programming

Lecture 45

Assignment Operator A = B

Assignment Operator ( A = B ) = C

Self Assignment

A = A

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

Addition Operator

Class Matix Matrix Matrix :: operator + ( Matrix & m ) const { // Check for conformability if ( numRows == m.numRows && numCols == m.numCols ) { Matrix temp ( * this ) ; for ( int i = 0 ; i < numRows ; i ++ ) { for ( int j = 0 ; j < numCols ; j ++) { temp.elements [ i ] [ j ] += m.elements [ i ] [ j ] ; } } return temp ; } Matrix temp ( * this ) ; return temp ; } 8: Docsity.com

+= Operator

Class Matrix const Matrix & Matrix :: operator += ( Matrix & m ) {

  • this = * this + m ; return * this ; }

Class Matrix Matrix Matrix :: operator + ( double d ) const { Matrix temp ( * this ) ; for ( int i = 0 ; i < numRows ; i ++ ) { for ( int j = 0 ; j < numCols ; j ++ ) { temp.elements [ i ] [ j ] += d ; } } return temp ; } Docsity.com


Operator**

Class Matrix Matrix Matrix :: operator * ( const Matrix & m ) { Matrix temp ( numRows , m.numCols ) ; if ( numCols == m.numRows ) { for ( int i = 0 ; i < numRows ; i ++ ) { for ( int j = 0 ; j < m.numCols ; j ++ ) { temp.elements [ i ] [ j ] = 0.0 ; for ( int k = 0 ; k < numCols ; k ++ ) { temp.elements [ i ] [ j ] += elements [ i ] [ k ] * m.elements [ k ] [ j ] ; } } } } return temp ; }

Matrix / d Check d = 0?

Extraction Operator << Insertion Operator