over loading matrix operations

hey guys i have done c++ program to overload operators for matrix. it works fine when i hard code the rows and cols. but when i take from the user it doesnt work. below is my code.. please help me
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
#include<iostream>
using namespace std;

class Matrix
{
    int i,j,k,data[2][2];
    static int rows,cols;
    public:void getdata(void);
    void display(void);
    Matrix operator +(Matrix);
    Matrix operator -(Matrix);
    Matrix operator *(Matrix);
    Matrix operator /(int);
    Matrix();
};
int Matrix::rows=2;
int Matrix::cols=2;

Matrix::Matrix()
{
   for(i=0;i<rows;i++){
       for(j=0;j<cols;j++){
           data[i][j]=0;   
       }
   }
}

Matrix Matrix::operator+(Matrix a)
{
    Matrix temp;
    for(i=0;i<rows;i++) {
        for(j=0;j<cols;j++){
            temp.data[i][j]=data[i][j]+a.data[i][j];
        }
    }
        return temp;
}

Matrix Matrix::operator-(Matrix a)
{
    Matrix temp;
    for(i=0;i<rows;i++) {
        for(j=0;j<cols;j++){
            temp.data[i][j]=data[i][j]-a.data[i][j];
        }
    }
        return temp;
}

Matrix Matrix::operator*(Matrix a)
{
    Matrix temp;
    for(i=0;i<rows;i++) {
        for(j=0;j<cols;j++){
            for(k=0;k<rows;k++){
            temp.data[i][j]+=data[i][k]*a.data[k][j];
            }
        }
    }
        return temp;
}


Matrix Matrix::operator/(int a)
{
    Matrix temp;
    for(i=0;i<rows;i++) {
        for(j=0;j<cols;j++){
            temp.data[i][j]=data[i][j]/a;
        }
    }
        return temp;
}




void Matrix::getdata()
{
   /**cout<<"Enter the no of rows and columns of matrix";
    cin >> rows >> cols;*/
    cout<<"Enter the elements of matrix\n";
    for (  i = 0 ; i <rows; i++ )
      for ( j = 0 ; j <cols ; j++ )
         cin >> data[i][j];
}

void Matrix::display()
{
     for ( i = 0 ; i <rows ; i++ )
   {
      for ( j = 0 ; j <cols ; j++ )
         cout << data[i][j] << "\t";
 
      cout << endl;
   }
}

int main()
{
    
    Matrix m,n,q;
    m.getdata();
    //division : scaler
    q=m/2;
    q.display();
    
 //for addition,subtraction,multiplication
    /* do as follows
     * m.getdata();
     * n.getdata();
     * q=m+n for addition ,  q=m-n for subtraction,   q=m*n for multiplication
     * finally display the resultant matrix
     * q.display
     */
   return 0;
}
"data[2][2];"
In this case will not change only in the scope specified which is the first two rows and the first two columns

to Solve your problem simply you can increase the scope of the matrix to become as follows
"data[100][100];"

or u can type any number

sorry for my bad english
Last edited on
i have changed . but still am not able to display the resultant matrix below is my code

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
#include<iostream>
using namespace std;
//int i,j;
class matrix
{
     int row,col;
    int mat[100][100],i,j;
    public:
    void get_matdim();
    void get_matval();
    matrix operator +(matrix);
    matrix operator -(matrix);
    matrix operator *(matrix);
    matrix operator/ (matrix);
    void display();
    matrix()
    {
        row=0;
        col=0;
    }
};

void matrix::get_matdim()
{
    cout<<"enter rows n columns";
    cin>> row >> col;
}
void matrix::get_matval()
{
     for (  i = 0 ; i <row; i++ )
      for ( j = 0 ; j <col ; j++ )
         cin >> mat[i][j];
}

