Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
Matrix Class - 3 X 3 Matrices

This simple class has:

(1) an operator function to overload the * symbol to perform multiplication of a matrix by a scalar.  For example,

| 1 1 1 |                        | 2 2 2 |
| 2 2 2 |    times 2.0 gives     | 4 4 4 |
| 3 3 3 |                        | 6 6 6 |


This will work using the statement:

        mat2 = mat1*alpha;

However, in mat_driver.cpp, the statement

    mat2 = alpha*mat1;

will not work. Why?

Here's a friend function that will work for alpha*mat1 :

Matrix operator*( double alpha, Matrix& Z )
{
  Matrix temp;

  for(int i=0; i < 3; i++)
    for(int j=0; j < 3; j++)
      temp.M[i][j] = alpha * Z.M[i][j];

  return temp;
}

Remember to put the declaration in the Matrix.h file:

friend Matrix operator*( double, Matrix& );



(2) a friend function to create the transpose of a matrix.  The transpose switches the rows and columns.  For example, the transpose of

| 1 1 1 |         | 1 2 3 |
| 2 2 2 |    is   | 1 2 3 |
| 3 3 3 |         | 1 2 3 |


Class Declaration

% cat Matrix.h
 // Class declaration

class Matrix
{
   private:
      double M[3][3];

   public:
      Matrix(); // constructor
      Matrix operator*( double ); // operator* function
                                  // matrix*scalar
 
   // non-member friend functions

   friend Matrix transpose( Matrix & );

   friend istream& operator>>(istream&, Matrix& );
   friend ostream& operator<<(ostream&, Matrix& );

   
// for scalar*matrix   
   friend Matrix operator*(double alpha, Matrix& Z);
 
};


Class Implementation

% cat Matrix.cpp
 #include<iostream>
using namespace std;
#include <iomanip>
#include "Matrix.h"

// implementation section

// constructor

Matrix::Matrix()
{
   for (int i = 0; i < 3; i++)
     for (int j = 0; j < 3; j++)
      M[i][j] = 0.0;
}

// operator functions

Matrix Matrix::operator*( double alpha )
{  
  Matrix temp;
     
  for(int i=0; i < 3; i++)
    for(int j=0; j < 3; j++)
      temp.M[i][j] = alpha*M[i][j];
 
  return temp;
}

// non-member functions

ostream& operator<<(ostream& out, Matrix& q)
{
  for(int i=0; i < 3; i++)
  {
     for(int j=0; j < 3; j++)
         out <<  q.M[i][j] << " ";

     cout << endl;
  }

  return out;
}

istream& operator>>(istream& in, Matrix& q)
{
  for(int i=0; i < 3; i++)
     for(int j=0; j < 3; j++)
         in >> q.M[i][j];

  return in;
}

Matrix transpose( Matrix &Q )
{
  Matrix temp; 
  
  for(int i=0; i < 3; i++)
    for (int j=0; j < 3 ; j++)
      temp.M[j][i] = Q.M[i][j];
 
  return temp;
}


// friend for scalar*matrix
Matrix operator*(double alpha, Matrix &Z)
{
  Matrix temp;

  
for(int i=0; i < 3; i++)
    for (int j=0; j < 3 ; j++)
      temp.M[i][j] = alpha*Z.M[i][j];
 
  return temp;

}
 
Data File


 % cat matA
1 1 1
2 2 2
3 3 3


Driver Program & Execuation

% cat mat_driver.cpp
// driver program to test Matrix class

#include<iostream>
using namespace std;
#include <fstream>
#include "Matrix.h"

int main()
{
  char dataFile[21];
  ifstream file1, file2;

  Matrix mat1, mat2, mat3, mat4;

  cout << "Enter name of data file for matrix A: ";
  cin >> dataFile;

  file1.open(dataFile);

  file1 >> mat1;
  cout << mat1;

  cout << "\nScalar multiple" << endl;

  double alpha = 2.0;

  cout << "Use member operator function" << endl;
  mat2 = mat1*alpha; 
  cout << mat2;

  
cout << "\n\nUse friend operator function" << endl;
  mat3 = alpha*mat1
  cout << mat3;

  cout << "\nTranspose" << endl;

  mat4 = transpose( mat1 );  // friend function

  cout << mat4;

}


% g++ mat_driver.cpp Matrix.cpp
% a.out
Enter name of data file for matrix A: matA
1 1 1
2 2 2
3 3 3

Scalar multiple
Use member operator function
2 2 2
4 4 4
6 6 6


Use friend operator function
2 2 2
4 4 4
6 6 6

Transpose
1 2 3
1 2 3
1 2 3

 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming