Adding two matrix objects and assigning the result to third object using overloaded operators ?
-
here ih the code below i have tried to add two matrix objects and assign it to the third object but it doesn't give expected result and the rather some garbage value.
include
using namespace std;class matrix{
int **p; //pointer to Matrix
int d1, d2;public:
//matrix(){} //default constructor
matrix(int x=20, int y=20); //parameterisedvoid get\_element(); void put\_element(); int checkAdd(matrix &); //to check the feasibility of the operation //Rule of three matrix operator + (matrix &); void operator = (matrix ); ~matrix(){ for (int i = 0; i < d1; i++) delete p\[i\]; delete p; }
};
matrix temp; //global matrix temp
//=============================================
void matrix::operator = (matrix obj){
this->d1 = obj.d1; this->d2 = obj.d2; for(int i=0; ip\[i\]\[j\] = obj.p\[i\]\[j\]; //put\_element(); //return(\*this);
}
matrix matrix::operator+(matrix &obj){
if(checkAdd(obj)==0)
{
//matrix temp;
for(int i=0; ip[i][j] + obj.p[i][j];//temp.put\_element(); return(temp) ; } cout<<"coloumn and rows of both matrix are not equal !"<d1 == obj.d1) && (this->d2 == obj.d2)) return 0; else return 1;
}
void matrix::get_element(){
for(int i=0; i -
here ih the code below i have tried to add two matrix objects and assign it to the third object but it doesn't give expected result and the rather some garbage value.
include
using namespace std;class matrix{
int **p; //pointer to Matrix
int d1, d2;public:
//matrix(){} //default constructor
matrix(int x=20, int y=20); //parameterisedvoid get\_element(); void put\_element(); int checkAdd(matrix &); //to check the feasibility of the operation //Rule of three matrix operator + (matrix &); void operator = (matrix ); ~matrix(){ for (int i = 0; i < d1; i++) delete p\[i\]; delete p; }
};
matrix temp; //global matrix temp
//=============================================
void matrix::operator = (matrix obj){
this->d1 = obj.d1; this->d2 = obj.d2; for(int i=0; ip\[i\]\[j\] = obj.p\[i\]\[j\]; //put\_element(); //return(\*this);
}
matrix matrix::operator+(matrix &obj){
if(checkAdd(obj)==0)
{
//matrix temp;
for(int i=0; ip[i][j] + obj.p[i][j];//temp.put\_element(); return(temp) ; } cout<<"coloumn and rows of both matrix are not equal !"<d1 == obj.d1) && (this->d2 == obj.d2)) return 0; else return 1;
}
void matrix::get_element(){
for(int i=0; iWhat result did you expect and what did you get?
-
What result did you expect and what did you get?
Quote:
So basically i want to add 2 matrix object and assign the result to the third non-initiated matrix object, without changing the values of first 2 original matrix.
this is the result i am getting.
Quote:
Enter matrix elements row by row m[0][0] = 1 m[0][1] = 2 m[1][0] = 3 m[1][1] = 4 m[2][0] = 5 m[2][1] = 6 1 2 3 4 5 6 m[0][0] = 7 m[0][1] = 8 m[1][0] = 9 m[1][1] = 10 m[2][0] = 11 m[2][1] = 12 7 8 9 10 11 12 8 10 47120720 0 0 0 0 0 0 0 50331651 41272 47127056 0 47120720 0 0 2396 136 0 12 14 47120720 0 1550742898 1148219457 1549890657 1633906508 1700027500 1426092141 1146242387 1229016399 1162100046 1330924371 1429482832 844253517 1398079566 1329877573 1313423693 1095717471 16 18 47120720 0 1162690894 1918981181 1426091637 1347568979 1229344594 1128088908 1934974010 1551069797 1970430292 1398145134 1127232561 1414417743 1397509967 1547322173 1735357008 544039282 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424
-
here ih the code below i have tried to add two matrix objects and assign it to the third object but it doesn't give expected result and the rather some garbage value.
include
using namespace std;class matrix{
int **p; //pointer to Matrix
int d1, d2;public:
//matrix(){} //default constructor
matrix(int x=20, int y=20); //parameterisedvoid get\_element(); void put\_element(); int checkAdd(matrix &); //to check the feasibility of the operation //Rule of three matrix operator + (matrix &); void operator = (matrix ); ~matrix(){ for (int i = 0; i < d1; i++) delete p\[i\]; delete p; }
};
matrix temp; //global matrix temp
//=============================================
void matrix::operator = (matrix obj){
this->d1 = obj.d1; this->d2 = obj.d2; for(int i=0; ip\[i\]\[j\] = obj.p\[i\]\[j\]; //put\_element(); //return(\*this);
}
matrix matrix::operator+(matrix &obj){
if(checkAdd(obj)==0)
{
//matrix temp;
for(int i=0; ip[i][j] + obj.p[i][j];//temp.put\_element(); return(temp) ; } cout<<"coloumn and rows of both matrix are not equal !"<d1 == obj.d1) && (this->d2 == obj.d2)) return 0; else return 1;
}
void matrix::get_element(){
for(int i=0; i -
You already posted this exact question in the ATL/STL forum, and I gave you a suggestion. If you google for "operator overload C++" you will find plenty of samples.
i was told that these type of questions are to be posted here. and i did the program..
#include
using namespace std;class matrix{
int **p; //pointer to Matrix
int d1, d2;
int id=0; //identif which operator is usedpublic:
//matrix(){} //default constructor
matrix(int x=20, int y=20); //parameterised//to pass private data-members to other objects int getD\_1(){return (d1);} int getD\_2(){return (d2);} //int getId(const int x){id =x ;} //void showId(){cout<<"ID :\\t"<d1 = obj.d1; this->d2 = obj.d2; this->id = obj.id; for(int i=0; ip\[i\]\[j\] = obj.p\[i\]\[j\];
}
//defining overloaded addition operator
matrix operator+(matrix &a, matrix &b){
matrix temp;
temp.d1 = a.d1;
temp.d2 = a.d2;
temp.id = 1;if(checkAdd(a, b) == 0){ //checks the viability if it's feasible or not. for(int i=0; i
-
i was told that these type of questions are to be posted here. and i did the program..
#include
using namespace std;class matrix{
int **p; //pointer to Matrix
int d1, d2;
int id=0; //identif which operator is usedpublic:
//matrix(){} //default constructor
matrix(int x=20, int y=20); //parameterised//to pass private data-members to other objects int getD\_1(){return (d1);} int getD\_2(){return (d2);} //int getId(const int x){id =x ;} //void showId(){cout<<"ID :\\t"<d1 = obj.d1; this->d2 = obj.d2; this->id = obj.id; for(int i=0; ip\[i\]\[j\] = obj.p\[i\]\[j\];
}
//defining overloaded addition operator
matrix operator+(matrix &a, matrix &b){
matrix temp;
temp.d1 = a.d1;
temp.d2 = a.d2;
temp.id = 1;if(checkAdd(a, b) == 0){ //checks the viability if it's feasible or not. for(int i=0; i
-
Forget about matrix calculations. You need to go and study operator overloading and get that working on a basic class first. Once you understand how to do it correctly so you get no errors, then you can move on to the matrix functions.
-
I don't know; you have to do your own testing. But I find one of the easiest way to debug issues is to create a small test program that just focused on the area that appears not to work. So a small program with a very basic class, that just tests the
operator+
overload, would be much easier to understand. -
I don't know; you have to do your own testing. But I find one of the easiest way to debug issues is to create a small test program that just focused on the area that appears not to work. So a small program with a very basic class, that just tests the
operator+
overload, would be much easier to understand.