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...)
Header
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!