matrix matrix::operator +(matrix m)
{ 
    matrix temp;
    for(i=0;i<row;i++)
	    for(j=0;j<col;j++){
		temp.mat[i][j]=mat[i][j]+m.mat[i][j];
		cout<<temp.mat[i][j];
		}

      cout<<"\n the sum of the two matices is:\n";
    return temp;
}
matrix matrix::operator -(matrix a)
{
    matrix temp;
    for(i=0;i<row;i++) {
	for(j=0;j<col;j++){
	    temp.mat[i][j]=mat[i][j]-a.mat[i][j];
	}
    }
    cout<<"\n the difference of the two matices is:\n";
	return temp;
}

matrix matrix::operator*(matrix a)
{
    matrix temp;
    int k;
    for(i=0;i<row;i++) {
	for(j=0;j<col;j++){
	    for(k=0;k<row;k++){
	    temp.mat[i][j]+=mat[i][k]*a.mat[k][j];
	    }
	}
    }
    cout<<"\n the product of the two matices is:\n";
	return temp;
}


matrix matrix::operator/(matrix a)
{
    matrix temp;
    for(i=0;i<row;i++) {
	for(j=0;j<col;j++){
	    temp.mat[i][j]=mat[i][j]/a.mat[i][j];
	}
    }
    cout<<"\n the division of the two matices is:\n";
	return temp;
}
void matrix::display()
{
    for(i=0;i<row;i++){
	  for(j=0;j<col;j++){
		cout<<"\t"<<mat[i][j];
		}
	      cout<<"\n";
	  }
}
int main()
{
    matrix m1,m2,m3;
    cout<<"enter matrix 1 dimensions";
    m1.get_matdim();
    cout<<"enter matrix 1 values: ";
    m1.get_matval();
    cout<<"\nenter matrix 2 dimensions";
    m2.get_matdim();
    cout<<"\nenter matrix 2 values: ";
    m2.get_matval();
   
    m3=m1+m2;
    m3.display();
  
    return 0;
}
I think the problem is that operator+ (and the other operators) don't set the row and col values of the new matrix object.
yeah
set row and col of temp as of its operands.

Also I will suggest you to check if matrixes can be multiplied or not.

And matrix division doesn't work like this.I am not sure but you have to first find inverse of divisor matrix and then multiply it with dividend.
akshit how to set row and col of temp as of its operands. for matrix multiplication??
Last edited on
if matrix one is: m:n
and matrix two is: p:q

then for multiplication n should be equal to p and new matrix will be m:q

you can do programming for this i suppose.
ya i tried that i took 4 by 2 matrix and 2 by 3 matrix . the resultant matrix is 4 by 3

1
2
temp.row=row;
 temp.col=m.col;


but am getting the following result. ie last column is 0

3 4 0
7 10 0
3 4 0
7 10 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
matrix matrix::operator*(matrix a)
{
    matrix temp;
    if(col!=a.row)
    {
          cout<<endl<"Cannot multiply these matrices!!!!";
          return temp;    //here temp row and col =0;
    }
    temp.row=row;
    temp.col=a.col;
    int k;
    for(i=0;i<row;i++) {
	for(j=0;j<col;j++){
            temp.mat[i][j]=0;          // for += use initialization
	    for(k=0;k<col;k++){     //you used row here
	    temp.mat[i][j]+=mat[i][k]*a.mat[k][j];
	    }
	}
    }
    cout<<"\n the product of the two matices is:\n";
	return temp;
}
Last edited on
still am getting the same result. ie last column is zero
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
matrix matrix::operator*(matrix a)
{
    matrix temp;
    if(col!=a.row)
    {
          cout<<endl<<"Cannot multiply these matrices!!!!";
          return temp;    //here temp row and col =0
    }
    temp.row=row;
    temp.col=a.col;
    int k;
    for(i=0;i<temp.row;i++) {
	for(j=0;j<temp.col;j++){
            temp.mat[i][j]=0;          // for += use initialization
	    for(k=0;k<col;k++){     //you used row here
	    temp.mat[i][j]+=mat[i][k]*a.mat[k][j];
	    }
	}
    }
    cout<<"\n the product of the two matices is:\n";
	return temp;
}


row and col of temp
yea thanks, now its working :)
Topic archived. No new replies allowed.