Work with matrix
May 17, 2016 at 9:14am UTC
Hello guys,
I am a beginner in this domain, and I'm learning right now about 2d arrays that are dynamically allocated. I've already done this, a code that computes the sum of two matrix. I want to know if someone can give me some ideas about some codes that are doing the multiplication of two arrays
(only compatible dimensions), multiplication by scalar, the transposed matrix, and also a method to resize the matrix. I've google it so many times, and i don't want to think that the problem is me not understanding this elementary things. Have a nice day :)
PS:Sorry if my english is bad:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
class matrix
{
int row, col; // array dim //
double * m; // pointer used //
public :
matrix(int =0, int =0);
matrix(const matrix&); // copy constructor //
~matrix(); // destructor //
void read();
void print();
matrix& operator =(const matrix&);
matrix operator +(const matrix&);
friend matrix operator -(const matrix&, const matrix&);
matrix operator *(const matrix&);
double max();
};
matrix::matrix(int r, int c)
{
row =r;
col = c;
m=new double [row*col];
for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
m[i*col+j]=0;
}
matrix::matrix(const matrix& mat)
{
row =mat.row;
col = mat.col;
m=new double [row*col];
for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
m[i*col+j]=mat.m[i*mat.col+j];
}
matrix::~matrix()
{
delete [] m;
}
void matrix::print()
{
cout << fixed << setprecision(2);
for (int i=0;i<row;i++)
{
for (int j=0; j<col; j++)
{
cout.width(8);
cout<<m[i*col+j];
}
cout<<endl;
}
}
void matrix::read()
{
for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
{
cout<<"\nelement (" <<i<<"," <<j<<"): " ;
cin>>m[i*col+j];
}
}
matrix matrix::operator +(const matrix& a)
{
int i,j;
matrix x(row>a.row? row: a.row , col>a.col? col : a.col);
for ( i=0;i<row;i++)
for ( j=0;j<col;j++)
x.m[i*x.col+j]=m[i*col+j];
for ( i=0;i<a.row;i++)
for ( j=0;j<a.col;j++)
x.m[i*x.col+j] += a.m[i*a.col+j];
return x;
}
matrix& matrix::operator =(const matrix& y)
{
if (row != y.row || col != y.col)
{
delete [] m;
row = y.row;
col = y.col;
m = new double [row*col];
}
for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
m[i*col+j]=y.m[i*y.col+j];
return *this ;
}
matrix matrix::operator -(const matrix& s)
{
int main()
{
int r, c;
cout<<"rows number = " ;
cin >> r;
cout<<"columns number = " ;
cin >> c;
matrix a(r,c);
cout<<"\nread matrix a:\n" ;
a.read();
cout<<"\nmatrix a is:\n" ;
a.print();
matrix b(r, c);
cout<<"\nread matrix b\n" ;
b.read();
cout<<"\nmatrix b is:\n" ;
b.print();
matrix s=a+b;
cout<<"\nthe sum s = a+b is:\n" ;
s.print();
cin.ignore(100, '\n' );
cin.get();
}
May 17, 2016 at 12:00pm UTC
there are a lot of examples in the wide wide world... ^^
https://www.google.de/?client=firefox-b#q=c%2B%2B+matrix+multiplication&gfe_rd=cr
May 17, 2016 at 5:00pm UTC
i mean, if someone can help me in the way my code is written, this is the only way i learnt to dynamic allocate a matrix...
May 17, 2016 at 5:10pm UTC
Take this out of your code and the compiler is happy:
1 2 3 4
// matrix matrix::operator-(const matrix& s)
// {
Topic archived. No new replies allowed.