3 like 1 dislike

設計一個類別Matrix代表矩陣。多載+、*、=。

Design a class Matrix represent matrix. Overloading the operator +, *, =.

Write a main program to use your class Matrix.

[Exercise] Essay (Open question) - asked in Final Exam
Available when: 2016-06-22 14:30:00 - Due to: 2016-06-22 17:00:00

reshown by | 31 views
0 0
Unable to finish :'(
0 0
#include<iostream>

using namespace std;

class Matrix
{
public:
    Matrix(int, int);
    Matrix(int x,int y, int **arr);
    const Matrix& operator*(const Matrix&);
    const Matrix& operator=(const Matrix&);
    const Matrix& operator+(const Matrix&);
    void MatrixOutput();




private:
    int row;
    int col;
    int **nums;

};

Matrix::Matrix(int x,int y)
{
    row = x;
    col = y;

    nums = new int*[row];
    for(int i=0; i<row; i++)
    {
        nums[i] = new int[col];
    }
}


Matrix::Matrix(int x, int y, int** arr)
{
    row = x;
    col = y;

    nums = new int*[row];
    for(int i=0; i<row; i++)
    {
        nums[i] = new int[col];
    }

    for(int i=0; i<row; i++)
        for(int j=0; j<col; j++)
        {
            nums[i][j] = arr[i][j];
        }


}

void Matrix::MatrixOutput()
{
    for(int i=0; i<row; i++)
        for(int j =0; j<col; j++)
        {
            cout<<nums[i][j]<<" ";
        }
}

const Matrix& Matrix::operator=(const Matrix& outside)
{

    /*if( outside.row != this->row || outside.col != this->col )
    {
        cout<<"Sorry Cannot assign, Matrix size different"<<endl;
    }*/

    for(int i=0; i<row; i++)
    {
        for(int j=0; j<col; j++)
        {
            this->nums[i][j] = outside.nums[i][j];
        }
    }

    return *this;
}

const Matrix& Matrix::operator*(const Matrix& outside)
{

    /*if( outside.row != this->row || outside.col != this->col )
        cout<<"Sorry Cannot assign, Matrix size different"<<endl;*/

    for(int i=0; i<this->row; ++i)
        for(int j=0; j<outside.col; ++j)
            for(int k=0; k<this->col; ++k)
            {
                this->nums[i][j]+=this->nums[i][k]*outside.nums[k][j];
            }

    return *this;
}

const Matrix& Matrix::operator+(const Matrix& outside)
{

    if( outside.row != this->row || outside.col != this->col )
    {
        cout<<"Sorry Cannot assign, Matrix size different"<<endl;
    }

    for(int i=0; i<row; i++)
    {
        for(int j=0; j<col; j++)
        {
            this->nums[i][j] += outside.nums[i][j];
        }
    }

    return *this;
}



int main()
{
    int x=0, y=0, x1=0, y1=0;
    int** arr, **arr1;

    cout<<"Enter 1st matrix size n*m: ";
    cin>>x>>y;
    arr = new int*[x];

    for(int i=0; i<x; i++ )
    {
        arr[i] = new int[y];
    }

    cout<<"Enter digits"<<endl;
    for (int i = 0; i < x; i++) //initialize
    {
        for (int j = 0; j < y; j++)
        {
            cin >> arr[i][j];
        }
    }


    Matrix one(x,y,arr);




    cout<<"Enter 2nd matrix size n*m: ";
    cin>>x1,y1;
    arr1 = new int*[x1];

    for(int i=0; i<x1; i++ )
    {
        arr1[i] = new int[y1];
    }

    cout<<"Enter digits"<<endl;
    for (int i = 0; i < x1; i++) //initialize
    {
        for (int j = 0; j < y1; j++)
        {
            cin >> arr1[i][j];
        }
    }


    Matrix two(x,y,arr1);





    Matrix temp(3,3);

    temp = one*two;





    return 0;
}
0 0
#include <iostream>
using namespace std;

