Hi there. I am creating a mock up of a physics library currently, and I am currently trying to deal with 2d arrays. Right now I am having issues with transposing the array. here is the code with the messy stuff cut out
//TMatrix.h
#pragma once
#include "main.h"
class TMatrix
{
public:
TMatrix(void);
TMatrix(double mx00, double mx01, double mx02, double mx10,
double mx11, double mx12, double mx20, double mx21, double mx22);
double _Mx [3][3];
~TMatrix(void);
TMatrix trans(TMatrix m1); //transpose
//other funtions
void printm();
};
//TMatrix.cpp
#include "TMatrix.h"
TMatrix::TMatrix(void)
{
}
TMatrix::TMatrix(double mx00, double mx01, double mx02,
double mx10, double mx11, double mx12,
double mx20, double mx21, double mx22) {
_Mx[0][0]=mx00; _Mx[0][1]=mx01; _Mx[0][2]=mx02;
_Mx[1][0]=mx10; _Mx[1][1]=mx11; _Mx[1][2]=mx12;
_Mx[2][0]=mx20; _Mx[2][1]=mx21; _Mx[2][2]=mx22;
}
TMatrix::~TMatrix(void)
{
}
//transpose matrix
TMatrix TMatrix::trans(TMatrix m1)
{
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
m1._Mx[i][j] = m1._Mx[j][i];
}
}
return *this;
}
//print function
void TMatrix::printm()
{
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
cout << " " << _Mx[i][j] << " ";
}
putchar('\n');
}
cout << "\n" << endl;
}
In main I am testing it with this:
//build matrices
TMatrix m1, m2, m3;
m1 = TMatrix(1, 0 ,0,
0, 1, 0,
0, 0, 1);
m2 = TMatrix(1, 2 ,3,
4, 5, 6,
7, 8, 9);
//transpose matrix
m2.printm();
m2.trans(m2);
m2.printm();
The current output is :
1 2 3
4 5 6
7 8 9.
Any idea what's the issue here?