# Thread: Matrices (+,-,*)

1. ## Matrices (+,-,*)

Hi everyone. I am trying to create a matrix. I'm trying to write a matrix program that the user will input the matrices and the program will add, subtract, and multiply the 2 matrices.
Before performing the required data manipulation, each function should validate its input arguments. The dimensions of the matrices involved in an operation must be compatible for that operation.

I understand how to add and subtract matrices. I have been looking up how to multiply them. I understand that most of it will need revision. I have been getting confused lately in class.

I need to figure out what all to put in the private section of the header file.
Also, I don't quite understand the a.columns = b.rows; //i, j element of the product (line 22 in implementation/resource file). I understand a is the first array and b is the second and the columns of a array must equal rows of b array. But I don't know how to declare the arrays or when to use the (.) period.

Here is what I have:
(It's not completed. There are a LOT of things I put in not as code but as a reminder of what I need to do, from my notes...)

Code:
```#ifndef MATRIX_H
#define MATRIX_H
using namespace std;
class matrix{
private:
int row, column;		//number of rows and columns used
double array[MAX_ROWS][MAX_COLUMNS];
public:
matrix(double[][MAX_COLUMNS], int, int);
matrix& operator += (matrix);		// a+=b
matrix& operator *= (double);		// a*=3
friend matrix operator+(matrix, matrix);		//c=a+b
friend matrix operator*(double, matrix);		//b=3*a
friend matrix operator*(matrix, matrix);		//c=a*b
friend ostream& operator << (ostream&, const matrix&);		//displays
};
#endif;		//MATRIX_H```
Implementation
Code:
```#include<iostream>
#include"matrix.h"
using namespace std;

const int MAX_ROWS = 3;
const int MAX_COLUMNS = 3;

matrix& operator += (matrix){
int sum = 0;

if( = ){
+;
sum = ;
else
cout << "Arrays not same size";
}

return*this;
}

friend matrix operator * (matrix, matrix){		//multiply 2 matrices
a.columns = b.rows;		//i, j element of the product

double productArray[MAX_ROWS][MAX_COLUMNS];

if(columns = rows){
productArray[i][j] = 0;
for(k=0){
productArray[i][j] += a[i][k]*b[i][k];
productArray.rows = a.rows;
productArray.columns = b.columns;

matrix c(productArray, rows, columns);

return c;
}
}
else
cout << "Error";

return a;
}```
Main
Code:
```#include"matrix.h"
#include<iostream>
using namespace std;
int main(){
double a[MAX_ROWS, MAX_COLUMNS];
for(int i=0; i<2; i++){
cout << "Enter three numbers: ";
for(int j=0; j<2; j++)
cin >> a[i][j];
}

matrix myMatrix (a,2,2);
myMatrix += myMatrix;
myMatrix b = myMatrix + 3 * matrix;
cout << b << endl;

return 0;
}```
Any and all help will be appreciated. (: Thanks!

2. Code:
`	a.columns = b.rows;`
In order to multiply to matrices A and B, you need the numbers of collumns of A to be equal to the number of rows of matrix B. I think that line is supposed to be an if statement
Code:
```matrix operator*(const matrix& a, const matrix & b)
if( a.columns == b.rows)
do matrix multiplication;
else
print error;```
why do you have global variables max_col and max_row??? Are your matrices always 3x3? If the dimensions are allowed to change, what you did was incorrect. Since you don't know the size of array, you have to dynamically allocate memory and deep copy the value; or use std::vector or valarray. Remember, if you use "new" in the constructor, you have to write the destructor with "delete".

Code:
```// constructor
matrix( double A[][] , int n = 3 , int m = 3):row(n),collumn(m)
{
// deep copy the value of A into the dynamically allocated array;
}```
And for the business with the '.' dot, think of it as apostrophe s ' 's ', its job is to indicate a possessive case.
Code:
`A.collumnns; // the collumn member of A`
Use it when you want to access members of a class.

3. I changed the multiplication function to:
Code:
```friend matrix operator * (matrix, matrix){		//multiply 2 matrices

double productArray[MAX_ROWS][MAX_COLUMNS];

if(a.columns = b.rows){			//i, j element of the product
productArray[i][j] = 0;
for(k=0){
productArray[i][j] += a[i][k]*b[i][k];
productArray.rows = a.rows;
productArray.columns = b.columns;

matrix c(productArray, rows, columns);

return c;
}
}
else
cout << "Error";

return a;
}```
I get errors saying a and b are undeclared identifiers. Where would I declare them at?

What exactly do you mean by deep copy?
Thank you.

4. And yeah that makes sense with the period. thanx (:

5. Code:
`matrix operator*(const matrix& a, const matrix & b)`
since you're passing a class, you mostly never want to pass by value. always pass by reference.

6. I understand that. It took away some of my errors. But how should I define the constructor in the header? and how do I use it in the main?

sorry for all the questions. I just haven't had much time to study my book and I don't have that much time left before I'm done with the class... so thank you.

7. I already gave you the skeleton of the constructor which is based on the one you had before. For that constructor, write it in the implementation file. Here it is again:
Code:
```// constructor
matrix::matrix( double A[][] , int n = 3 , int m = 3):row(n),collumn(m)
{
// deep copy the value of A into the dynamically allocated array;
}```
To initialize a matrix in main using that constructor
Code:
```double A[][] = something;
matrix matrixA (A,colum,row); // initialize matrixA```
you have to write the body of that constructor yourself.

BTW, you're going to need the copy constructor and the assignment operator as well.

Popular pages Recent additions