class Matrix
{
    private:
        int lig,col;
        int tab[200][200];
    public:
    Matrix(){
        int i,j;
        for(i=0; i<lig; i++){
            for(j=0; j<col; j++){
                tab[i][j] = 0;
            }
        }
    }
    Matrix(int l, int c){
        lig = l;
        col = c;
    }
    void operator+ (Matrix& a){
        int i,j;
        Matrix temp;
        if(lig != a.lig || col != a.col)
            cout<<"A and B can not be sum up."<<endl;
        else{
            for(i=0; i<lig; i++){
                for(j=0; j<col; j++){
                    temp.tab[i][j] = tab[i][j] + a.tab[i][j];
                }
            }
        }
        for(i=0; i<lig; i++){
            for(j=0; j<col; j++)
                cout<< temp.tab[i][j]<<" ";
            cout<<endl;
        }
    }
    void operator* (Matrix& a){
        int i,j,k;
        Matrix temp;
        if(lig == a.col && col == a.lig){
            for(i=0; i<lig; i++){
                for(j=0; j<a.col; j++){
                    for(k=0; k<col; k++){
                    temp.tab[i][j] += tab[i][k]*a.tab[k][j];
                    }
                }
            }
            for(i=0; i<lig; i++){
            for(j=0; j<lig; j++)
                cout<< temp.tab[i][j]<<" ";
            cout<<endl;
        }
        }
        else
            cout<<"A and B can not be multiplied"<<endl;
    }





    void operator= (Matrix& a){
    int i,j,x=0;
        if(lig == a.lig && col == a.col){
            for(i=0; i<lig; i++){
                for(j=0; j<col; j++){
                    if(tab[i][j] != a.tab[i][j]){
                        x=1;
                        break;
                    }
                }
                if(x==1){
                    cout<<"A and B are not equals."<<endl;
                    break;
                }
            }
            cout<<"A and B are equals."<<endl;
        }
        else{
            cout<<"A and B are not equals."<<endl;
        }
    }

    void fill(){
    int i,j;
        for(i=0; i<lig; i++)
            for(j=0; j<col; j++)
                cin>>tab[i][j];
    }


};
int main(){
    int l,c,i,j;
    cout<<"Enter the number of lignes and colums for the matix A: ";
    cin>>l>>c;
    Matrix a(l,c);
    cout <<"fill the matrix A"<<endl;
    a.fill();

    cout<<"Enter the number of lignes and colums for the matix B: ";
    cin>>l>>c;
    Matrix b(l,c);
    cout <<"fill the matrix B"<<endl;
    b.fill();
    cout<<"A + B ="<<endl;
    a+b;
    cout<<"A * B ="<<endl;
    a*b;
    cout<<"B * A  ="<<endl;
    b*a;

    a=b;
    return 0;
}
0 0
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
class Matrix
{public:
    Matrix();
    Matrix(int a, int b);
     void operator+(Matrix &T);
    void operator*(Matrix &T);
    void operator=(int Arr[2][2]);
   private:
    int x[100][100], r[100][100];
int sizeI, sizeJ;
};

Matrix::Matrix(int a, int b)
{
    x[a][b];
    sizeI=a;
    sizeJ=b;
}

void Matrix::operator+(Matrix &T)
{
    r[sizeI][sizeJ];
    int i,j;
    for(i=0; i<sizeI; i++){
      for(j=0; j<sizeJ; j++)
        r[i][j]=this->x[i][j]+T.x[i][j];

    }
    for(i=0; i<sizeI; i++){
      for(j=0; j<sizeJ; j++)
        cout<< r[i][j];
        }


}
void Matrix::operator*(Matrix &T)
{int i,j;
    for(i=0; i<sizeI; i++){
      for(j=0; j<sizeJ; j++)
        r[i][j]=this->x[i][i]*T.x[j][j];

        }for(i=0; i<sizeI; i++){
      for(j=0; j<sizeJ; j++)
        cout<< r[i][j];
        }

}
void Matrix::operator=(int arr[2][2])
{int i=0,j=0;
while(i<sizeI){
    while(j<sizeJ)
        this->x[i][j]=arr[i][j];

}
}
int arrA[2][2]={(1,4),(3,7)};
int arrB[2][2]={(4,6),(5,8)};
int main()
{
 Matrix A(2,2) ;
 Matrix B(2,2);

  A=arrA;
  B=arrB;
    A+B;
    A*B;
    return 0;
}

gave up! haha..

8 Answers

0 like 0 dislike
Hidden content!
#include * ** * ** **



using namespace std;



class Matrix{



private:
** ** ** * * * * * * *** row;
* * * *** * * ** ** * ** col;
* *** * ***** * * *** m[99][99];



public:
* * * * * * ******* ** ***** v1=0, int v2=0):row(v1), col(v2){}
* * ** * **** ** defineMatrix();
* *** ****** * * * * * ****
* ** * *** *** * *** ** * ** *** *
** * *** * ** * * ** ** * * **
* * * * * **** * * * print();

};



void * *** ** * *
* ** ** * * **** i, j;
*** ** * * * * ** *** * **** * * **** the values for the **** **** * * ** ** endl;
********* ** ****** (i=0; i<col-1 ;i++){
* * ******* * * * * *** ** * ** * *** * *** * (j=0; j<row-1 ; j++){
** * * ** * * * * * * * *** * ***** *** * ** * * ** * * ** * ** *
** * * *** * * * ** **** * **** ** ** *
* * * *** **** * ** *

}



void **** * ***

{ int i, j;
**** * ** * **** * * ** (i=0; i<col-1 ;i++){
* *** ** * * * ***** * * *** *** ** *** (j=0; j<row-1 ; j++){
**** * ** ** ** **** * *** ** ** *** * ** ** * ******** * ** **** **
** * ** * * * *** ** * ** ** * **** ** *
**** * ** * *** * *** *** *** ******* * ** endl;
* ** * ** ***

}

// overloading +

//void * * * * * * * * *



//}



int main()

{
******* ** * ** * *** * * * m1(3,2);
* * *** * * * ** * * ** * ** *** *
** * * * ********** * * **** **
* * ** ** **** *** * * * * m2(3,2);


* ** ** * * * * * ** * 0;

}
answered by (-95 points)
0 like 0 dislike
Hidden content!
#include <iostream>

using namespace std;



class Matrix

{

private:
****** *** * *** ** * *mat;

public:
* ** ** ** * * * * * m[])
** *** * ** * * * *
*** * *** ** ** * * *** * ***** ** * ****** = m;
** ** ** *
*** * *** *** **** ** * operator+ (Matrix& ptr)
** * * * * * ** *** * *
**** * *** ****** * * ** ** **** * * * * i=0; i<6; i++)
* * * * ** *** * * * * ** * ** * * * * * ****
** * * *** ** * *** ***** ** * ** * * * * ** * * * *** * * * ***** * ** * << this->mat[i] + ptr.mat[i] << " ";
**** ** * * ** ** * ** ******* * * * * * **
** * * * ** * ** ** * * * * ** * *** * * * ******* << endl;
* * * **** ** * *** *
*** **** **** *** * ** operator* (Matrix& ptr)
**** * * * * * ***
* * * ** * * * * **** * * ****** * *** new_mat[3][3];
* ** *** ** ** ***** * * * ***** i=0; i<3; i++)
* * ** **** * * *** ****** *** *
* ** * **** * ** **** * ** ** * * * * * * * *** * ** * j=0; j<3; j++)
* *** ** * *** ** *** ** ** ** * *** ** * * * *** * * ** * **
* * * * * ** * * *** * * * ** ** ******** ** **** *** * * * * **** ** ***** *** * * ** * * ** * = this->mat[i][0]*ptr.mat[0][j] + this->mat[i][1]*ptr.mat[1][j];
* ** ** *** * **** ** * * * * *** * * * * *********** *
*** * * * ** * * * *** * ** ***** * *** *** **
* ** * * * * **** * ***** ** * ****** ***** ** * i=0; i<3; i++)
** ** *** * *** * **** ****** *** ** * **
**** * * ****** * * * ** *** * *** ***** ***** * ** ** * * ***** ** *** j=0; j<2; j++)
* * ** * * * **** * ***** * * ** * ****** * * * * * **** *
* ** *** **** * **** * ******* * ** ****** ** ** * ** * * * *** **** ** * ** * << new_mat[i][j] << " ";
* ** * * *** ****** *** ** ** *** * *** * ** *** * ** * ** *** **
* * * * * ** *** ** ********** * ****
** ******* * ** ** * * * * ** ** ** * << endl;
** *** * * ** * * ** **

};



int main()

{
*** * ***** ** ** pa[] = {1, 3, 1, 0, 1, 2};
* ** ** ******* * *** * * pb[] = {0, 0, 7, 5, 2, 1};


* * ***** *** ** ****** ** * PA(pa);
* **** * ******** ** * * ** PB(pb);


* ***** ** **** * + PB;


** * * ** * ** *** * 0;

}



Just operator +
answered by (16 points)
0 like 0 dislike
Hidden content!
#include<iostream>

#include<string>

#include<cstring>

using namespace std;



class matrix

{
*** *** **** *** * *** ** *
* * ** * *** * ** x;
** * * * * * **** * * y;
* ******* * * * ******* ** * z;
** *** ** ** * t;
** * *** * * * ** * * *** c, int d, string a):x(c),y(d),z(a){t=x*y;}
* * * *** * * ** **
*** *** * * ** ** * * * operator+(matrix &n)

    {
* * * ** * ***** * * ** * ** * * *** * ** D;
** *** * * *** * ****** ** *** **** d;
* *** ***** **** ***** * *** * ** ** * *** ** i;
* **** ** ** * * * * ** *** * ** ** * ***** * ** * ** **
* * ****** * * * ** ** ** * * * * *** ***** *
** * * **** * * * * * ** * * * *** ** ** * * * ** * ** **** ****** * ******
* * ** ** *** * * ** * ** ***** *
*** ** ** * * ** * **** * * ** * **** * ** * *****
** * * * * * ** * ** ** ** *** * *** D;

    }
* **** * * * ** ** * operator-(matrix &n)

    {
* ** ** * ** * * ** * ** * * * * * * ** ** D;
* * *** * * * * **** * * * *** * * * * * * d;
**** *** ***** * ** * * ** **** **** ***** * i;
** * * ***** *** * *** * ***** ** ** **** * * *** ****** ** **
** * ** **** ** ** * * * * * * * *****
* *** * * *** ***** *** * **** * ** * ** * *** * * *** * *** ******** * *
*** * ** *** * **** ** * ** ** * * * ** * *
***** ** * ** ***** * * *** ** *** * ** *
* **** ** ** ****** * * ****** * * ** *** ** ****** D;

    }
* *** * * * ** * *** * ** operator*(matrix &n)
****** * * ** ***
**** * ******** **** * * * * * * * ** ** * * d;
* ** * ** ** * * **** * ** * * ** * * * *
* * * * * ** ***** * ** ** * * *** **** *** * *
** ** * *** * ** * ****

};



int main()

{
** *** * * **** * * ** ** * how many rows, columns, then put in numbers by row then column(finish row then next row)";
*** ** * ****** *** ***** m1(2,3,"142332");
*** * * ** * ** ** * * * ** m2(2,3,"007521");
*** ** * *** * ** ** * ** m3;
** * ** * ** ** * ** *** *
** **** * * ** * ** ** ** **** *******
* * * * ** ******* * * * *
* *** * ** * * * * *
** * * ** * ** * ** *** ** * * * **** * **** * ** * **** can use * now";
** * ** * * * *** * ** * ** ** * ** * ** * **** ***

    }
** * * * * * ** *
* * * ** ** * *** *** * ** ** **** ** ** * * * **** ***** * * * **

}
answered by (-144 points)
0 like 0 dislike
Hidden content!
**** * * ** *
* * ** *** * * **
* **** **
* ** *
* * *
* * **** * * * *
* * ** ** * *
* * *
* * * ** *
* * * * * * * * *
* * **** * * * **
* * * * * * *** ** * *
* * * *
* * * **** * * ** *
* * * ** * * * * *
* * * * * ***
* * * * * *
* *
* * * **
* * * *
* * *** *
* * * **** * * * ***** **
* * * ** * * * * ** *
* * * ** *** * * * **** * * ********* ** ***
*
* * * * *** *** **** * ** *
* * *
* * *
*** * * ** * * * **
* * * ***
* *
* *

** * * ** ** ** * * * * *
* * * * ** * ** * * **** * * * * **
* * * ** * ** * *
* * * * * * * ** ***** *
* **** * * **** * * *
* * * * * *** ** * ** **** * * ***
* * *
* *
** *

*** *
*
* * * ***
* *
*
* **** * * **** * * * *
* * ** ** *
* * * * * ** * ** *
* * * * * ** *** **
* * * * * * *** * *
* * * * * * * *
* * ** * * * ** ** * * ** * *
* * * * * * * * ** ** * * * ***** **
* * * * * * *
* * * * * * **** * * *
* * * *
* * * *
* * * **
*
* **
* * * * * *
* * * * * *
* *


* *
*
* ** ** *
* *******
* * * *** * * *
* * ** ** * * *
* * * ***
* ** **
* *** *
* ** ** ** ***
* * * ***** * *
* *** * *** *
* *
* ** ** ** * **
* * *** ** * * * *
* *** **
* * *
* * *
**** ** **** * * * **** * ** ***
* * ** * *
* * * * * * *
* ** ** * ** ** ** * * * * ** * ** *
* ** ** *
** ** *
* **** * * * *
* * **** * ** ** * * ** ** ** * *
* * **** **
** ** * *** * * *
* * **** ** * * * ** * * * ** ***
* * * *
* *** * ** **
** * ** * ** * *** * ** * **** * *
* **** *
* ** *** ** ** *
* ** * * * *
* ** * * **** * * **** ** *** * * ** *******
* * **** **
** ** * ***
* * *
}
answered by (24 points)
0 0
#include <iostream>
#include <fstream>
using namespace std;

class Matrix{
    public :
        int **matrix , row , column;
        Matrix(int a , int b){

                row = a ;
                column = b ;

                int **matrix1 = new int*[a];
                for(int i = 0 ; i < a ; i ++){
                    matrix1[i] = new int[b] ;
                }

                for(int i = 0 ; i < a ; i ++){
                    for (int j = 0 ; j < b ; j ++){
                     matrix1[i][j] = 0;
                    }
                }

            matrix = matrix1 ;
            }

        void printMatrix(){
            for(int i = 0 ; i < row ; i ++){
                for (int j = 0 ; j < column ; j ++){
                    cout << matrix[i][j] << " " ;
                }
            cout << endl ;
            }
        }

        void MatrixSet(int x , int y , int data){
            matrix[x][y] = data ;
        }

};


Matrix operator + (Matrix& m1 , Matrix& m2){
        if((m1.column == m2.column)&&(m1.row == m2.row)){
                Matrix m3(m1.row ,m1.column) ;
                for(int i = 0 ; i < m1.row ; i ++){
                        for (int j = 0 ; j < m1.column ; j ++){
                            m3.MatrixSet(i , j ,m1.matrix[i][j] + m2.matrix[i][j]);
                        }
            }
        return m3 ;
        }
        else {
            Matrix m4(0, 0);
            return m4 ;
        }
    }

    Matrix operator * (Matrix& m1 , Matrix& m2){
        if(m1.column == m2.row){
                Matrix m3(m1.row ,m2.column) ;
                for(int i = 0 ; i < m1.row ; i ++){
                        for (int j = 0 ; j < m1.column ; j ++){
                                int answer = 0 ;
                                for(int a = 0 ; a < m1.column ; a ++){
                                        answer = answer + m1.matrix[i][a]*m2.matrix[a][j] ;
                                        }
                            m3.MatrixSet(i , j ,answer);
                    }
            }
        return m3 ;
        }
        else {
            Matrix m4(0, 0);
            return m4 ;
        }
    }



int main()
{
    Matrix m1(3 ,2);
    m1.MatrixSet(0 , 0 , 1);
    m1.MatrixSet(0 , 1 , 3);
    m1.MatrixSet(1 , 0 , 1);
    m1.MatrixSet(1 , 1 , 0);
    m1.MatrixSet(2 , 0 , 1);
    m1.MatrixSet(2 , 1 , 2);

    Matrix m2(3 ,2);
    m2.MatrixSet(0 , 0 , 0);
    m2.MatrixSet(0 , 1 , 0);
    m2.MatrixSet(1 , 0 , 7);
    m2.MatrixSet(1 , 1 , 5);
    m2.MatrixSet(2 , 0 , 2);
    m2.MatrixSet(2 , 1 , 1);

    Matrix m4(2 ,3);
    m4.MatrixSet(0 , 0 , 1);
    m4.MatrixSet(0 , 1 , 4);
    m4.MatrixSet(1 , 0 , 2);
    m4.MatrixSet(1 , 1 , 3);
    m4.MatrixSet(2 , 0 , 3);
    m4.MatrixSet(2 , 1 , 2);

    Matrix m5(3 ,2);
    m5.MatrixSet(0 , 0 , 1);
    m5.MatrixSet(0 , 1 , 2);
    m5.MatrixSet(1 , 0 , 3);
    m5.MatrixSet(1 , 1 , 2);
    m5.MatrixSet(2 , 0 , 0);
    m5.MatrixSet(2 , 1 , -1);




    cout << "matrix 1 :" << endl ;
    m1.printMatrix();
    cout << endl ;

    cout << "matrix 2 :" << endl;
    m2.printMatrix();
    cout << endl ;

    Matrix m3 = m1 + m2 ;
    cout << "matrix1 + matrix 2 :" << endl ;
    m3.printMatrix();
    cout << endl ;

    cout << "matrix 4 :" << endl ;
    m4.printMatrix();
    cout << endl ;

    cout << "matrix 5 :" << endl;
    m5.printMatrix();
    cout << endl ;

    Matrix m6 = m4 * m5 ;
    cout << "matrix1 * matrix 2 :" << endl ;
    m6.printMatrix();
    cout << endl ;

    return 0;
}
1 like 0 dislike
Hidden content!
*
* *** * **** *
*

*
* * * *
* ** ** * * *
* * ****** *** * **** * *
* * *** * *** * *** **
* *** ** * ***** ****
* * * *** **
*
*
*
* * *
* *
*

** * *

*

* * * * * * * **

** * **
* * * *
* * *
***** ** ** * ** ****** ***
* * *
* ** * ** **** * * * * * * * * * * ** *
***

** *** ** *** ***
{
* ** * *
{
* * * * * * * ** ****
* * }
}

** * *
}
* ** * ***** *** * * ** * * * **
{

***** *** * ** * ** * ** * ** ** ** **
* *** * * **** * * * * * * * * ** * ** *** * * ** ****

* * * * ** * * ***
* ** **** ***

* * ** * * * ** * **** ** **** ********* * * *
* * *
* *** **

** * *** ** * * * ** *** *


*** * ****** * ** ** * ***** )
{
* * **** *** ** * * * ** * * * * ** * * ***
}

* *** * * * * *
{
* * * * * *
{
** *** * *** * *** ***
* * *
* *
* * ** * **
*
*
* **
*
* * * *
**
* *** *** * * * * * * ** *
* * * * ** ** ****
* * * * ***
* * * ** * * ****** * * * ** *** *
* * * *** * ** * **
* *
* * ** * * *
*

*
* * ** *** ** **** * * * * ** * * * *
* * * ***** *
** * ** * *
* ** ** *
* *** * ****
*


* **
}
answered by (6 points)
0 0
#include<iostream>

using namespace std;

class Matrix
{
public:
    Matrix(int, int);
    Matrix(int x,int y, int **arr);
    const Matrix& operator*(const Matrix&);
    const Matrix& operator=(const Matrix&);
    const Matrix& operator+(const Matrix&);
    void MatrixOutput();




private:
    int row;
    int col;
    int **nums;

};

Matrix::Matrix(int x,int y)
{
    row = x;
    col = y;

    nums = new int*[row];
    for(int i=0; i<row; i++)
    {
        nums[i] = new int[col];
    }
}


Matrix::Matrix(int x, int y, int** arr)
{
    row = x;
    col = y;

    nums = new int*[row];
    for(int i=0; i<row; i++)
    {
        nums[i] = new int[col];
    }

    for(int i=0; i<row; i++)
        for(int j=0; j<col; j++)
        {
            nums[i][j] = arr[i][j];
        }


}

void Matrix::MatrixOutput()
{
    for(int i=0; i<row; i++)
        for(int j =0; j<col; j++)
        {
            cout<<nums[i][j]<<" ";
        }
}

const Matrix& Matrix::operator=(const Matrix& outside)
{

    /*if( outside.row != this->row || outside.col != this->col )
    {
        cout<<"Sorry Cannot assign, Matrix size different"<<endl;
    }*/

    for(int i=0; i<row; i++)
    {
        for(int j=0; j<col; j++)
        {
            this->nums[i][j] = outside.nums[i][j];
        }
    }

    return *this;
}

const Matrix& Matrix::operator*(const Matrix& outside)
{

    /*if( outside.row != this->row || outside.col != this->col )
        cout<<"Sorry Cannot assign, Matrix size different"<<endl;*/

    for(int i=0; i<this->row; ++i)
        for(int j=0; j<outside.col; ++j)
            for(int k=0; k<this->col; ++k)
            {
                this->nums[i][j]+=this->nums[i][k]*outside.nums[k][j];
            }

    return *this;
}

const Matrix& Matrix::operator+(const Matrix& outside)
{

    if( outside.row != this->row || outside.col != this->col )
    {
        cout<<"Sorry Cannot assign, Matrix size different"<<endl;
    }

    for(int i=0; i<row; i++)
    {
        for(int j=0; j<col; j++)
        {
            this->nums[i][j] += outside.nums[i][j];
        }
    }

    return *this;
}



int main()
{
    int x=0, y=0, x1=0, y1=0;
    int** arr, **arr1;

    cout<<"Enter 1st matrix size n*m: ";
    cin>>x>>y;
    arr = new int*[x];

    for(int i=0; i<x; i++ )
    {
        arr[i] = new int[y];
    }

    cout<<"Enter digits"<<endl;
    for (int i = 0; i < x; i++) //initialize
    {
        for (int j = 0; j < y; j++)
        {
            cin >> arr[i][j];
        }
    }


    Matrix one(x,y,arr);




    cout<<"Enter 2nd matrix size n*m: ";
    cin>>x1,y1;
    arr1 = new int*[x1];

    for(int i=0; i<x1; i++ )
    {
        arr1[i] = new int[y1];
    }

    cout<<"Enter digits"<<endl;
    for (int i = 0; i < x1; i++) //initialize
    {
        for (int j = 0; j < y1; j++)
        {
            cin >> arr1[i][j];
        }
    }


    Matrix two(x,y,arr1);





    Matrix temp(3,3);

    temp = one*two;





    return 0;
}
1 like 0 dislike
Hidden content!
*
*** * * * *
** ** * ***

**
** * ** *
* * *
* ** * * * * * **** * **
* * * **** **
* *
* *
* * ** * * *
*
* * * ** *
{
* *

**

** ** * * * * ** ** *
}
** ** * * *
{
* *

* ** * ***
** * ** * ***
** * * * ***
*** * * * *
** ** *** * * ** ******

* *** *
}
* * * ********** * ***** **
{

* ***

* * * * ****
* ** *
** ** **
* ** ** *** * ** * **** * * *

** *
}
* *
{
* ** **
***
*** * ** * * * ? ? ******
* * *
*** ** * * * **
* *
** *** *** * ** ? ? * * **
* **** **** * **
* * * * * * * * * **
* **

*****
** *** * * *** * **
** **

* ** *** * ******** * * *** * * **
* *** *** * *** *** ***
* * *** * * *** * ** **
* * * * *
** * * * ** * * * * * *****
* ** ** **** ** ****
* ** * ** *** * * * *
* ** *
* **
* ** * *** *
* *
* *
* ****** * * * **
* *** *
* ****

*
*
*
*


*
*

*
* *
* *
*



answered by (-121 points)
0 like 0 dislike
Hidden content!
** * * ** * *******
* * *** * * *
* * *** ** * *
** * ****** * *** * : ** * * ***
{
***
* * * * * **** **** : * * * * * ** ***** * * * *** {}
};

{
** * ** * ******* * * * * * *** ** ** * ** ***** *** * * *
*
* * *** r, *
* **** ** * * * ** * **
* * * *** ** * * **** * * *** ****
** * * *** * * ****
** *** * *** * * ** ** * ** *
* * **** * * * ** * ** * **
** ** ****** * * ***** ** ** ** *
* ***
*
****
* **
** ** * * *** ** ** *** * * * * *
* **
* * ****** * *** * *** **** * * * * *
* * * * * ** ****
* * * ** ** * *
* * * * *** * ** * * ** * *
** * * * * **
* * * * ***
};
* ** ** r = 0, * c = 0) : * * * *** * *
{
* i = 0; i != r;
**** **** ***** * *** ***
}
*** * * * * * * * += ** ** ****
{
* * == ** *** * * *** * == *****
{
** * * = * *** ***
** ** = *
* (;
* != * *** * ** * != *** * * *
*** ** * **
{
* ** * = * *** * *
* ** = *** ****
* (;
* ** != * **** * ** * * * != * * * *** *
** *
+= ** *
}
}
* *
{
*** = ** *
** ****** *** *** **
}
*** ***
}
* * ** *** * * ******
{
* * ** = * **
+=
** ** *
}
* ** ** * * * * * *** -= * * * * *
{
** == ** * ** ** ** * ** * * ** *
*
* * ** = *** ** **
** * ** = ** * ** *
* *
* * * * * * * * * *** *
* * ***
{
* = * * * ** **
* * ** = * ******
** *
* * **** ** * * ** != * ** * * *
* *** * *
* -= **
}

**
{
** = **** *
* ** * ** ***** * * **
}
*** * *
}
** * ***** * * ** * * * *** **
{
** * * = ** *
-= *
* * * ****
}
** *** * *** ****** *** * * *
{
** ***** != * *
** * ***** *** ****
**** = * * * *
** ** *
}
* * * * ** *** * * * * * ** *
{
*** *** ** != * * *
* ** * *** **

* * * * * * * ** ** *
* * i = 0; i != * ** *** * * *
* ** j = 0; j != * * * *
{
*** ** * c = * r = 0; c != * ** *** * * r != *** ** * *
{
** a = * ** ** *
* * b = ** * *
* ** += a * b;
}
}

* ** *
}
* ******** * * * ** ********* ***** ** * ** * ****
{
* ** i = 0; i != *
** * j = 0; j != ** * *
* * * ** * * ** *
** *
}
*** * *
{
* ** i = 0; i != ***
{
** j = 0; j != ** * *
* * ** * * * * ** * ** * * ** ** *
** * * *** **
*
* * ***** * * * ****
}
**** *
* **
{

** ***** * * **** * * * *** * *** *
* * 1 2 3 4 5 6 7 8
* * ** ** ***
** * * *
*******
* ** * ** *
** *
**
* **

***** ** ** * * * ** * * * * *** ** ** ** ***
* ** * ** 1 3 1 0 1 2 0 0 7 5 2 1
* * ** **
** * * * * * c * * ** *
*
* ***

*** * * * * ** **** * * ** * * *** * *** * ** * * **
* * * 1 4 2 3 3 2 1 2 3 2 0
* * ** * * *
* ****** e * * * *
**

e
**
**
** ** * ** **

** * ** * * * ** * ** *
*

*** * * * * ***** * ** * * *** * * ** * **
* * * * * 1 4 2 3 3 2 1 2 3 2 0
**** * * *
* * **** *** g *** *
* *** * * * **** x B = * * * **** * ***
= g * *
* **
** * ***** ** x A = * ** * * * *
= h * *
* *

* ****** * * * * * * * ** ** *** * *** * * * *
* * * *
** * 1 2 3 4 1 4 2 3 3 2
** * * ** ****** g * *
*
**
g = g *
* ** *

** ** ** * *** *
**
** ****** * ** * * * * * *** **
*

** ** *** * * ****** * ** ** ** *** ** ** * * *
* **
}
answered by (96 points)
edited by
0 like 0 dislike
Hidden content!
* **

**** * *
*****
** ****
*** * *
* * ** * ** * * *** ***** **** * **
** * * *** * ** *** *** * * * * ** ****
* ******* **

* * ********
{
*** * ** * *
}
* * **
{
* * *** ** *****
}
}
* ** * *
{
** ** ** * *
* ** *
** * ***** *
*** * ** *** * *
{
* ** **
{
* * * * ***** ***
}
** **** * *** *

*** * ******
*
* ** **** ** *
* *
* ** ** * * *** ***
* }
** *** * **
}
* * * *** *
* ** ** * ****
{
**** * *** **
*
* **** **** *
* *
* * * ****** * ** *
}
answered by (-605 points)
Get it on Google Play Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:162.158.79.107
©2016-2018

Related questions

7 like 0 dislike
25 answers
[Exercise] Coding (C) - asked Jun 22, 2016 in Final Exam
Available when: 2016-06-22 14:30:00 - Due to: 2016-06-22 17:00:00
| 33 views
7 like 0 dislike
44 answers
[Exercise] Multichoice - asked Jun 21, 2016 in Final Exam
Available when: 2016-06-22 14:30:00 - Due to: 2016-06-22 17:00:00
| 17 views
5 like 1 dislike
24 answers
[Exercise] Coding (C) - asked Jun 21, 2016 in Final Exam
Available when: 2016-06-22 14:30:00 - Due to: 2016-06-22 17:00:00
| 34 views
6 like 0 dislike
32 answers
[Exercise] True/False - asked Jun 21, 2016 in Final Exam
Available when: 2016-06-22 14:30:00 - Due to: 2016-06-22 17:00:00
| 29 views
6 like 0 dislike
52 answers
asked Jun 21, 2016 in Final Exam by admin (9.7k points) | 25 views
2,974 questions
57,433 answers
29,089 comments
938 users